Home > Back-end >  Win32 API Disable User Input in Drop Down / Combobox
Win32 API Disable User Input in Drop Down / Combobox

Time:11-19

This sample code I found online has a list dropdown menu, and when I click the text box that the items are displayed in, it doesn't allow me to type anything into the box. This is the behavior I want for my own code. However when I paste in the relevant parts of this code into my own, my program is allowing me to type characters into the Drop Down box. I can't find a single flag in this code that is causing the user input to be disabled, but it only works properly here and not in my code for some reason.

I even tried commenting out certain lines of the code to see if they were responsible for disabling user input, but it is still disabled. Yet in my code, no matter what I do, I can type additional characters into the list box as if it were any other text field. I can't figure out what I'm missing.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE g_hinst;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR lpCmdLine, int nCmdShow) {

    HWND hwnd;
    MSG  msg ;    
    WNDCLASS wc = {0};
    wc.lpszClassName = "Application";
    wc.hInstance     = hInstance ;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc ;
    wc.hCursor       = LoadCursor(0,IDC_ARROW);

    g_hinst = hInstance;
  
    RegisterClass(&wc);
    hwnd = CreateWindow(wc.lpszClassName, "Combo box",
                  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                  100, 100, 270, 170, 0, 0, hInstance, 0);  


    while (GetMessage(&msg, NULL, 0, 0)) {
    
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, 
        WPARAM wParam, LPARAM lParam) {

    static HWND hwndCombo;
  //static HWND hwndStatic;

    const char *items[] = { "FreeBSD", "OpenBSD", 
        "NetBSD", "Solaris", "Arch" };

    switch(msg) {
    
        case WM_CREATE:
        
              hwndCombo = CreateWindow("Combobox", NULL, 
                    WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,
                    10, 10, 120, 200, hwnd, NULL, g_hinst, NULL);   

              // CreateWindowW(L"Button", L"Drop down", 
              //       WS_CHILD | WS_VISIBLE,
              //       150, 10, 90, 25, hwnd, (HMENU) 1, g_hinst, NULL); 

              // hwndStatic = CreateWindowW(L"Static", L"", 
              //       WS_CHILD | WS_VISIBLE,
              //       150, 80, 90, 25, hwnd, NULL, g_hinst, NULL); 

              for (int i = 0; i < 4; i   ) {
              
                  SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
              }

              break;

        case WM_COMMAND:
        
             // if (HIWORD(wParam) == BN_CLICKED) {
             
             //      SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, 0);
             // }
             
             if (HIWORD(wParam) == CBN_SELCHANGE) {      
             
                  LRESULT sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
                 // SetWindowTextW(hwndStatic, items[sel]);
             }
             break;

        case WM_DESTROY:
        
            PostQuitMessage(0);
            break; 
    }
  
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

There is no special handling, no character limit flags, no additional source code or headers, or resource files, nothing...

I've read this code over and over and I just don't understand what is causing this program to present the dropdown items as read-only text.

My code is pretty much identical with just some additional switch statements and functions, but the functionality of the combo box should be the same. Is there some other global flag in a program that could cause user input where it shouldn't be allowed?

CodePudding user response:

Use the CBS_DROPDOWNLIST style on the ComboBox to remove the writable edit control

hwndCombo = CreateWindow("Combobox", NULL, 
                    WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
                    10, 10, 120, 200, hwnd, NULL, g_hinst, NULL);  

Per the documentation:

Combo Box Styles

To create a combo box using the CreateWindow or CreateWindowEx function, specify the COMBOBOX class, appropriate window style constants, and a combination of the following combo box styles.

Constant Description
... ...
CBS_DROPDOWN Similar to CBS_SIMPLE, except that the list box is not displayed unless the user selects an icon next to the edit control.
CBS_DROPDOWNLIST Similar to CBS_DROPDOWN, except that the edit control is replaced by a static text item that displays the current selection in the list box.
... ...
CBS_SIMPLE Displays the list box at all times. The current selection in the list box is displayed in the edit control.
... ...
  • Related