특별한딸기이야기

간단한 SDK 예제 프로그램 본문

딸기 공부방/MFC공부방

간단한 SDK 예제 프로그램

특별한녀석 2008. 1. 7. 02:55

#include <windows.h>

// WinMain()함수에서 참조하므로 함수 원형을 선언한다.
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nShowCmd)
{
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;

 // 윈도우 클래스를 초기화하고 운영체제에 등록한다.
 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 = "HelloClass";

 if(!RegisterClass(&wndclass))
 {
  return -1;
 }

 // 윈도우를 생성하고 화면에 보이게 한다.
 hwnd = CreateWindow("HelloClass", "Hello SDK Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
 ShowWindow(hwnd, nShowCmd);
 UpdateWindow(hwnd);

 // 메세지 대기열에서 메시지를 하나씩 꺼내 처리한다.
 while(GetMessage(&msg, NULL, 0, 0) > 0)
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 char *str = "Hello SDK";

 // 메시지 종류에 따라 응용 프로그램이 적절히 처리한다.
 switch(message)
 {
 case WM_CREATE :
  return 0;
 case WM_PAINT :
  hdc = BeginPaint(hwnd, &ps);
  TextOut(hdc, 100, 100, str, lstrlen(str));
  EndPaint(hwnd, &ps);
  return 0;
 case WM_LBUTTONDOWN :
  MessageBox(hwnd, "마우스를 클릭했습니다.", "마우스 메세지", MB_OK);
  return 0;
 case WM_DESTROY :
  PostQuitMessage(0);
  return 0;
 }

 // 응용프로그램이 처리하지 않으면 윈도우 운영체제가 처리한다.
 return DefWindowProc(hwnd, message, wParam, lParam);
}