Drag a WPF window around the desktop with a button - c#

I have this code for dragging my Window with its MouseDown Event.
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
But I want to do this using a Button, because my form is transparent. And Using the same function for that button's MouseDown event will not work.
How can I achieve this?

You have to use PreviewMouseDown instead of MouseDown event.
And do not forget to mark the event "Handled" in the end.
XAML code:
<Button x:Name="Button_Move" PreviewMouseDown="Window_Main_MouseDown"/>
C# code:
private void Window_Main_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
{
this.DragMove();
e.Handled = true;
}
}

Use border instead of Button. Because DragMove only work on PrimaryMouseButton event. Not work on Click event
XAML
<Border Background="Blue" MouseLeftButtonDown="Border_MouseLeftButtonDown">
</Border>
CODE
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}

My Finally Code was here:
private Point startPoint;
private void btnChat_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(btnChat);
}
private void btnChat_PreviewMouseMove(object sender, MouseEventArgs e)
{
var currentPoint = e.GetPosition(btnChat);
if (e.LeftButton == MouseButtonState.Pressed &&
btnChat.IsMouseCaptured &&
(Math.Abs(currentPoint.X - startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(currentPoint.Y - startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance))
{
// Prevent Click from firing
btnChat.ReleaseMouseCapture();
DragMove();
}
}

Related

Mouse right in textbox

I everyone,
I have development this application, ... my objective is user, if click on right side of the mouse the url is paste to textbox.
MenuStrip
But when i click in right side, appear the menustrip of textbox. My question is I can disable this "menustrip"?!?
I send my code at the moment:
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
This should work for your code:
private MouseButtons e_Button = new MouseButtons();
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
e_Button = e.Button;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
private void cms_Opening(object sender, CancelEventArgs e)
{
if (e_Button == System.Windows.Forms.MouseButtons.Right)
e.Cancel = true;
}

Button.MouseMove event in WPF always has e.LeftMouse with status Released

I am trying to start a drag and drop on a Button control in WPF. I am using Button because I also want to handle click event.
Adding a Button in XAML and handling the MouseMove event always has e.LeftMouse equal to MouseButtonState.Released.
<Button MouseMove="Button_MouseMove"/>
In the following handler implementation the exception is never thrown.
private void Button_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
throw new Exception("It works!");
}
I noticed the same is true for any control I place inside a Button and try to process MouseMove event.
How do I handle drag and drop from a Button control or any other control inside a button in WPF?
EDIT - Solution based on mm8's answer
The updated XAML is:
<Button PreviewMouseDown="Button_PreviewMouseDown"
PreviewMouseUp="Button_PreviewMouseUp"
PreviewMouseMove="Button_PreviewMouseMove">
</Button>
The updated handler code:
Point startPosition;
double delta = 10;
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
startPosition = e.GetPosition(this);
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Point currentPosition = e.GetPosition(this);
if ((currentPosition - startPosition).Length < delta)
throw new Exception("MouseClick");
}
private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition = e.GetPosition(this);
double currentDelta = (currentPosition - startPosition).Length;
if (e.LeftButton == MouseButtonState.Pressed && currentDelta >= delta)
throw new Exception("DragAndDrop");
}
You could perhaps handle the PreviewMouseDown event instead of Click:
private void Button_Click(object sender, MouseButtonEventArgs e)
{
//handle click here...
e.Handled = true;
}
XAML:
<Button PreviewMouseDown="Button_Click" MouseMove="Button_MouseMove"/>

Trigger mouseup event on keyup - WPF

My question is simple. Is it possible to trigger mouseup and mousedown on keyup and keydown event respectivey in WPF.
I have set of event handlers for mouse down and mouse up in my custom control. I want to trigger the same if the user try to press the spacebar on my custom button.
private void CustomButton_MouseDown(object sender, MouseButtonEventArgs e)
{
eventlabel.Content = "Mousedown event Triggered";
}
private void CustomButton_MouseUp(object sender, MouseButtonEventArgs e)
{
eventlabel.Content = "MouseUp event Triggered";
}
private void CustomButton_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
//Need to trigger all the handlers of mousedown
}
}
private void CustomButton_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
//Need to trigger all the handlers of mouseup
}
}
You can raise Events in all UIElements with RaiseEvent(EventArgs). The MouseButtonArgsEventsArgs can be used as followed in the KeyDown-EventHandler:
MouseDevice mouseDevice = Mouse.PrimaryDevice;
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, 0, MouseButton.Left);
mouseButtonEventArgs.RoutedEvent = Mouse.MouseDownEvent;
mouseButtonEventArgs.Source = this;
RaiseEvent(mouseButtonEventArgs);
(modified example from https://social.msdn.microsoft.com/Forums/vstudio/de-DE/7ec6b75b-b808-48ca-a880-fafa9025da6e/how-to-raise-a-click-event-on-an-uielement?forum=wpf)
Answer mentioned by Tobias works for me. But, it need some tweak.
elementName.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseDownEvent,
Source = this,
});
Here is the Reference

Exit while loop of mouseDown event

I am trying to make my Form, which has no border, moveable with holding the left mousebutton down and exit the while loop, when releasing the mousebutton.
But the code I have right now doesn't exit the loop on release.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
while (mouseDown)
{
mouseX = MousePosition.X;
mouseY = MousePosition.Y - 30;
this.SetDesktopLocation(mouseX, mouseY);
if (e.Button != MouseButtons.Left)
mouseDown = false;
}
I also tried to add a mouseUp event but it cant happen as long as mouseDown is active.
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = true;
}
OK, I fixed it for myself.
I just did this:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
mouseX = MousePosition.X - 20;
mouseY = MousePosition.Y - 40;
this.SetDesktopLocation(mouseX, mouseY);
}
}
By using a loop in the on mouse up event you are locking the thread. You could use the MouseMove event with a public variable to check if the mouse is down.

Double click to Windows form in C#

How can I detect, which mouse button have double clicked the form i.e. Left, Right or Middle?
Updated:
I am using .NET2.0
Store the last clicked button in MouseUp event and then check that in the double click event. Sample code:
MouseButtons _lastButtonUp = MouseButtons.None;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_lastButtonUp = e.Button;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
switch (_lastButtonUp)
{
case System.Windows.Forms.MouseButtons.Left:
MessageBox.Show("left double click");
break;
case System.Windows.Forms.MouseButtons.Right:
MessageBox.Show("right double click");
break;
case System.Windows.Forms.MouseButtons.Middle:
MessageBox.Show("middle double click");
break;
}
}
Have a look at MouseDoubleClick and MouseEventArgs and MouseButtons Enumeration
MouseDoubleClick is one of the Form events.
In Whatever_Click or DoubleClick event, you can check the MouseEventArgs e, which contains what key was pressed.
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Console.WriteLine("Left Mouse Button was clicked!");
else if (e.Button == MouseButtons.Middle)
Console.WriteLine("Middle Mouse Button was clicked!");
}
Other buttons include MouseButtons.Right, MouseButtons.Left
in form_MouseDoubleClick event you can trace
void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Do Operation
}
}

Categories