What event is raised when the mouse is moved while a button is pressed?
I need an event that doesn't exist in the .NET Framework's standard events. For example, mouse move while the left mouse button is down.
I also need to change some behaviors. For example, I have some buttons and I want to change the background image of each one that cursor is on while the left mouse button is down, but wh开发者_如何学Cen I click on one button and hold down the left mouse button, when I move the mouse the other buttons will not raise any events.
What should I do? How can I create new events? How can I change behaviors?
Any help is appreciated.
Your problem is that when the MouseDown event occurs on a button, that button 'captures' the mouse and doesn't release it until the button is released, which means that the MouseMove events are not received by the other buttons.
There's some code from here which may help:
// Assuming all buttons subscribe to this event:
private void buttons_MouseMove (object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Control control = (Control) sender;
if (control.Capture)
{
control.Capture = false;
}
if (control.ClientRectangle.Contains (e.Location))
{
Control.BackgroundImage = ...;
}
}
}
The MouseMove-Event has an Property called 'Button' which tells you, which button is pressed. So all you have to do is something like this:
void panel1_MouseMove(Object sender, System::Windows::Forms::MouseEventArgs e) {
if(e.Button = MouseButtons.Left){
//Do what you want when mouse_move with left button pressed
}
}
Above code isn't tested, and I haven't looked up the correct spelling etc. of the Properties, just give it a little try in IntelliSense / MSDN.
You can find even more Information in the MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove%28v=vs.71%29.aspx
For the second question, just try finding a fitting event or overwrite the WindowProc-Event-Function there you can listen to the WindowMessages and fetch what you need - more information is given on http://msdn.microsoft.com/en-us/library/ms633573%28v=VS.85%29.aspx and on http://www.pinvoke.net
This example (one form with 2 textboxes) moves the text in a textbox to another textbox by clicking left on one and releasing the mouse on the other textbox. If you comment out the capture part, it stops working.
Public Class Form1
Dim MousebuttonIsPressed As Boolean = False
Dim TextboxWhereMouseDown As TextBox
Dim TextboxWhereMouseUp As TextBox
Private Sub Form1_mouseup(sender As Object, e As EventArgs) Handles MyBase.MouseUp
MousebuttonIsPressed = False
End Sub
Private Sub Textbox_MouseDown(sender As TextBox, e As MouseEventArgs) Handles TextboxOne.MouseDown, TextboxTwo.MouseDown ' etc for each textbox involved
TextboxWhereMouseDown = sender
If e.Button = MouseButtons.Left Then
MousebuttonIsPressed = True
End If
End Sub
Private Sub Textbox_MouseUp(sender As TextBox, e As MouseEventArgs) Handles TextboxOne.MouseUp, TextboxTwo.MouseUp ' etc.
TextboxWhereMouseUp = sender
If Not (TextboxWhereMouseDown Is TextboxWhereMouseUp) Then
TextboxWhereMouseUp.Text = TextboxWhereMouseDown.Text
TextboxWhereMouseDown.Text = ""
End If
MousebuttonIsPressed = False
End Sub
Private Sub Textbox_MouseEnter(sender As TextBox, e As EventArgs) Handles TextboxOne.MouseEnter, TextboxTwo.MouseEnter ' etc
If MousebuttonIsPressed Then
TextboxWhereMouseUp = sender
End If
End Sub
Private Sub Textbox_MouseMove(sender As TextBox, e As MouseEventArgs) Handles TextboxOne.MouseMove, TextboxTwo.MouseMove ' etc
If (e.Button = System.Windows.Forms.MouseButtons.Left) Then
Dim Contrl As Control = sender
If Contrl.Capture Then
If Not Contrl.ClientRectangle.Contains(e.Location) Then ' to allow selection of text
Contrl.Capture = False
End If
End If
End If
End Sub
End Class
精彩评论