明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2337|回复: 6

[讨论]关于在VBA中创建快捷菜单的问题

[复制链接]
发表于 2003-12-11 16:22:00 | 显示全部楼层 |阅读模式
下面的语句能在用户单击鼠标右键时弹出一个快捷菜单,但是位置不正确。
窗体中的代码:
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
  1. 程序代码内容
复制代码
  1. 程序代码内容
复制代码



请几位版主和各位看一看,能否使弹出菜单显示在正确的位置上?
你也可以下载相关的程序。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2003-12-12 10:51:00 | 显示全部楼层
  1.     lResult = TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN Or TPM_LEFTBUTTON, (Me.Left + X) * 100 / 75, _
  2.                             (Me.Top + Y) * 100 / 75, 0&, GetActiveWindow, vbNull)
复制代码
 楼主| 发表于 2003-12-12 22:13:00 | 显示全部楼层
可是对于不同的显示器,这个比率应该是不固定的吧。
发表于 2005-6-28 10:14:00 | 显示全部楼层
我使用以下语句刚好合适: lResult = TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN Or TPM_LEFTBUTTON, (Me.Left + X + 120), _
(Me.top + Y + 40), 0&, GetActiveWindow, vbNull) 换个机子好象又不行了.
发表于 2005-6-28 10:34:00 | 显示全部楼层
请问如何把我的程序与单击快捷菜单上的条目联系起来呢? 也就是如何利用快捷菜单上的条目来处理我的程序.
发表于 2005-6-28 18:21:00 | 显示全部楼层
'类加入 Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Type POINTAPI
x As Long
y As Long
End Type '窗体加入 Dim Pt As POINTAPI

GetCursorPos Pt
lResult = TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN Or TPM_LEFTBUTTON, _
Pt.x, Pt.y, 0, GetActiveWindow, 0&) '这样完全解决了快捷菜单跟随鼠标的问题.
发表于 2005-7-1 11:17:00 | 显示全部楼层
就是呀,好好玩,不过我从来没用VBA做过菜单呢,包括屏幕菜单,改日我也做做
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-27 12:41 , Processed in 0.179080 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表