How to move a window using a button? W#

0

I have a WPF application that has no borders or background, it's just a stylized button with an image and a frame, but I wish it were possible to move it. My current code looks 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 did not get results

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

Edit: Following the answers, I've attached this code to the application, however the movement only occurs with the right mouse and the left does not generate any events.

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 the mouse event left is to use PreviewMouseLeftButtonDown , but in this case the window is stuck to the cursor and I can not release it.

    
asked by anonymous 06.01.2018 / 00:00

1 answer

1

A CodeProject technique is available in this project , it's more or less 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 it clicks on the window title and drags it. It is not right for the button, but you click and drag the window using the mouse and clicking on the window itself, not the button.

But if you really want to do it for a button, you would have to associate mouse events with 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;
}
    
06.01.2018 / 03:24