重拾VB6(20):Mouse Events, Drag and Drop

来自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

(1)image

(2) The button argument uses either a decimal value or an constant to represent these binary patterns.

Binary ValueDecimal ValueConstantMeaning
0011vbLeftButtonThe left button is pressed.
0102vbRightButtonThe right button is pressed.
1004vbMiddleButtonThe 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.

Private Sub Form_MouseMove ( Button As Integer , _
      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).

image 

3. Dragging and Dropping

(1)

CategoryItemDescription
PropertiesDragModeEnables automatic or manual dragging of a control.
 DragIconSpecifies what icon is displayed when the control is dragged.
EventsDragDropRecognizes when a control is dropped onto the object.
 DragOverRecognizes when a control is dragged over the object.
MethodsDragStarts 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.

 

重拾VB6目录

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值