How to move a window using a button? c#

Asked

Viewed 248 times

0

I have an application in WPF that has no borders or background, it’s just a stylized button with an image and a frame, but I would like it to be possible to move it. My current code is like this:

    <Button Name="button" Margin="10,130.475,377.541,10" Click="button_Click">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Rectangle Width="Auto" Margin="29.78,15.35,20.479,13.896" RenderTransformOrigin="0.5,0.5" Stroke="{x:Null}">
                <Rectangle.Fill>
                    <ImageBrush ImageSource="img.png" Stretch="Uniform"/>
                </Rectangle.Fill>
            </Rectangle>
        </ControlTemplate>
    </Button.Template>
</Button>
//<...>

I tried this way, but I got no results

private void button_Click(object sender, RoutedEventArgs e)
        {
            DragMove();
            //<...>
        }

Edit: Following the answers, I adjusted this code to the application, however the movement only occurs with the right mouse and the left one does not generate any event.

private bool clicked = false;
private Point lmAbs = new Point();
void PnMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
{
    clicked = true;
    this.lmAbs = e.GetPosition(this);
    this.lmAbs.Y = Convert.ToInt16(this.Top) + this.lmAbs.Y;
    this.lmAbs.X = Convert.ToInt16(this.Left) + this.lmAbs.X;
}

void PnMouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
    clicked = false;
}

void PnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    if (clicked)
    {
        Point MousePosition = e.GetPosition(this);
        Point MousePositionAbs = new Point();
        MousePositionAbs.X = Convert.ToInt16(this.Left) + MousePosition.X;
        MousePositionAbs.Y = Convert.ToInt16(this.Top) + MousePosition.Y;
        this.Left = this.Left + (MousePositionAbs.X - this.lmAbs.X);
        this.Top = this.Top + (MousePositionAbs.Y - this.lmAbs.Y);
        this.lmAbs = MousePositionAbs;
    }
}

One way to get me to catch the event from the left mouse was by using the Previewmouseleftbuttondown, but then the window gets stuck to the cursor and I can’t release it.

  • use the onmousedown and onmouseup event associated with the button you want, and the associated onmousemove event on your screen, no down, mark a flag as true, no up, mark as false, no move, if the flag is true, move the screen along with the mouse

1 answer

1

There is a technique available in this project of Codeproject, she’s kind of like this:

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{     
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

This does exactly what Windows moves when you click on the window title and drag it. Not quite by the button, but you click and drag the window using the mouse and clicking the same window, not the button.

But if you really want to do by a button, you would have to associate mouse events to it:

private bool mouseDown;
private Point lastLocation;

private void button_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown = true;
    lastLocation = e.Location;
}

private void button_MouseMove(object sender, MouseEventArgs e)
{
    if(mouseDown)
    {
        this.Location = new Point(
            (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);

        this.Update();
    }
}

private void button_MouseUp(object sender, MouseEventArgs e)
{
    mouseDown = false;
}
  • That way he returns to me "CS0123 No Overload for 'button_MouseUp' Matches delegate 'Mousebuttoneventhandler'", just like Mousedown and Mousemove.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.