DrawFocusRect过程的功能就是在指定的矩形周围绘制虚框,我们先在标准模块的声明部分声明一个自定义的矩形结构:
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
然后再声明这个过程:
Declare Function DrawFocusRect Lib "User32" Alias _
"DrawFocusRect" (ByVal hdc As Long, _
lpRect As RECT) As Long
最后,在程序中调用这个过程就行了。这个过程有两个特点:1. 在调用的时候,图片框的坐标的度量单位为像素,即 Picture1.ScaleMode = 3;2. 这个函数用XOR(异或)画图,即第二次以同样的参数调用它,就会将第一次画的虚线框擦掉。下面是一个简单的例子,我们将在一个表单上的图片框中实现这个功能:
首先,在表单的公共声明部分声明三个变量:Dim MouseRect As RECT,Dim x1,y1 As Integer。(其中x1,y1 用来记录鼠标按下时的坐标值)
Private Sub Form_Load ()
MouseRect.Left = 0 '初始化虚线框
MouseRect.Top = 0
MouseRect.Right = 0
MouseRect.Bottom = 0
End Sub
然后,编写Picture.MouseDown, Picture.MouseMove子程序。
Private Sub Picture1_ MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button And 1 Then '按下的是鼠标左键
Picture1.ScaleMode = 3
DrawFocusRect Picture1.hdc, MouseRec '清除上次画的虚线框t
MouseRect.Left = X
MouseRect.Top = Y
MouseRect.Right = X
MouseRect.Bottom = Y
X1=X
Y1=Y
End If
End Sub
Private Sub Picture1_ MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.ScaleMode = 3
If Button And 1 Then
DrawFocusRect Picture1.hdc, MouseRect '清除上次画的虚线框
If X > x1 Then
MouseRect.Left = x1
MouseRect.Right = X
Else
MouseRect.Left = X
MouseRect.Right = x1
End If
If Y > y1 Then
MouseRect.Top = y1
MouseRect.Bottom = Y
Else
MouseRect.Top = Y
MouseRect.Bottom = y1
End If
DrawFocusRect Picture1.hdc, MouseRect
'画虚线框
End IfEnd Sub
我们还可以设定x2, y2作为公共变量,在Picture.MouseUP子程序中记录矩形框另一个角的位置,这样就能够对选中的范围进行各种操作了。
此程序在586机器,Visual Basic 5.0环境下通过。
(上海 裘键)
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/