|
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72
Dim PointsPerPixel As Single
Sub DrawShape()
Dim oShape As Shape
Set oShape = DrawCenterShape(msoShapeRectangle, 150, 150)
End Sub
Function DrawCenterShape(ShapeType As MsoAutoShapeType, lWidth As Long, lHeight As Long) As Shape
If PointsPerPixel = 0 Then PointsPerPixel = GetPixelSize
Dim ScreenWidth As Integer, ScreenHeight As Integer
Dim ScreenTop As Integer, ScreenLeft As Integer
Dim lLeft As Integer, lTop As Integer
Dim lLeftWorkarea As Long, lTopWorkarea As Long
Dim hwnd As Long
Dim oRect As RECT
hwnd = FindWindowEx(0, 0, "XLMAIN", Application.Caption)
hwnd = FindWindowEx(hwnd, 0, "XLDESK", vbNullString)
GetWindowRect hwnd, oRect
ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
lLeft = ((ScreenWidth - lWidth) / 2 - oRect.Left - 42) * PointsPerPixel + (ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left - ActiveWindow.Left)
lTop = ((ScreenHeight - lHeight) / 2 - oRect.Top - 51) * PointsPerPixel + (ActiveSheet.Rows(ActiveWindow.ScrollRow).Top - ActiveWindow.Top)
Set DrawCenterShape = ActiveSheet.Shapes.AddShape(ShapeType, lLeft, lTop, lWidth, lHeight)
End Function
Function GetPixelSize() As Single
Dim hDC As Long
hDC = GetDC(0)
GetPixelSize = POINTS_PER_INCH / GetDeviceCaps(hDC, LOGPIXELSX)
ReleaseDC 0, hDC
End Function
[ 本帖最后由 小fisher 于 2009-2-25 16:49 编辑 ] |
-
|