combobox를 win32api로 만들어봤습니다.
글쓴이: mydream / 작성시간: 화, 2015/08/04 - 1:33오후
#include <windows.h> #include <tchar.h> #include <string.h> #define ID_COMBOBOX 100 TCHAR *items[]={TEXT("APPLE"), TEXT("Orange"), TEXT("Melon"), TEXT("Grape"), TEXT("Strawberry")}; HWND hCombo; HINSTANCE hgInstance; LRESULT CALLBACK WndProc(HWND hwnd , UINT message, WPARAM wparam, LPARAM lparam) { int i; TCHAR str[128]; static TCHAR tmbuffer[128]; switch(message) { case WM_CREATE: hCombo=CreateWindow(TEXT("combobox"), TEXT(""), WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | WS_OVERLAPPED | CBS_HASSTRINGS,10, 10, 100, 200, hwnd, (HMENU)ID_COMBOBOX, hgInstance, NULL); tmbuffer[0]=TEXT('\0'); for(i=0;i<5;i++) { SendMessage(hCombo, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)items[i]); _stprintf(tmbuffer, TEXT("%s %d"), tmbuffer, i); } //SendMessage(hCombo, CB_SETCURSEL, (WPARAM)2, (LPARAM)0); //ShowWindow(hCombo, SW_SHOWNORMAL); return 0; case WM_COMMAND: switch(LOWORD(wparam)) { case ID_COMBOBOX: switch(HIWORD(wparam)) { case CBN_SELCHANGE: i=SendMessage(hCombo, CB_GETCURSEL, 0, 0); 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_PAINT: { PAINTSTRUCT pst; HDC hdc=BeginPaint(hwnd, &pst); TextOut(hdc, 50, 400, tmbuffer, _tcslen(tmbuffer)); EndPaint(hwnd, &pst); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wparam, lparam); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASS WndCls; WndCls.cbClsExtra=0; WndCls.cbWndExtra=0; WndCls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); WndCls.hCursor=LoadCursor(NULL, IDC_ARROW); WndCls.hIcon=NULL; WndCls.hInstance=hgInstance=hInstance; WndCls.lpfnWndProc=WndProc; LPCTSTR clsname=TEXT("combobox"); WndCls.lpszClassName=clsname; WndCls.lpszMenuName=NULL; WndCls.style=CS_HREDRAW | CS_VREDRAW; RegisterClass(&WndCls); HWND hwnd=CreateWindow(clsname, TEXT("Displaying combobox"), WS_OVERLAPPEDWINDOW, 300, 300, 500, 500, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nShowCmd); MSG message; while( GetMessage(&message, NULL, 0, 0) ) { TranslateMessage(&message); DispatchMessage(&message); } return (int)message.wParam; }
그런데 콤보박스가 제대로 화면에 나타나지 않습니다. 어디가 잘못된 것인가요?
Forums:
클래스명이 잘못되었네요.
LPCTSTR clsname=TEXT("combobox");
WndCls.lpszClassName=clsname;
HWND hwnd=CreateWindow(clsname, ...(생략)
부모의 클래스명이 "combobox"인데, 자식도 "combobox"이니 컴파일러가 부모자식 눈에 뵈는게 없어서 안나타날듯 싶군요.. ^^);;
명쾌한 답변 감사드립니다.
겹치면 안되는 것이었군요. 이것 때문에 3일 헤맸습니다.
다들 한번쯤 하는 경험입니다..
윈도우 프로그래밍 시작하면 다들 한번쯤 클래스명 때문에 고생하지요.. :)
즐프하세요~!
댓글 달기