来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Programmer’s Guide/Part 2: What Can You Do With Visual Basic/Responding to Mouse and Keyboard Events/
1. Responding to Mouse Events
(1) You can use the MouseDown,MouseUp,and MouseMove events to enable your applications to respond to both the location and the state of the mouse.
(2) A form can recognize a mouse event when the pointer is over a part of the form where there are no controls. A control can recognize a mouse event when the pointer is over the control.
(3) When the user holds down a mouse button,the object continues to recognize all mouse events until the user releases the button. This is true even when the pointer is moved off the object.
(4) A bit-field argument returns information in individual bits,each indicating whether a certain condition is on or off.
(5) Using binary notation,the three leftmost bits are referred to as most-significant and the three rightmost bits as least-significant.
(6) Visual Basic doesn't necessarily generate a MouseMove event for every pixel the mouse moves over. The operating environment generates a limited number of mouse messages per second.
(7) Your application can recognize many MouseMove events in quick succession. Therefore,a MouseMove event procedure shouldn't do anything that requires large amounts of computing time.
2. Detecting Mouse Buttons
(2) The button argument uses either a decimal value or an constant to represent these binary patterns.
Binary Value | Decimal Value | Constant | Meaning |
001 | 1 | vbLeftButton | The left button is pressed. |
010 | 2 | vbRightButton | The right button is pressed. |
100 | 4 | vbMiddleButton | The middle button is pressed. |
(3) MouseDown and MouseUp only recognize one button press at a time.
(4) If the user presses more than one button,Visual Basic interprets that action as two or more separate MouseDown events. It sets the bit for the first button pressed,prints the message for that button,and then does the same for the next button.
(5) For the MouseMove event,button indicates the complete state of the mouse buttons — not just which button caused the event,as with MouseDown and MouseUp. To test for whether a particular button is down,use the And operator.
Shift As Integer , X As Single , Y As Single)
If Button And 1 Then Print "You're pressing _
the left button."
If Button And 2 Then Print "You're pressing _
the right button."
End Sub
(6) The mouse and keyboard events use the shift argument to determine whether the SHIFT,CTRL,and ALT keys are pressed and in what,if any,combination. If the SHIFT key is pressed,shift is 1; if the CTRL key is pressed,shift is 2; and if the ALT key is pressed,shift is 4. To determine combinations of these keys,use the total of their values. For example,if SHIFT and ALT are pressed,shift equals 5 (1 + 4).
3. Dragging and Dropping
(1)
Category | Item | Description |
Properties | DragMode | Enables automatic or manual dragging of a control. |
DragIcon | Specifies what icon is displayed when the control is dragged. | |
Events | DragDrop | Recognizes when a control is dropped onto the object. |
DragOver | Recognizes when a control is dragged over the object. | |
Methods | Drag | Starts or stops manual dragging. |
(2) All controls except menus,timers,lines,and shapes support the DragMode and DragIcon properties and the Drag method. Forms recognize the DragDrop and DragOver events,but they don't support the DragMode and DragIcon properties or the Drag method.
(3) Controls can only be dragged when they do not have the focus. To prevent a control from getting the focus,set its TabStop property to False.
4. Dragging and Dropping
(1) When you set dragging to Automatic (set its DragMode property to 1-Automatic),the control being dragged doesn't recognize other mouse events.
(2) When dragging a control,Visual Basic uses a gray outline of the control as the default drag icon. You can substitute other images for the outline by setting the DragIcon property. This property contains a Picture object that corresponds to a graphic image.
(3) Source: The control being dragged; Target: The object onto which the user drops the control.
(4) A control becomes the target if the mouse position is within its borders when the button is released. A form is the target if the pointer is in a blank portion of the form.
5. Controlling When Dragging Starts or Stops
(1) To enable dragging from code,leave DragMode in its default setting (0-Manual). Then use the Drag method whenever you want to begin or stop dragging an object.
(2) The Syntax for the Drag method is as follows:
[object.]Drag action
If action is set to vbBeginDrag,the Drag method initiates dragging of the control. If action is set to vbEndDrag,the control is dropped,causing a DragDrop event. If action is set to vbCancel,the drag is canceled. The effect is similar to giving the value vbEndDrag,except that no DragDrop event occurs.
6. Changing the Position of a Control
(1) You can reposition a control when it is dragged and dropped to any location on the form not occupied by another control.
(2) When the gray rectangle is being used,the user usually wants the control to move precisely into the final position of the gray rectangle. To do this,record the initial mouse position within the source control. Then use this position as an offset when the control is moved.
原文链接:https://www.f2er.com/vb/262987.html