前面的尺寸單位中介紹過Window對象的PointsToScreenPixelsX和PointsToScreenPixelsY方法,這兩個方法對精確控制鼠標光標在Excel中的位置很有幫助。這個例子示范控制鼠標光標到指定的單元格或控件中心,并可模擬鼠標單擊控件如按鈕的功能,光標的位置是以像素為單位,而控件或單元格在Excel中的位置是以Point為單位,所以需要用到前面的換算自定義函數。
Public Type POINTAPI x As Long y As Long End Type ' 設置鼠標光標位置 Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long ' 鼠標動作模擬 Public Const MOUSEEVENTF_LEFTDOWN = &H2 Public Const MOUSEEVENTF_LEFTUP = &H4 Public Declare Sub mouse_event Lib "user32" ( _ ByVal dwFlags As Long, _ ByVal dx As Long, _ ByVal dy As Long, _ ByVal cButtons As Long, _ ByVal dwExtraInfo As Long) ' 移動鼠標光標到指定的單元格中心,iRow和iCol分別為指定單元格行和列 Sub SetCursorToCell(iRow As Integer, iCol As Integer) Dim pt As POINTAPI Dim iTRow As Integer Dim iTCol As Integer ' 當前窗口可見范圍的總行數和列數 iTRow = ActiveWindow.VisibleRange.Rows.Count iTCol = ActiveWindow.VisibleRange.Columns.Count ' 如果指定單元格在可見范圍之外,則選擇該單元格讓其在可見范圍中顯示 If iRow < ActiveWindow.VisibleRange.Row Or iRow > ActiveWindow.VisibleRange.Row + iTRow - 1 Or _ iCol < ActiveWindow.VisibleRange.Column Or iCol > ActiveWindow.VisibleRange.Column + iTCol - 1 Then Cells(iRow, iCol).Select End If ' 指定單元格中心在屏幕坐標中X軸的位置,以像素為單位 pt.x = ActiveWindow.PointsToScreenPixelsX(0) + _ Point2PixelX(Cells(iRow, iCol).Left + Cells(iRow, iCol).Width / 2) ' 指定單元格中心在屏幕坐標中Y軸的位置,以像素為單位 pt.y = ActiveWindow.PointsToScreenPixelsY(0) + _ Point2PixelX(Cells(iRow, iCol).Top + Cells(iRow, iCol).Height / 2) ' 設置鼠標光標到指定位置 SetCursorPos pt.x, pt.y End Sub ' 移動鼠標光標到指定的控件中心,vControl為指定空間 Sub SetCursorToControl(vControl As Variant) Dim pt As POINTAPI ' 如果控件顯示在可見范圍之外,選擇控件覆蓋的單元格使其可見 With ActiveWindow.VisibleRange If vControl.Left + vControl.Width > .Left + .Width Or vControl.Top + vControl.Height > .Top + .Height Then vControl.BottomRightCell.Select End If If vControl.Left < .Left Or vControl.Top < .Top Then vControl.TopLeftCell.Select End If End With ' 指定控件中心在屏幕中標中X軸的位置,以像素為單位 pt.x = ActiveWindow.PointsToScreenPixelsX(0) + Point2PixelX(vControl.Left + vControl.Width / 2) ' 指定控件中心在屏幕中標中Y軸的位置,以像素為單位 pt.y = ActiveWindow.PointsToScreenPixelsY(0) + Point2PixelY(vControl.Top + vControl.Height / 2) ' 設置鼠標光標到指定位置 SetCursorPos pt.x, pt.y End Sub Sub ClickControl(iControl As Variant) ' 設置光標到指定控件 SetCursorToControl iControl ' 模擬鼠標單擊的動作 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0&, 0& mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0&, 0& End Sub ' 移動鼠標光標到控件CommandButton1 Sub test1() SetCursorToControl Sheet1.CommandButton1 End Sub ' 模擬單擊控件CommandButton1 Sub test2() ClickControl Sheet1.CommandButton1 End Sub ' 移動鼠標光標到單元格B2 Sub test3() SetCursorToCell 2, 2 End Sub
另外Excel窗體是以屏幕坐標位置顯示的,但單位仍然是Point,如果需要在指定位置顯示窗體,例如在所選擇的單元格右側,同樣要用到尺寸轉換的自定義函數和Window對象的PointsToScreenPixelsX和PointsToScreenPixelsY方法。
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' 如果窗體UserForm1沒有顯示,則顯示它 If UserForm1.Visible = False Then UserForm1.Show 0 End If ' 移動窗體到選擇的單元格右側位置 UserForm1.Move Pixel2PointX(ActiveWindow.PointsToScreenPixelsX(0)) + Target.Left + Target.Width, _ Pixel2PointY(ActiveWindow.PointsToScreenPixelsY(0)) + Target.Top End Sub