본문 바로가기

공부/윈도우api

그리기 모드(윈도우api)소스

반응형

#include <windows.h>

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)GetStockObject(WHITE_BRUSH);
 WndClass.hCursor=LoadCursor(NULL, IDC_ARROW);
 WndClass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
 WndClass.hInstance=hInstance;
 WndClass.lpfnWndProc=WndProc;
 WndClass.lpszClassName=lpszClass;
 WndClass.lpszMenuName=TEXT("MyMenu");
 WndClass.style=CS_HREDRAW|CS_VREDRAW;
 RegisterClass(&WndClass);

 hWnd=CreateWindow(lpszClass,TEXT("My First Program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,(HMENU)NULL,hInstance,NULL);
 ShowWindow(hWnd,nCmdShow);

 while(GetMessage(&Message,NULL,0,0)) {
  TranslateMessage(&Message);
  DispatchMessage(&Message);
 }
 return (int)Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
 static int sx, sy, oldx, oldy;
 static BOOL bNowDraw = FALSE;
 HDC hdc;

 switch (iMessage) {
  case WM_LBUTTONDOWN:
   sx = LOWORD(lParam);
   sy = HIWORD(lParam);
   oldx = sx;
   oldy = sy;
   bNowDraw = TRUE;
   return 0;
  case WM_MOUSEMOVE:
   if(bNowDraw) {
    hdc = GetDC(hWnd);
    SetROP2(hdc, R2_NOT);
    MoveToEx(hdc, sx, sy, NULL);
    LineTo(hdc, oldx, oldy);
    oldx = LOWORD(lParam);
    oldy = HIWORD(lParam);
    MoveToEx(hdc, sx, sy, NULL);
    LineTo(hdc, oldx, oldy);
    ReleaseDC(hWnd, hdc);
   }
   return 0;
  case WM_LBUTTONUP:
   bNowDraw = FALSE;
   return 0;
  case WM_DESTROY :
   PostQuitMessage(0);
   return 0;

 }
 return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

반응형