用鼠标在控件的标签上左右拖动,就可以改变控件的值,听着就很爽吧.
'首先,写一个标准模块,内容如下:
'声明 API引用
Public Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long '获得"←→"形光标
Public Declare Function LoadCursorByNum Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long '获得"←→"形光标
'函数定义
Public Sub SetControlValue(Button As Integer, X As Single, Optional MinLocked As Long = 0, Optional MaxLocked As Long = 2147483647, Optional LockedShift As Boolean = False, Optional Command As Control = Null)
'作用:控件的值随着鼠标按下左键后左右移动,而在MinLocked~MaxLocked之间改变大小.
'参数: 1.表示按下的键,
' 2.表示鼠标在控件上的X坐标值
' 3.表示,控件可接受的最小值,默认为0.界时,鼠标继续左移,控件值不变
' 4.表示,控件可接受的最大值,默认为2147483647(Long型最大值).界时,鼠标继续右移,控件值不变
' 5.表示控件的Locked属性为True时,是否改变控件的值;为False时不改变.为True 时,改变控件的值.
' 6.表示控件的值改变后,与其有关的命令按钮的Enabled属性是否变为True,如果参数不为空,则参数按钮的Enabled属性将改为 True
On Error Resume Next
Dim ctlCurrentControl As Control, CurrControlName As String
Static PrevControlName As String, theX As Single, IsButtonDown As Boolean
Set ctlCurrentControl = Screen.ActiveControl
CurrControlName = ctlCurrentControl.Name
'确定控件
If CurrControlName <> PrevControlName Then
PrevControlName = CurrControlName
Exit Sub
End If
'确定位置及鼠标:必须按左键;必须在标签上按
If Button = acLeftButton And X < 0 Then IsButtonDown = True
If Button = 0 And X > 0 Then IsButtonDown = False
If Button = 0 Then IsButtonDown = False
'赋值
If IsButtonDown And IIf(LockedShift = False And ctlCurrentControl.Locked = True, False, True) Then
SetCursor LoadCursorByNum(0, 32644)
If X - theX > 10 Then
If ctlCurrentControl = MaxLocked Then Exit Sub 'MaxLocked
ctlCurrentControl = ctlCurrentControl + 1
End If
If X - theX < -10 Then
If ctlCurrentControl = MinLocked Then Exit Sub 'MinLocked
ctlCurrentControl = ctlCurrentControl - 1
End If
If Not Command Is Nothing And Command.Enabled = False Then Command.Enabled = True
Else
SetCursor 1 '恢复正常形状
End If
theX = X
'鼠标在标签上移动时,显示为"←→"形状
If X < 0 And IIf(LockedShift = False And ctlCurrentControl.Locked = True, False, True) Then
SetCursor LoadCursorByNum(0, 32644)
Else
SetCursor 1
End If
Set ctlCurrentControl = Nothing
End Sub
接下来,在要用鼠标操作的控件的"鼠标移动"事件属性中,,选择[事件过程],在过程代码中,加入下面代码:
SetControlValue Button, X
好了,减轻劳动量,轻松工作,源于需要.源于智慧.
(本人通过看到PhotoShop软件的这个特点,写出来的.)
最新修改:
1.加入红尘如烟的建议;
2.加入最小值、最大值的数值界定;
3.修正鼠标拖放控件时,其值还会变的Bug,使其只能在标签上有效。