ComboBox

Combobox là tổ hợp của edit box hoặc static text và list box. Combo box được dùng để lựa chọn 1 item từ 1 list option.

#include <windows.h>

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

HINSTANCE g_hinst;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                    LPSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG msg ; 
    WNDCLASS wc = {0};
    wc.lpszClassName = TEXT("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, TEXT("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, hwndStatic;
    const TCHAR *items[] = { TEXT("C/C++"), TEXT("C#"), TEXT("Java"), TEXT("Python") };
    int i;
    LRESULT sel = 0;

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

        CreateWindow(TEXT("button"), TEXT("Drop down"), 
                          WS_CHILD | WS_VISIBLE,
                          150, 10, 90, 25, hwnd, (HMENU)1, g_hinst, NULL); 

        hwndStatic = CreateWindow(TEXT("static"), TEXT(""), 
                                  WS_CHILD | WS_VISIBLE,
                                  150, 80, 90, 25, hwnd, NULL, g_hinst, NULL); 

        for ( 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) 
        { 
            sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
            SetWindowText(hwndStatic, items[sel]);
            SetFocus(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break; 
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

Giải thích:

Trong ví dụ này, ta đặt 3 control trong window : combo box , button và static text. Static text hiển thị các item được lựa chọn từ combo box. Click button để mở combo box.

hwndCombo = CreateWindow(TEXT("combobox"), NULL, 
                         WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,
                         10, 10, 120, 110, hwnd, NULL, g_hinst, NULL);

Để tạo combo box, ta sử dụng “combobox” string. Ta sử dụng CBS_DROPDOWN flag.

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

Ta fill combo box với các item. Để add 1 string tới combo box, ta gửi message CB_ADDSTRING tới combo box.
Nếu ta lựa chọn 1 item từ combo box,hàm WndProc(..,..) sẽ nhận message WM_COMMAND và CBN_SELCHANGE là phần word cao của tham số wParam.

sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
SetWindowText(hwndStatic, items[sel]);
SetFocus(hwnd);

Khi ta send message CB_GETCURSEL tới commbo box. Giá trị trả về của hàm SendMessage( ) là chỉ số của item được lựa chọn. Ta sẽ set string được lựa chọn từ combo box cho static text bằng SetWindowText(hwndStatic, items[sel]); . Combo box đã được focus. Để loại bỏ focus của combo box sau khi lựa chọn item, ta dùng hàm SetFocus(hwnd).

Kết quả:

Combobox
Combobox

Be the first to comment

Leave a Reply