Option Explicit
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY As Long = 90
Private Const POINTS_PER_INCH As Long = 72
Private Type POINTAPI
x As Long
y As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, ByVal hdc As LongPtr) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, ByVal nIndex As Long) As Long
#Else
Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long
Private Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hdc As Long, ByVal nIndex As Long) As Long
#End If
Sub DinhViFormTaiChuot(ByVal Form As Object)
#If VBA7 Then
Dim DC As LongPtr
#Else
Dim DC As Long
#End If
Dim PixelsPerPointsX As Double, PixelsPerPointsY As Double, pt As POINTAPI
DC = GetDC(0)
PixelsPerPointsX = GetDeviceCaps(DC, LOGPIXELSX) / POINTS_PER_INCH
PixelsPerPointsY = GetDeviceCaps(DC, LOGPIXELSY) / POINTS_PER_INCH
ReleaseDC 0, DC
GetCursorPos pt
Form.Left = pt.x / PixelsPerPointsX
Form.Top = pt.y / PixelsPerPointsY
End Sub
Sub DinhViFormTaiCell(ByVal Form As Object, ByVal tlCell As Range, ByVal showFullForm As Boolean)
' dinh vi Form sao cho goc tren ben trai cua Form trung voi o tlCell.
' neu showFullForm = True thi khi can thiet thi cua so se duoc cuon de hien thi toan bo Form
' neu showFullForm = False thi tham chi khi Form bi che khuat thi cua so khong duoc cuon.
#If VBA7 Then
Dim DC As LongPtr
#Else
Dim DC As Long
#End If
Dim l As Double, t As Double, r As Long, c As Long, rng As Range
Dim PixelsPerPointsX As Double, PixelsPerPointsY As Double
ActiveWindow.Zoom = 100
If showFullForm Then
l = tlCell.Left + Form.Width
t = tlCell.Top + Form.Height
Set rng = ActiveWindow.VisibleRange(ActiveWindow.VisibleRange.Count)
Do While tlCell.Offset(0, c).Left < l
If tlCell.Offset(0, c).Left < l Then c = c + 1
Loop
If Intersect(tlCell.Offset(0, c), ActiveWindow.VisibleRange) Is Nothing Then
ActiveWindow.SmallScroll ToRight:=tlCell.Offset(0, c).Column - rng.Column
End If
Do While tlCell.Offset(r).Top < t
If tlCell.Offset(r).Top < t Then r = r + 1
Loop
If Intersect(tlCell.Offset(r), ActiveWindow.VisibleRange) Is Nothing Then
ActiveWindow.SmallScroll Down:=tlCell.Offset(r).Row - rng.Row
End If
End If
DC = GetDC(0)
PixelsPerPointsX = GetDeviceCaps(DC, LOGPIXELSX) / POINTS_PER_INCH
PixelsPerPointsY = GetDeviceCaps(DC, LOGPIXELSY) / POINTS_PER_INCH
ReleaseDC 0, DC
Form.StartUpPosition = 0
Form.Left = ActiveWindow.PointsToScreenPixelsX(tlCell.Left * PixelsPerPointsX) / PixelsPerPointsX
Form.Top = ActiveWindow.PointsToScreenPixelsY(tlCell.Top * PixelsPerPointsY) / PixelsPerPointsY
End Sub