Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisable a ComboBox's context menu
KeywordsComboBox, right click, popup, context menu
CategoriesControls, API
 
The basic idea is to subclass the ComboBox and ignore the WM_CONTEXTMENU message that makes the context menu appear. Unfortunately the ComboBox itself deosn't process this message, the TextBox that it contains does.

The main program calls the FindEditControl function to find the TextBox contained within the ComboBox. It then subclasses that control.

Note that the program must restore the original WindowProc before exiting or the program will not unload the subclassed control properly and will crash.

 
Private m_EditHWnd As Long

Private Sub Form_Load()
    ' Find the ComboBox's Edit control.
    m_EditHWnd = FindEditChild(cboMenuDisabled.hwnd)

    ' Set the control's new WindowProc.
    OldWindowProc = SetWindowLong( _
        m_EditHWnd, GWL_WNDPROC, _
        AddressOf NoPopupWindowProc)
End Sub

' Restore the original WindowProc.
Private Sub Form_Unload(Cancel As Integer)
    SetWindowLong _
        m_EditHWnd, GWL_WNDPROC, _
        OldWindowProc
End Sub
 
The FindEditControl function calls GetWindow to enumerate a control's children. For each, it uses the ClassName function to get the child's class name.

Function ClassName uses the GetClassName API function to get a window's class name.

 
' Return the hWnd of this control's Edit control child.
Public Function FindEditChild(ByVal hwnd As Long) As Long
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Dim child_hwnd As Long

    child_hwnd = GetWindow(hwnd, GW_CHILD)
    Do While child_hwnd <> 0
        If ClassName(child_hwnd) = "Edit" Then
            FindEditChild = child_hwnd
            Exit Function
        End If

        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
    Loop

    FindEditChild = 0
End Function

' Return this control's class name.
Public Function ClassName(ByVal hwnd As Long) As String
Dim buf As String
Dim buflen As Long

    buflen = 256
    buf = Space$(buflen - 1)
    buflen = GetClassName(hwnd, buf, buflen)
    ClassName = Left$(buf, buflen)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated