0

I am building a borderless application. What i need to achieve is to move the window by simply click and drag on an image it contains but i also want to do something when clicked.

this is how my app looks Just a button, i have functionality for left and right clicks but can't figure how to implement drag now. This is what my code looks like

private void btnHome_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if(e.LeftButton == MouseButtonState.Released)
        {
            toggle();
        }
        else if(e.LeftButton == MouseButtonState.Pressed)
        {
            DragMove();
        }
    }

Right now only drag works, If i rearrange their occurrence then only left click would work. Right click functionality is working as expected right now.

  • I found a similar question but can't figure out where to put the code that the author suggests is correct. My reputation is low so i cannot comment there https://stackoverflow.com/questions/1917176/c-sharp-wpf-dragmove-and-click – Mohammad Sheriyar Sid Nov 12 '17 at 19:40

2 Answers2

1

Do your thing once the DragMove() method has returned, i.e. when the mouse capture has been released:

private void btnHome_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        DragMove();
        System.Diagnostics.Debug.Write("some action...");
    }
}

Or, if you want to do something before the mouse is captured, you could use a boolean flag:

private bool _capture;
private void btnHome_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        if (_capture)
        {
            DragMove();
            _capture = false;
        }
        else
        {
            System.Diagnostics.Debug.Write("some action...");

            _capture = true;
            btnHome.RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left)
            {
                RoutedEvent = MouseDownEvent
            });
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

EDIT: Well I found the problem! You listen to MouseDownEvent!!!!

Just link the new event to the following new function =]

private void btnHome_MouseUp(object sender, MouseButtonEventArgs e)
{
     toggle();
}

private void btnHome_MouseDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
    RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left) 
    { 
        RoutedEvent = MouseLeftButtonUpEvent 
    });
}
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
  • I tried all the combinations by putting this snipped around, but nothing works. This does not work only drag is working not click function. I rearranged them but still only drag works. – Mohammad Sheriyar Sid Nov 12 '17 at 20:02
  • Nope. Here is a bigger picture of my current code. I encountered a strange behaviour. Still experimenting on my end https://1drv.ms/u/s!AiIsYCuW6f4yg9EcagXJ7wwQK4Uj7w – Mohammad Sheriyar Sid Nov 12 '17 at 20:24