티스토리 뷰

WinAPI 콤보박스(ComboBox)와 에디트컨트롤(Edit Control) 예제


 전체 소스


#include <windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

{

HWND     hwnd;

MSG      msg;

WNDCLASS WndClass;

hInst = hInstance;

WndClass.style = CS_HREDRAW | CS_VREDRAW;

WndClass.lpfnWndProc = WndProc;

WndClass.cbClsExtra = 0;

WndClass.cbWndExtra = 0;

WndClass.hInstance = hInstance;

WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);

WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

WndClass.lpszMenuName = NULL;

WndClass.lpszClassName = "WinExam3";

RegisterClass(&WndClass);

hwnd = CreateWindow("WinExam3",

"WinExam3",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL

);

ShowWindow(hwnd, nCmdShow);

UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return (int)msg.wParam;

}


#define IDC_STATIC 100

#define IDC_EDIT_1 101

#define IDC_EDIT_2 101

#define IDC_COMBO_1 103

#define IDC_COMBO_2 104

#define IDC_COMBO_3 105

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

{

static HWND hEdit1, hEdit2, hCombo1, hCombo2, hCombo3;

static HWND hStatic;


int x;

static char str[10] = "", result[10] = "";

char year[][10] = { "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000",

"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"};

char month[][10] = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };

char day[][10] = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",

"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};


switch (iMsg)

{

case WM_CREATE:

hEdit1 = CreateWindow(

"edit",

"",

WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY,

75,

125,

100,

25,

hwnd,

(HMENU)IDC_EDIT_1,

hInst,

NULL);

hEdit2 = CreateWindow(

"edit",

"",

WS_CHILD | WS_VISIBLE | WS_BORDER,

225,

125,

100,

25,

hwnd,

(HMENU)IDC_EDIT_2,

hInst,

NULL);


hCombo1 = CreateWindow(

"combobox",

NULL,

WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,

25,

50,

125,

300,

hwnd,

(HMENU)IDC_COMBO_1,

hInst,

NULL);

hCombo2 = CreateWindow(

"combobox",

NULL,

WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,

150,

50,

125,

200,

hwnd,

(HMENU)IDC_COMBO_2,

hInst,

NULL);

hCombo3 = CreateWindow(

"combobox",

NULL,

WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,

275,

50,

125,

300,

hwnd,

(HMENU)IDC_COMBO_3,

hInst,

NULL);


hStatic = CreateWindow(

"static",

"년도 선택",

WS_CHILD | WS_VISIBLE,

50,

25,

70,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);

hStatic = CreateWindow(

"static",

"월 선택",

WS_CHILD | WS_VISIBLE,

175,

25,

70,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);

hStatic = CreateWindow(

"static",

"일 선택",

WS_CHILD | WS_VISIBLE,

300,

25,

70,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);

hStatic = CreateWindow(

"static",

"주민번호 앞자리",

WS_CHILD | WS_VISIBLE,

60,

100,

120,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);

hStatic = CreateWindow(

"static",

"주민번호 뒷자리",

WS_CHILD | WS_VISIBLE,

215,

100,

120,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);

hStatic = CreateWindow(

"static",

"---",

WS_CHILD | WS_VISIBLE,

190,

130,

18,

18,

hwnd,

(HMENU)IDC_STATIC,

hInst,

NULL);


for (int i = 0; i < 25; i++)

SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)year[i]);

for (int i = 0; i < 11; i++)

SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)month[i]);

for (int i = 0; i < 31; i++)

SendMessage(hCombo3, CB_ADDSTRING, 0, (LPARAM)day[i]);


MoveWindow(hwnd, 0, 0, 500, 400, TRUE);

return 0;

case WM_COMMAND:

switch (LOWORD(wParam))

{

case IDC_COMBO_1:

switch (HIWORD(wParam))

{

case CBN_SELCHANGE:

x = SendMessage(hCombo1, CB_GETCURSEL, 0, 0);

SendMessage(hCombo1, CB_GETLBTEXT, x, (LPARAM)str);

result[0] = str[2];

result[1] = str[3];

result[2] = NULL;

SetDlgItemText(hwnd, IDC_EDIT_1, result);

}

case IDC_COMBO_2:

switch (HIWORD(wParam))

{

case CBN_SELCHANGE:

x = SendMessage(hCombo2, CB_GETCURSEL, 0, 0);

SendMessage(hCombo2, CB_GETLBTEXT, x, (LPARAM)str);

result[2] = str[0];

result[3] = str[1];

result[4] = NULL;

SetDlgItemText(hwnd, IDC_EDIT_1, result);

}

case IDC_COMBO_3:

switch (HIWORD(wParam))

{

case CBN_SELCHANGE:

x = SendMessage(hCombo3, CB_GETCURSEL, 0, 0);

SendMessage(hCombo3, CB_GETLBTEXT, x, (LPARAM)str);

result[4] = str[0];

result[5] = str[1];

result[6] = NULL;

SetDlgItemText(hwnd, IDC_EDIT_1, result);

}

}

return 0;

case WM_DESTROY:

PostQuitMessage(0);

return 0;

}

return DefWindowProc(hwnd, iMsg, wParam, lParam);

}


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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