2006年3月份,开发PRTool的时候,开始式样书编辑用的是FlexGrid控件。为了使FlexGrid控件的单元格可编辑,用隐藏文本框。但是,文本框和 FlexGrid控件的单元格不在同一坐标系。这样,在 文本框就不能覆盖要编辑的单元格。此时,就需要Twips→Pixels 的转换。当时也是在网站上查到的,在这里发布也许有点占别人功劳的便宜。不过,为了以后自己用的时候方便找到,还是写在这里了。下边是源代码:
Private Declare Function apiGetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Private Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer
Private Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Const DIRECTION_VERTICAL = 1
Private Const DIRECTION_HORIZONTAL = 0
Public Function gFunTwipsToPixels(ByVal Grid As AxMSFlexGridLib.AxMSFlexGrid, ByVal rlngTwips As Long, ByVal rlngDirection As Long) As Long
'
' Twips→Pixelsに変換
'
On Error GoTo Err_gFunTwipsToPixels
Dim lngDeviceHandle As Long
Dim lngPixelsPerInch As Long
lngDeviceHandle = apiGetDC(Grid.hWnd)
If rlngDirection = DIRECTION_HORIZONTAL Then
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSX)
Else
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSY)
End If
lngDeviceHandle = apiReleaseDC(Grid.hWnd, lngDeviceHandle)
gFunTwipsToPixels = rlngTwips / 1440 * lngPixelsPerInch
Exit_gFunTwipsToPixels:
On Error Resume Next
Exit Function
Err_gFunTwipsToPixels:
MsgBox(Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number)
Resume Exit_gFunTwipsToPixels
End Function