前段时间在打造自己印前工具箱,懒得设计UI界面,在工具栏里一个功能添加一个图标。随着功能原来越多,和增加第三方功能,逼着要制作软件图形界面了,以前 C/C++
写的工具就尝试过,所以就按以前的土办法,使用一张图片来当界面了。
1. 先用PS制作一张排好图标的图片,如图
2. 把图片导入到VBA编辑器的窗口中,命名UI
,双击图片添加两行代码
Private Sub UI_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgBox "获取图标坐标: " & X & " , " & Y
End Sub
3. 测试这个UI程序雏形,运行后点击各个图标,获得图标坐标
4. 由于VBA数据结构没有 C/C++
方便,所以使用两个数组,分别存5列X左右,3行Y坐标
' 定义图标坐标pos
Dim pos_x As Variant
Dim pos_Y As Variant
pos_x = Array(32, 110, 186, 265, 345)
pos_Y = Array(50, 135, 215)
5. 使用点击坐标和存储的坐标差值30以内来确定是否点击到图标
最后完成代码如下
Private Sub UI_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' 定义图标坐标pos Dim pos_x As Variant Dim pos_Y As Variant pos_x = Array(32, 110, 186, 265, 345) pos_Y = Array(50, 135, 215) If Abs(X - pos_x(0)) < 30 And Abs(Y - pos_Y(0)) < 30 Then 物件角线 ElseIf Abs(X - pos_x(1)) < 30 And Abs(Y - pos_Y(0)) < 30 Then 绘制矩形 ElseIf Abs(X - pos_x(2)) < 30 And Abs(Y - pos_Y(0)) < 30 Then 角线爬虫 ElseIf Abs(X - pos_x(3)) < 30 And Abs(Y - pos_Y(0)) < 30 Then 矩形拼版 ElseIf Abs(X - pos_x(4)) < 30 And Abs(Y - pos_Y(0)) < 30 Then 拼版角线 End If If Abs(X - pos_x(0)) < 30 And Abs(Y - pos_Y(1)) < 30 Then 居中页面 ElseIf Abs(X - pos_x(1)) < 30 And Abs(Y - pos_Y(1)) < 30 Then 拼版标记 ElseIf Abs(X - pos_x(2)) < 30 And Abs(Y - pos_Y(1)) < 30 Then 智能群组 ElseIf Abs(X - pos_x(3)) < 30 And Abs(Y - pos_Y(1)) < 30 Then CQL选择 ElseIf Abs(X - pos_x(4)) < 30 And Abs(Y - pos_Y(1)) < 30 Then 批量替换 End If If Abs(X - pos_x(0)) < 30 And Abs(Y - pos_Y(2)) < 30 Then 尺寸取整 ElseIf Abs(X - pos_x(1)) < 30 And Abs(Y - pos_Y(2)) < 30 Then Dim r As Long ElseIf Abs(X - pos_x(2)) < 30 And Abs(Y - pos_Y(2)) < 30 Then WebHelp ElseIf Abs(X - pos_x(3)) < 30 And Abs(Y - pos_Y(2)) < 30 Then WebHelp ElseIf Abs(X - pos_x(4)) < 30 And Abs(Y - pos_Y(2)) < 30 Then WebHelp End If End Sub