Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

7-7. 콤보 박스 본문

Windows/윈도우즈 API

7-7. 콤보 박스

scarecrow1992 2021. 12. 28. 23:36

0. 개요

에디트 컨트롤과 리스트 박스를 결합시킨 형태의 컨트롤

목록에 있는 항목중 다수를 선택할 수 있고, 원하는 항목이 없으면 에디트를 통해 직접 입력할 수도 있다.

항상 열려있는 리스트박스에 비해 필요한 경우에만 목록을 열어 선택 가능하다.

 

1. 스타일

스타일 설명
CBS_SIMPLE 에디트만 가진다.
CBS_DROPDOWN 에디트와 리스트 박스를 가진다.
CBS_DROPDOWNLIST 리스트 박스만 가지며 에디트에 항목을 입력할 수는 없다.

 

이외의 특징이나 메시지들에 대해서는 MSDN을 참고한다.

https://docs.microsoft.com/en-us/windows/win32/controls/combo-boxes

 

사용법의 예는 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <windows.h>
#include <tchar.h>
#include <vector>
 
#define ID_COMBOBOX 100
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("Menu");
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
    , LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;
 
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = (WNDPROC)WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName = NULL;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);
 
    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
 
    while (GetMessage(&Message, 000)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}
 
WCHAR Items[][15= { L"Apple", L"Orange", L"Melon", L"Graph", L"Strawberry" };
WCHAR str[128];
HWND hCombo;
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    int i;
    switch (iMessage) {
    case WM_CREATE:
        hCombo = CreateWindow(L"combobox"NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,
            1010100200, hWnd, (HMENU)ID_COMBOBOX, g_hInst, NULL);
        for (i = 0; i < 5; i++)
            SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)Items[i]);
        return 0;
    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case ID_COMBOBOX:
            switch (HIWORD(wParam)) {
            case CBN_SELCHANGE:
                i = SendMessage(hCombo, CB_GETCURSEL, 00);
                SendMessage(hCombo, CB_GETLBTEXT, i, (LPARAM)str);
                SetWindowText(hWnd, str);
                break;
            case CBN_EDITCHANGE:
                GetWindowText(hCombo, str, 128);
                SetWindowText(hWnd, str);
                break;
            }
        }
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
cs

위 사진에서는 항목리스트를 일부러 열어두었지만, 원하는 항목을 선택하면 리스트창이 사라지면서 해당 항목이 선택된다. 그 결과로 window title이 변경되게끔 했다.

 

위 처럼 list에 없는 항목일지라도 입력을 넣으면 해당 입력값에 맞추어 출력이 발생함을 알 수 있다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Windows > 윈도우즈 API' 카테고리의 다른 글

7-9. 스태틱  (0) 2021.12.29
7-8. 스크롤 바  (0) 2021.12.29
7-6. 컨트롤 - 리스트 박스  (0) 2021.12.27
7-4. 컨트롤 - 에디트  (0) 2021.12.27
7-3. 컨트롤 - 라디오 버튼  (0) 2021.12.27
Comments