【问题标题】:C# WPF Interupt DragMove with mouse eventC# WPF 用鼠标事件中断 DragMove
【发布时间】:2018-10-03 14:29:33
【问题描述】:

我面临以下问题: 在 WPF 中,我有一个 WindowStyle="None" 的窗口,因此我添加了一个按钮来使用 DragMove() 方法移动窗口。这部分工作正常。我还想要的是,当窗口到达某个位置时,它会停止 DragMove。 我的想法是通过提高 MouseLeftButtonLeft 来实现它,认为它会中断 DragMove,但事实并非如此。

移动窗口的按钮:

<Button Grid.Column="0" x:Name="MoveButton" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="3" Cursor="Hand">
            <Image x:Name="MoveImage" Source="images/move.png" MouseLeftButtonDown="MoveWindow" MouseLeftButtonUp="Poney" />
</Button>

移动窗口的方法:

// Move the window with drag of the button. Ensure that we are not over the taskbar
    private void MoveWindow(object sender, MouseButtonEventArgs e)
    {
        e.Handled = true;
        DragMove();

        // https://stackoverflow.com/questions/48399180/wpf-current-screen-dimensions
        System.Drawing.Rectangle workingArea = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle).WorkingArea;

        //The left property of the window is calculated on the width of all screens, so use VirtualScreenWidth to have the correct width
        if (Top > (workingArea.Height - Height))
        {
            Top = (workingArea.Height - Height);
        }
        else if (Left > (SystemParameters.VirtualScreenWidth - Width))
        {
            Left = (SystemParameters.VirtualScreenWidth - Width);
        }
        else if (Left < 0)
        {
            Left = 0;
        }
    }

引发事件的方法:

private void MainWindow_LocationChanged(object sender, EventArgs e)
    {
        // https://stackoverflow.com/questions/48399180/wpf-current-screen-dimensions
        System.Drawing.Rectangle workingArea = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle).WorkingArea;

        if(Left > 2000)
        {
            MouseButtonEventArgs mouseButtonEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = MouseLeftButtonUpEvent,
                Source = MoveImage
            };

            MoveImage.RaiseEvent(mouseButtonEvent);
            //InputManager.Current.ProcessInput(mouseButtonEvent);
        }
    }

检查是否引发了事件:

public void Poney(object sender, MouseButtonEventArgs e)
    {
        Console.WriteLine("Poney");
    }

我的控制台中显示“小马”,所以我猜引发事件的代码有效?

简而言之,我需要一种方法来中断 DragMove,以便我可以进行一些更改并重新启动 DragMove。

谢谢:)

PS : 2000 值用于测试,在“真实”中计算位置。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以使用原生的mouse_event 函数来合成鼠标向上事件,例如:

    const int MOUSEEVENTF_LEFTUP = 0x04;
    
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    
    void MainWindow_LocationChanged(object sender, EventArgs e)
    {
        if (Left > 1000)
        {
            Point point = Mouse.GetPosition(this);
            mouse_event(MOUSEEVENTF_LEFTUP, (uint)point.X, (uint)point.Y, 0, 0);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2011-08-17
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多