IsMouseOver trigger doesn't work when using ShowDialog and borderless window

后端 未结 3 1754
谎友^
谎友^ 2020-12-28 22:35

I have two Windows for an application. One of them is MainWindow and the other is for settings. SettingsWindow opens when settings but

3条回答
  •  萌比男神i
    2020-12-28 23:15

    I think you have to observe the mouse position manually. For this you could use the code behind posted by Peheje here.

    I used this to program a working example. While leaving your window, the Button gets the correct style.

    using System.Runtime.InteropServices;
    using Point = System.Drawing.Point;
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(ref Point lpPoint);
    
    public bool IsMouseOverButton {
        get { return _isMouseOverButton; }
        set {
            if (value == _isMouseOverButton) return;
            _isMouseOverButton = value;
            OnPropertyChanged();
        }
    }
    
    public SettingsWindow()
    {
        InitializeComponent();
    
        new Thread(() =>
        {
            while (true)
            {
                //Logic
                Point p = new Point();
                GetCursorPos(ref p);
    
                //Update UI
                Application.Current.Dispatcher.Invoke(() =>
                {
                    double btnLeft = DlgWindow.Left;
                    double btnRight = btnLeft + DlgBtn.ActualWidth;
                    double btnBottom = DlgWindow.Top + DlgWindow.ActualHeight;
                    double btnTop = btnBottom - DlgBtn.ActualHeight;
    
                    IsMouseOverButton =
                        p.X >= btnLeft && p.X <= btnRight &&
                        p.Y >= btnTop && p.Y <= btnBottom;
                });
    
                //async wait (non blocking)
                (new ManualResetEvent(false)).WaitOne(100);
            }
        }).Start();
    }
    

    xaml

    
    
      
    

提交回复
热议问题