본문 바로가기

API 프로그래밍

롭모드 프로그램

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPCTSTR lpszClass=TEXT("RopMode2");

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_WINDOW+1);
 WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
 WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 WndClass.hInstance=hInstance;
 WndClass.lpfnWndProc=WndProc;
 WndClass.lpszClassName=lpszClass;
 WndClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
 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,NULL,0,0)) {
  TranslateMessage(&Message);
  DispatchMessage(&Message);
 }
 return (int)Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
 static int x, y, oldx, oldy;
 static BOOL bNowDraw = FALSE;
 static int ch = 0,cl=RGB(0,0,0),width=1;
 HDC hdc;
 HPEN mypen,oldpen;

 switch(iMessage) {
  case WM_COMMAND:
   switch(LOWORD(wParam)) {
    case ID_FREE:
     ch = 0; break;
    case ID_LINE:
     ch = 1; break;
    case ID_CIRCLE:
     ch = 2; break;
    case ID_RECT:
     ch = 3; break;
    case ID_RED:
     cl = RGB(255,0,0);break;
    case ID_YELLOW:
     cl = RGB(255,255,0);break;
    case ID_BLACK:
     cl = RGB(0,0,0);break;
    case ID_BLUE:
     cl = RGB(0,0,255);break;
    case ID_X1:
     width = 1;break;
    case ID_X2:
     width = 2;break;
    case ID_X3:
     width = 3;break;
    case ID_X4:
     width = 4;break;
    case ID_EXIT:
     PostQuitMessage(0);
     return 0;
   }
   return 0;
  case WM_LBUTTONDOWN:
   x = LOWORD(lParam);
   y = HIWORD(lParam);
   oldx = x;
   oldy = y;
   bNowDraw = TRUE;
   return 0;
  case WM_MOUSEMOVE:
   if(bNowDraw) {
    hdc = GetDC(hWnd);
    switch(ch) {
     case 0: // 자유곡선 그리기
      mypen = CreatePen(PS_SOLID,width,cl);
      oldpen = (HPEN)SelectObject(hdc,mypen);
      MoveToEx(hdc, x, y, NULL);
      x = LOWORD(lParam);
      y = HIWORD(lParam);
      LineTo(hdc, x, y);
      SelectObject(hdc,oldpen);
      DeleteObject(mypen);
      break;
     case 1: // 직선 그리기
      mypen = CreatePen(PS_SOLID,width,cl);
      oldpen = (HPEN)SelectObject(hdc,mypen);
      SetROP2(hdc, R2_NOTXORPEN);      
      MoveToEx(hdc, x, y, NULL);
      LineTo(hdc, oldx, oldy);
      oldx = LOWORD(lParam);
      oldy = HIWORD(lParam);
      MoveToEx(hdc, x, y, NULL);
      LineTo(hdc, oldx, oldy);
      SelectObject(hdc,oldpen);
      DeleteObject(mypen);
      break;
     case 2: // 원 그리기
      mypen = CreatePen(PS_SOLID,width,cl);
      oldpen = (HPEN)SelectObject(hdc,mypen);
      SetROP2(hdc, R2_NOTXORPEN);
      Ellipse(hdc, x, y, oldx, oldy);
      oldx = LOWORD(lParam);
      oldy = HIWORD(lParam);
      Ellipse(hdc, x, y, oldx, oldy);
      SelectObject(hdc,oldpen);
      DeleteObject(mypen);
      break;
     case 3: // 사각형 그리기
      mypen = CreatePen(PS_SOLID,width,cl);
      oldpen = (HPEN)SelectObject(hdc,mypen);
      SetROP2(hdc, R2_NOTXORPEN);
      Rectangle(hdc, x, y, oldx, oldy);
      oldx = LOWORD(lParam);
      oldy = HIWORD(lParam);
      Rectangle(hdc, x, y, oldx, oldy);
      SelectObject(hdc,oldpen);
      DeleteObject(mypen);
      break;
    }
    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));
}