- 积分
- 5966
- 明经币
- 个
- 注册时间
- 2003-1-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
下面的语句能在用户单击鼠标右键时弹出一个快捷菜单,但是位置不正确。
窗体中的代码:
Option Explicit
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim hPopupMenu As Long
Dim lResult As Long
Dim xScale As Double
Dim yScale As Double
xScale = GetDeviceCaps(GetWindowDC(GetActiveWindow()), LOGPIXELSX)
yScale = GetDeviceCaps(GetWindowDC(GetActiveWindow()), LOGPIXELSY)
If Button = 1 Then Exit Sub
hPopupMenu = CreatePopupMenu()
lResult = AppendMenu(hPopupMenu, MF_STRING, IDM_NEW, "&File")
lResult = AppendMenu(hPopupMenu, MF_STRING, IDM_OPEN, "&Open")
lResult = TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN Or TPM_LEFTBUTTON, (Me.Left + X) * 1440 / xScale, (Me.Top + Y) * 1440 / yScale, 0&, GetActiveWindow, vbNull)
End Sub
标准模块中的代码:
Public Declare Function CreatePopupMenu Lib "user32" () As Long
Public Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Public Declare Function AppendMenu Lib "user32" _
Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, _
ByVal lpNewItem As Any) As Long
Public Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, _
ByVal X As Long, ByVal Y As Long, ByVal nReserved As Long, _
ByVal hwnd As Long, lprc As Any) As Long ' 最后一个参数类型改为Any(从RECT)
' Flags for TrackPopupMenu
Public Const TPM_CENTERALIGN = &H4&
Public Const TPM_LEFTALIGN = &H0&
Public Const TPM_LEFTBUTTON = &H0&
Public Const TPM_RIGHTALIGN = &H8&
Public Const TPM_RIGHTBUTTON = &H2&
Public Const WM_COMMAND = &H111
Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Public Const MF_STRING = &H0&
Public Const MF_BITMAP = &H4&
Public Const IDM_NEW As Long = 1001
Public Const IDM_OPEN As Long = 1002
请几位版主和各位看一看,能否使弹出菜单显示在正确的位置上?
你也可以下载相关的程序。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|