VB:如何?在代碼中彈出toolbar的ButtonMenu

來源:互聯網
上載者:User

      首先,需要指出的是toolbar的ButtonMenu不是標準的menu,所以用TrackPopupMenu之類的api函數無法達到我們的目的,事實上當使用者點擊dropdown button時,toolbar會發送TBN_DROPDOWN這條通知訊息,所以我們只要用代碼類比發送這條通知訊息即可。代碼如下:

Option Explicit
Private Type TBBUTTON
   iBitmap As Long
   idCommand As Long
   fsState As Byte
   fsStyle As Byte
   bReserved1 As Byte
   bReserved2 As Byte
   dwData As Long
   iString As Long
End Type
Private Type NMHDR
   hwndFrom As Long
   idfrom As Long
   code As Long
End Type

Private Type NMTOOLBAR
    hdr As NMHDR
    iItem As Long
    tbBtn As TBBUTTON
    cchText As Long
    lpszString As Long
End Type
Private Const TBN_FIRST = -700&
Private Const TBN_DROPDOWN = (TBN_FIRST - 10)

Private Const WM_USER = &H400
Private Const TB_GETBUTTON As Long = (WM_USER + 23)
Private Const WM_NOTIFY As Long = &H4E&
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
    If Button.Index = 4 Then '假如toolbar上的第4個按鈕有buttonmenu,同時其樣式為5
       showbuttonmenu Toolbar1, Button.Index - 1
    End If
End Sub
Private Sub showbuttonmenu(Toolbar1 As MSComctlLib.Toolbar, ByVal Buttonindex As Long) 'buttonindex從0開始
    Dim tButton As TBBUTTON
    Dim tNotify As NMTOOLBAR
    Dim lResult As Long
    Dim mhwnd As Long
    Dim lCommandId As Long
    mhwnd = FindWindowEx(Toolbar1.hwnd, 0, "msvb_lib_toolbar", vbNullString)
    lResult = SendMessage(mhwnd, TB_GETBUTTON, Buttonindex, tButton)
    lCommandId = tButton.idCommand
    With tNotify
        .hdr.code = TBN_DROPDOWN
        .hdr.hwndFrom = mhwnd
        .iItem = lCommandId
    End With
    lResult = SendMessage(Toolbar1.hwnd, WM_NOTIFY, 0, tNotify)
End Sub

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.