前面的尺寸單位中介紹過Window對象的PointsToScreenPixelsX和PointsToScreenPixelsY方法,這兩個方法對精確控制鼠標(biāo)光標(biāo)在Excel中的位置很有幫助。這個例子示范控制鼠標(biāo)光標(biāo)到指定的單元格或控件中心,并可模擬鼠標(biāo)單擊控件如按鈕的功能,光標(biāo)的位置是以像素為單位,而控件或單元格在Excel中的位置是以Point為單位,所以需要用到前面的換算自定義函數(shù)。
Public Type POINTAPIx As Longy As LongEnd Type' 設(shè)置鼠標(biāo)光標(biāo)位置Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long' 鼠標(biāo)動作模擬Public Const MOUSEEVENTF_LEFTDOWN = &H2Public Const MOUSEEVENTF_LEFTUP = &H4Public 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)' 移動鼠標(biāo)光標(biāo)到指定的單元格中心,iRow和iCol分別為指定單元格行和列Sub SetCursorToCell(iRow As Integer, iCol As Integer)Dim pt As POINTAPIDim iTRow As IntegerDim iTCol As Integer' 當(dāng)前窗口可見范圍的總行數(shù)和列數(shù) iTRow = ActiveWindow.VisibleRange.Rows.CountiTCol = 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 ThenCells(iRow, iCol).SelectEnd If' 指定單元格中心在屏幕坐標(biāo)中X軸的位置,以像素為單位 pt.x = ActiveWindow.PointsToScreenPixelsX(0) + _Point2PixelX(Cells(iRow, iCol).Left + Cells(iRow, iCol).Width / 2)' 指定單元格中心在屏幕坐標(biāo)中Y軸的位置,以像素為單位 pt.y = ActiveWindow.PointsToScreenPixelsY(0) + _Point2PixelX(Cells(iRow, iCol).Top + Cells(iRow, iCol).Height / 2)' 設(shè)置鼠標(biāo)光標(biāo)到指定位置 SetCursorPos pt.x, pt.yEnd Sub' 移動鼠標(biāo)光標(biāo)到指定的控件中心,vControl為指定空間Sub SetCursorToControl(vControl As Variant)Dim pt As POINTAPI' 如果控件顯示在可見范圍之外,選擇控件覆蓋的單元格使其可見 With ActiveWindow.VisibleRangeIf vControl.Left + vControl.Width > .Left + .Width Or vControl.Top + vControl.Height > .Top + .Height ThenvControl.BottomRightCell.SelectEnd IfIf vControl.Left < .Left Or vControl.Top < .Top ThenvControl.TopLeftCell.SelectEnd IfEnd With' 指定控件中心在屏幕中標(biāo)中X軸的位置,以像素為單位 pt.x = ActiveWindow.PointsToScreenPixelsX(0) + Point2PixelX(vControl.Left + vControl.Width / 2)' 指定控件中心在屏幕中標(biāo)中Y軸的位置,以像素為單位 pt.y = ActiveWindow.PointsToScreenPixelsY(0) + Point2PixelY(vControl.Top + vControl.Height / 2)' 設(shè)置鼠標(biāo)光標(biāo)到指定位置 SetCursorPos pt.x, pt.yEnd SubSub ClickControl(iControl As Variant)' 設(shè)置光標(biāo)到指定控件 SetCursorToControl iControl' 模擬鼠標(biāo)單擊的動作 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0&, 0&mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0&, 0&End Sub' 移動鼠標(biāo)光標(biāo)到控件CommandButton1Sub test1()SetCursorToControl Sheet1.CommandButton1End Sub' 模擬單擊控件CommandButton1Sub test2()ClickControl Sheet1.CommandButton1End Sub' 移動鼠標(biāo)光標(biāo)到單元格B2Sub test3()SetCursorToCell 2, 2End Sub
另外Excel窗體是以屏幕坐標(biāo)位置顯示的,但單位仍然是Point,如果需要在指定位置顯示窗體,例如在所選擇的單元格右側(cè),同樣要用到尺寸轉(zhuǎn)換的自定義函數(shù)和Window對象的PointsToScreenPixelsX和PointsToScreenPixelsY方法。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)' 如果窗體UserForm1沒有顯示,則顯示它 If UserForm1.Visible = False ThenUserForm1.Show 0End If' 移動窗體到選擇的單元格右側(cè)位置 UserForm1.Move Pixel2PointX(ActiveWindow.PointsToScreenPixelsX(0)) + Target.Left + Target.Width, _Pixel2PointY(ActiveWindow.PointsToScreenPixelsY(0)) + Target.TopEnd Sub
