Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub ReleaseCapture Lib "user32" ()
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Dim ileft As Long
Dim iTop As Long
Dim ctlRect As RECT
'决定显示下拉框的显示位置
GetWindowRect UserControl.hwnd, ctlRect '获取控件的矩形区域
If ctlRect.Bottom + (grd.Height / Screen.TwipsPerPixelX) > Screen.Height / Screen.TwipsPerPixelY Then
'置前
iTop = (ctlRect.Top - (grd.Height / Screen.TwipsPerPixelY)) * Screen.TwipsPerPixelY
Else
'置后
iTop = ctlRect.Bottom * Screen.TwipsPerPixelY
End If
'If the popup window is as wide as, or wider than the control, we want to align
'it to the left edge of the control. Otherwise, we align it to the right. If
'we're too far to the right, we push it back left.
If (ctlRect.Right - ctlRect.Left) > grd.Width / Screen.TwipsPerPixelX Then
'try to align to the right of the control
If ctlRect.Right > Screen.Width / Screen.TwipsPerPixelX Then
ileft = Screen.Width - grd.Width
Else
ileft = ctlRect.Right * Screen.TwipsPerPixelX - grd.Width
End If
'Check we haven't gone outside the left edge of the screen
If ileft < 0 Then ileft = 0
Else
'try to align to the left
If ctlRect.Left < 0 Then
ileft = 0
Else
ileft = ctlRect.Left * Screen.TwipsPerPixelX
End If
'Check we haven't gone outside the left edge of the screen
If ileft + grd.Width > Screen.Width Then ileft = Screen.Width - grd.Width
End If
With grd
.Top = iTop
.Left = ileft
.Visible = True
.ZOrder
End With
DoEvents
SetCapture grd.hwnd
End Sub
Private Sub HidePopup()
'隐藏下拉框
If GetCapture = grd.hwnd Then
ReleaseCapture
End If
grd.Visible = False
DoEvents