0

I'm facing the following issue: In WPF I have a window with WindowStyle="None" and so I add a button to move the window with the DragMove() method. This part is working fine. What I want in addition is that when the window reach a certain position it's stop the DragMove. My idea was to make it by raising a MouseLeftButtonLeft thinking it will interrupt the DragMove but it isn't.

Button to move window:

<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>

Method to move the window:

// 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;
        }
    }

Method to raise event :

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);
        }
    }

Check that the event is raised :

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

I have the 'poney' displaying in my console, so i guess the code to raise the event is working?

In short version I need a method to interrupt the dragmove so I can make some change and the restart DragMove.

Thanks :)

PS : The 2000 value is used for test, in "real" the position is calculated.

S.Martignier
  • 383
  • 2
  • 4
  • 17

1 Answers1

1

You could use the native mouse_event function to synthesize a mouse up event, e.g.:

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);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88