seanmet
Topic Author
Posts: 8
Joined: 19 Jun 2024, 10:01

IsMouseOver behavior

03 Nov 2024, 15:52

Hey,

Im trying to get a "hover" effect on my button and cant seem to figure out what the problem is with what im doing..

So I have a button and a control template which should convert the button from a round shape to some rectangluar shape when triggered on "IsMouseOver" trigger:

This is the control template:
        <!-- Define reusable ControlTemplate for round buttons -->
        <ControlTemplate x:Key="test" TargetType="Button">
            <Border x:Name="border" Background="{TemplateBinding Background}" CornerRadius="80" Width="160" Height="160">
                <!-- Using ContentPresenter to display button content (TextBlock) -->
                <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <!-- Triggers to change background color and content visibility on Button events -->
            <ControlTemplate.Triggers>
                <!-- Change background color when button is pressed -->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="border" Property="Width" Value="470"/>
                    <Setter TargetName="border" Property="Height" Value="160"/>
                    <Setter TargetName="border" Property="CornerRadius" Value="80"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
This is the usage:
        <Button Height="Auto" Width="Auto" Canvas.Left="1600" Canvas.Top="800" Background="#4DEBD7F8" Template="{StaticResource test}">
            <Image Source="/ScreenResources_HomeScreen;component/logo.jpeg" Width="160" Height="160"/>
        </Button>
        
I get very weird behaviour.. i dont get a hover effect without clicking the mouse..
If i hover over the button nothing happens, but if i left click and hold and then hover over the mouse, i get the desired behavior.

I've tested with XamlToy that you have on the site, and it seems as though the code is correct since i get the behavior i expect, so there is probably something wrong with my integration.. could you maybe try and guide me to might cause this?
I would really appreciate your help here :)
Thanks a lot in advance!
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: IsMouseOver behavior

05 Nov 2024, 12:48

Hi,

I guess you are already sending all the mouse events, including MouseMove, to the View:
NS_INTERFACE IView: public Interface
{
    /// Notifies that a mouse button was pressed. Origin is in the upper-left corner.
    /// Returns true if event was handled
    virtual bool MouseButtonDown(int x, int y, MouseButton button) = 0;

    /// Notifies that a mouse button was released. Origin is in the upper-left corner.
    /// Returns true if event was handled
    virtual bool MouseButtonUp(int x, int y, MouseButton button) = 0;

    /// Notifies that a mouse button was double clicked. Origin is in the upper-left corner.
    /// When a double click happens the following sequence of events are expected:
    ///  - MouseButtonDown (first click)
    ///  - MouseButtonUp
    ///  - MouseDoubleClick (second click)
    ///  - MouseButtonUp
    /// See https://docs.microsoft.com/en-us/windows/win32/learnwin32/mouse-clicks#double-clicks
    /// Returns true if event was handled
    virtual bool MouseDoubleClick(int x, int y, MouseButton button) = 0;

    /// Notifies that mouse was moved. Origin is in the upper-left corner.
    /// Returns true if event was handled
    virtual bool MouseMove(int x, int y) = 0;

    /// Notifies that mouse vertical wheel was rotated. Origin is in the upper-left corner
    /// The parameter wheelRotation indicates the distance the wheel is rotated, expressed in
    /// multiples or divisions of 120 (is the default value for 3 lines of scrolling). A positive
    /// value indicates that the wheel was rotated forward, away from the user; a negative value
    /// indicates that the wheel was rotated backward, toward the user.
    /// Returns true if event was handled
    virtual bool MouseWheel(int x, int y, int wheelRotation) = 0;

    /// Notifies that mouse horizontal wheel was rotated. Origin is in the upper-left corner
    /// The parameter wheelRotation indicates the distance the wheel is rotated, expressed in
    /// multiples or divisions of 120 (is the default value for 3 lines of scrolling). A positive
    /// value indicates that the wheel was rotated to the right; a negative value indicates that
    /// the wheel was rotated to the left.
    /// Returns true if event was handled
    virtual bool MouseHWheel(int x, int y, int wheelRotation) = 0;
    ...
};
Did you enable the touch emulation in the View? Because when touch is emulated, MouseMove events are ignored if no "finger" (left mouse button) is pressed.
virtual void SetEmulateTouch(bool emulate) = 0;
 
seanmet
Topic Author
Posts: 8
Joined: 19 Jun 2024, 10:01

Re: IsMouseOver behavior

05 Nov 2024, 17:27

Yes!!!! That fixed it, it was the emulator. thanks a lot!

Who is online

Users browsing this forum: No registered users and 3 guests