#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPCTSTR lpszClass=TEXT("정훈승 SimplePaint");
enum {ID_R1=101, ID_R2, ID_R3, ID_R4, ID_R5, ID_R6, ID_R7, ID_R8, ID_R9 }; // 색상정보 정의
enum {ID_S1=201, ID_S2, ID_S3, ID_S4, ID_S5 }; // 선굵기정보 정의
HWND r1, r2, r3, r4, r5, r6, r7, r8, r9, s1, s2, s3, s4, s5; // 핸들값
int Graph=0;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
HACCEL hAccel; // 엑셀러레이터를 위해
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
//WndClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE+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 | CS_DBLCLKS;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
hAccel=LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1)); // 엑셀러레이터를 위해
while (GetMessage(&Message,NULL,0,0)) {
if(!TranslateAccelerator(hWnd, hAccel, &Message)) { // 엑셀러레이터를 위해
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
return (int)Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int sx, sy, oldx, oldy; // 마우스 현재 위치와 브러쉬 중첩을 위해
static int jhs; // case문을 효율적으로 활용하기 위해
HPEN MyPen, OldPen; // 펜을 위해
static COLORREF Color=RGB(0,0,0); // 색상초기값 검정
static Size=1; // 선굵기 초기값 1
static BOOL bNowDraw=FALSE; // 처음엔 선이 안그어지게 설정(마우스를 클릭해야 그어짐)
switch (iMessage) {
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("Color▼"), WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
5, 5, 110, 300, hWnd, (HMENU)0, g_hInst, NULL);
r1=CreateWindow(TEXT("button"), TEXT("검정색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,
10, 20, 100, 30, hWnd, (HMENU)ID_R1, g_hInst, NULL);
r2=CreateWindow(TEXT("button"), TEXT("빨강색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 50, 100, 30, hWnd, (HMENU)ID_R2, g_hInst, NULL);
r3=CreateWindow(TEXT("button"), TEXT("파랑색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 80, 100, 30, hWnd, (HMENU)ID_R3, g_hInst, NULL);
r4=CreateWindow(TEXT("button"), TEXT("초록색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 110, 100, 30, hWnd, (HMENU)ID_R4, g_hInst, NULL);
r5=CreateWindow(TEXT("button"), TEXT("보라색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 140, 100, 30, hWnd, (HMENU)ID_R5, g_hInst, NULL);
r6=CreateWindow(TEXT("button"), TEXT("크림슨색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 170, 100, 30, hWnd, (HMENU)ID_R6, g_hInst, NULL);
r7=CreateWindow(TEXT("button"), TEXT("오렌지색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 200, 100, 30, hWnd, (HMENU)ID_R7, g_hInst, NULL);
r8=CreateWindow(TEXT("button"), TEXT("분홍색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 230, 100, 30, hWnd, (HMENU)ID_R8, g_hInst, NULL);
r9=CreateWindow(TEXT("button"), TEXT("황금색"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
10, 260, 100, 30, hWnd, (HMENU)ID_R9, g_hInst, NULL);
CreateWindow(TEXT("button"), TEXT("Size▼"), WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
130, 5, 110, 180, hWnd, (HMENU)0, g_hInst, NULL);
s1=CreateWindow(TEXT("button"), TEXT("선굵기1"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,
135, 20, 100, 30, hWnd, (HMENU)ID_S1, g_hInst, NULL);
s2=CreateWindow(TEXT("button"), TEXT("선굵기2"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
135, 50, 100, 30, hWnd, (HMENU)ID_S2, g_hInst, NULL);
s3=CreateWindow(TEXT("button"), TEXT("선굵기3"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
135, 80, 100, 30, hWnd, (HMENU)ID_S3, g_hInst, NULL);
s4=CreateWindow(TEXT("button"), TEXT("선굵기4"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
135, 110, 100, 30, hWnd, (HMENU)ID_S4, g_hInst, NULL);
s5=CreateWindow(TEXT("button"), TEXT("선굵기5"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
135, 140, 100, 30, hWnd, (HMENU)ID_S5, g_hInst, NULL);
CheckRadioButton(hWnd,ID_R1,ID_R9,ID_R1);
CheckRadioButton(hWnd,ID_S1,ID_S5,ID_S1);
break;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case ID_R1: Color=RGB(0, 0, 0); break; // 검정색
case ID_R2: Color=RGB(255, 0, 0); break; // 빨강색
case ID_R3: Color=RGB(0, 0, 255); break; // 파랑색
case ID_R4: Color=RGB(0, 140, 0); break; // 초록색
case ID_R5: Color=RGB(130, 0, 130); break; // 보라색
case ID_R6: Color=RGB(129, 0, 45); break; // 크림슨색
case ID_R7: Color=RGB(255, 138, 0); break; // 오렌지색
case ID_R8: Color=RGB(255, 100, 255); break; // 분홍색
case ID_R9: Color=RGB(255, 174, 0); break; // 황금색
case ID_S1: Size=1; break;
case ID_S2: Size=2; break;
case ID_S3: Size=3; break;
case ID_S4: Size=4; break;
case ID_S5: Size=5; break;
case IDM_FILE_MENU1:
jhs = 1; // case문을 효율적으로 활용하기 위해 임의 숫자를 입력
MessageBox(hWnd, TEXT("자유곡선을 선택했습니다."), TEXT("정훈승 SimplePaint"), MB_OK);
return 0;
case IDM_FILE_MENU2:
jhs = 2;
MessageBox(hWnd, TEXT("직선을 선택했습니다."), TEXT("정훈승 SimplePaint"), MB_OK);
return 0;
case IDM_FILE_MENU3:
jhs = 3;
MessageBox(hWnd, TEXT("원을 선택했습니다."), TEXT("정훈승 SimplePaint"), MB_OK);
return 0;
case IDM_FILE_MENU4:
jhs = 4;
MessageBox(hWnd, TEXT("사각형을 선택했습니다."), TEXT("정훈승 SimplePaint"), MB_OK);
return 0;
case IDM_FILE_MENU5:
MessageBox(hWnd, TEXT("화면을 초기화합니다."), TEXT("정훈승 SimplePaint"), MB_OK);
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
case IDM_COLOR1:
MessageBox(hWnd, TEXT("Black 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(0, 0, 0); return 0;
case IDM_COLOR2:
MessageBox(hWnd, TEXT("Red 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(255, 0, 0); return 0;
case IDM_COLOR3:
MessageBox(hWnd, TEXT("Blue 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(0, 0, 255); return 0;
case IDM_COLOR4:
MessageBox(hWnd, TEXT("Green 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(0, 140, 0); return 0;
case IDM_COLOR5:
MessageBox(hWnd, TEXT("Violet 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(130, 0, 130); return 0;
case IDM_COLOR6:
MessageBox(hWnd, TEXT("Crimson 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(129, 0, 45); return 0;
case IDM_COLOR7:
MessageBox(hWnd, TEXT("Orange 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(255, 138, 0); return 0;
case IDM_COLOR8:
MessageBox(hWnd, TEXT("Pink 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(255, 100, 255); return 0;
case IDM_COLOR9:
MessageBox(hWnd, TEXT("Gold 선택"), TEXT("정훈승 SimplePaint"), MB_OK);
Color=RGB(255, 174, 0); return 0;
case IDM_SIZE1: MessageBox(hWnd, TEXT("매우 얇게 선택"), TEXT("정훈승 SimplePaint"), MB_OK); Size=1; return 0;
case IDM_SIZE2: MessageBox(hWnd, TEXT("얇게 선택"), TEXT("정훈승 SimplePaint"), MB_OK); Size=2; return 0;
case IDM_SIZE3: MessageBox(hWnd, TEXT("보통 선택"), TEXT("정훈승 SimplePaint"), MB_OK); Size=3; return 0;
case IDM_SIZE4: MessageBox(hWnd, TEXT("굵게 선택"), TEXT("정훈승 SimplePaint"), MB_OK); Size=4; return 0;
case IDM_SIZE5: MessageBox(hWnd, TEXT("매우 굵게 선택"), TEXT("정훈승 SimplePaint"), MB_OK); Size=5; return 0;
}
return 0;
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);
switch(jhs) {
case 1: // 자유곡선
MyPen=CreatePen(PS_SOLID, Size, Color);
OldPen=(HPEN)SelectObject(hdc, MyPen);
MoveToEx(hdc, sx, sy, NULL);
sx=LOWORD(lParam);
sy=HIWORD(lParam);
LineTo(hdc, sx, sy);
SelectObject(hdc, OldPen);
DeleteObject(MyPen);
ReleaseDC(hWnd, hdc);
return 0;
case 2: // 선
MyPen=CreatePen(PS_SOLID, Size, Color);
OldPen=(HPEN)SelectObject(hdc, MyPen);
SetROP2(hdc, R2_NOTXORPEN);
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 3: // 원
MyPen=CreatePen(PS_SOLID, Size, Color);
OldPen=(HPEN)SelectObject(hdc, MyPen);
SetROP2(hdc, R2_NOTXORPEN);
Ellipse(hdc, sx, sy, oldx, oldy);
sx=LOWORD(lParam);
sy=HIWORD(lParam);
Ellipse(hdc, sx, sy, oldx, oldy);
SelectObject(hdc, OldPen);
DeleteObject(MyPen);
ReleaseDC(hWnd, hdc);
return 0;
case 4: // 사각형
MyPen=CreatePen(PS_SOLID, Size, Color);
OldPen=(HPEN)SelectObject(hdc, MyPen);
SetROP2(hdc, R2_NOTXORPEN);
Rectangle(hdc, sx, sy, oldx, oldy);
sx=LOWORD(lParam);
sy=HIWORD(lParam);
Rectangle(hdc, sx, sy, oldx, oldy);
SelectObject(hdc, OldPen);
DeleteObject(MyPen);
ReleaseDC(hWnd, hdc);
return 0;
}
MyPen=CreatePen(PS_SOLID, Size, Color); // 초기값 자유곡선
OldPen=(HPEN)SelectObject(hdc, MyPen);
MoveToEx(hdc, sx, sy, NULL);
sx=LOWORD(lParam);
sy=HIWORD(lParam);
LineTo(hdc, sx, sy);
SelectObject(hdc, OldPen);
DeleteObject(MyPen);
ReleaseDC(hWnd, hdc);
}
return 0;
case WM_LBUTTONUP:
bNowDraw = FALSE; // 마우스를 땠을 때 선 안그어짐
hdc = GetDC(hWnd);
return 0;
case WM_PAINT:
hdc=BeginPaint(hWnd, &ps);
TextOut(hdc, 1, 320, TEXT("ㆍ초기값 : 자유곡선, 매우 얇게"), 30);
TextOut(hdc, 1, 345, TEXT("ㆍ자유곡선 선택 : Ctrl+I"), 24);
TextOut(hdc, 1, 370, TEXT("ㆍ직선 선택 : Ctrl+L "), 24);
TextOut(hdc, 1, 395, TEXT("ㆍ원 선택 : Ctrl+E "), 24);
TextOut(hdc, 1, 420, TEXT("ㆍ사각형 선택 : Ctrl+R "), 24);
TextOut(hdc, 1, 445, TEXT("ㆍ화면 초기화 : Ctrl+S "), 24);
TextOut(hdc, 1, 470, TEXT("ㆍ프로그램 종료 : Ctrl+X"), 24);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
SimplePaint 문제로 유명한 그 소스의 해답이라고 말하고 싶다.
첨보는 사람은 그냥 존나 긴~ 쏘스라고만 생각하겠지만
차근차근 공부하면 뭔말인지 다 이해가 감
리소스 추가는 셀프^^