Reputation: 135
I'm trying to build my custom bar for my window and I'm working on the drag and drop code, but it is not working as expected.
It works 'ok' since it drags the window, but if I go a little bit faster with the mouse and it gets out of the DragBar it stops working. At Windows Forms it works smoothly with almost the same aproach, but on WPS i get this problem.
private Point mouseDown; //Mouse on click
private Point windowDown; //Window on click
private Vector mouseOffset; //Mouse on click - Mouse Actual Position
private bool drag; //If drag is happening (between mouse Down and mouse Up)
private void DragBar_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
mouseDown = PointToScreen(Mouse.GetPosition(this));
windowDown = new Point(this.Left, this.Top);
}
private void DragBar_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
mouseOffset = PointToScreen(Mouse.GetPosition(this)) - mouseDown;
this.Left = windowDown.X + mouseOffset.X;
this.Top = windowDown.Y + mouseOffset.Y;
}
}
private void DragBar_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
}
~~EDIT ___________________
Got a bit more efficiency with this method but still 'losing the grip' when the mouse gets too fast.
@Cody Gray I'll keep getting position from event e for good practice, but it doesn't seem to help the problem
private Point mouseDown; //Mouse position relative to form
private bool drag = false; //If drag is happening (between mouse Down and mouse Up)
private void DragBar_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
mouseDown = e.GetPosition(this);
}
private void DragBar_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
this.Left = PointToScreen(e.GetPosition(this)).X - mouseDown.X;
this.Top = PointToScreen(e.GetPosition(this)).Y - mouseDown.Y;
}
}
private void DragBar_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
}
Upvotes: 0
Views: 1965
Reputation: 35
If you want to make a custom Draggable window bar in WPF. here is how i would do it.
XAML:
<!-- #region costum menu -->
<Grid Grid.Row="0"
Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.5*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Logo and text -->
<Label Grid.Row="0" Grid.Column="0"
Margin="5, 0, 0, 0"
Content="appName"
VerticalAlignment="Center"
HorizontalAlignment="Left"
MouseDown="Window_MouseDown"
/>
<!-- Close and minimize buttons -->
<StackPanel Grid.Row="0" Grid.Column="1"
Margin="0, 0, 2, 0"
Orientation="Horizontal"
HorizontalAlignment="Right"
VerticalAlignment="Center" >
<Button Content="_"
Width="25"
Height="22"
Margin="0, 0, 5, 0"
Click="MinimizeThisWindow"
Background="Transparent"
/>
<Button Content="X"
Width="25"
Height="22"
Click="CloseThisWindow"
Background="Transparent"
/>
</StackPanel>
</Grid>
<!-- #endregion -->
C# backend:
/// <summary>
/// Allows the application to be dragable
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{ this.DragMove(); }
}
Done.
Upvotes: 0
Reputation: 982
Guy I work with pointed out there's an easier way of doing this.
In your window's xaml
<Border x:Name="DragBar"
Height="100"
MouseLeftButtonDown="DragBar_MouseLeftButtonDown" />
Code behind
private void DragBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
Done.
Upvotes: 4