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.