李晓亮的博客

导航

【转自csdn】 屏蔽textbox控件自身的右键菜单,并显示自己的菜单

方法一:  
  Private   Sub   Text1_MouseDown(Button   As   Integer,   Shift   As   Integer,   X   As   Single,   Y   As   Single)  
        If   Button   =   2   Then  
            Text1.Enabled   =   False  
            Text1.Enabled   =   True  
            PopupMenu   mymenu  
        End   If  
  End   Sub  
   
  方法二:回调函数   
  module:  
  Option   Explicit  
  Public   OldWindowProc   As   Long   '   保存默认的窗口函数的地址  
  Public   Const   WM_CONTEXTMENU   =   &H7B   '   当右击文本框时,产生这条消息  
  Public   Declare   Function   GetWindowLong   Lib   "user32"   Alias   "GetWindowLongA"   (ByVal   hWnd   As   Long,   ByVal   nIndex   As   Long)   As   Long  
  Public   Declare   Function   SetWindowLong   Lib   "user32"   Alias   "SetWindowLongA"   (ByVal   hWnd   As   Long,   ByVal   nIndex   As   Long,   ByVal   dwNewLong   As   Long)   As   Long  
  Private   Declare   Function   CallWindowProc   Lib   "user32"   Alias   "CallWindowProcA"   (ByVal   lpPrevWndFunc   As   Long,   ByVal   hWnd   As   Long,   ByVal   Msg   As   Long,   ByVal   wParam   As   Long,   ByVal   lParam   As   Long)   As   Long  
  Public   Function   SubClass_WndMessage(ByVal   hWnd   As   Long,   ByVal   Msg   As   Long,   ByVal   wp   As   Long,   ByVal   lp   As   Long)   As   Long  
  '   如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理  
    If   Msg   <>   WM_CONTEXTMENU   Then  
        SubClass_WndMessage   =   CallWindowProc(OldWindowProc,   hWnd,   Msg,   wp,   lp)  
        Exit   Function  
    End   If  
    SubClass_WndMessage   =   True  
  End   Function  
  窗体中:  
  Private   Const   GWL_WNDPROC   =   (-4)  
  Private   Sub   Text1_MouseDown(Button   As   Integer,   Shift   As   Integer,   X   As   Single,   Y   As   Single)  
    If   Button   =   1   Then   Exit   Sub  
        OldWindowProc   =   GetWindowLong(Text1.hWnd,   GWL_WNDPROC)   '   取得窗口函数的地址  
              '   用SubClass_WndMessage代替窗口函数处理消息  
        Call   SetWindowLong(Text1.hWnd,   GWL_WNDPROC,   AddressOf   SubClass_WndMessage)  
  End   Sub  
  Private   Sub   Text1_MouseUp(Button   As   Integer,   Shift   As   Integer,   X   As   Single,   Y   As   Single)  
      If   Button   =   1   Then   Exit   Sub  
          '   恢复窗口的默认函数  
          Call   SetWindowLong(Text1.hWnd,   GWL_WNDPROC,   OldWindowProc)  
          PopupMenu   mymenu  
  End   Sub
如果不想弹出任何对话框,把     PopupMenu   mymenu去掉(对第二种方法有效).

posted on 2008-05-03 19:20  LeeXiaoLiang  阅读(479)  评论(0编辑  收藏  举报