티스토리 뷰

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

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

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 = NULL;
	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;
}

int GetCheckRadioButton(HWND hDlg, int First, int Last);	//라디오 버튼 체크 확인
int x, y;
COLORREF penColor = RGB(0, 0, 0);
int width = 1;
BOOL bNowDraw = FALSE;
enum tag_color { BLACK, RED, GREEN, BLUE } Color;	//선택된 색상
BOOL bBold = FALSE;	//굵기 선택

//대화상자 프로시저
BOOL CALLBACK PaintDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	switch (iMessage)
	{
	case WM_INITDIALOG:
		CheckDlgButton(hDlg, IDC_CHKBOLD, bBold ? BST_CHECKED : BST_UNCHECKED);
		CheckRadioButton(hDlg, IDC_RDCOLOR1, IDC_RDCOLOR4, IDC_RDCOLOR1 + Color);
		return TRUE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			Color = tag_color(GetCheckRadioButton(hDlg, IDC_RDCOLOR1, IDC_RDCOLOR4) - IDC_RDCOLOR1);
			bBold = (IsDlgButtonChecked(hDlg, IDC_CHKBOLD) == BST_CHECKED);
			EndDialog(hDlg, IDOK);
			return TRUE;
		case IDCANCEL:
			EndDialog(hDlg, IDCANCEL);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	HPEN MyPen, OldPen;

	switch (iMessage)
	{
	case WM_CREATE:
		hWndMain = hWnd;
		return 0;
	case WM_KEYDOWN:
		switch (LOWORD(wParam))
		{
			//Delete키 입력 시 화면 지우기
		case VK_DELETE:
			InvalidateRect(hWnd, NULL, TRUE);
			break;
		}
		return 0;
	case WM_LBUTTONDOWN:
		x = LOWORD(lParam);
		y = HIWORD(lParam);
		//굵기 설정
		if (bBold)
			width = 3;
		else
			width = 1;
		//색상 설정
		switch (Color)
		{
		case BLACK:
			penColor = RGB(0, 0, 0);
			break;
		case RED:
			penColor = RGB(255, 0, 0);
			break;
		case GREEN:
			penColor = RGB(0, 255, 0);
			break;
		case BLUE:
			penColor = RGB(0, 0, 255);
			break;
		}
		bNowDraw = TRUE;
		return 0;
	case WM_MOUSEMOVE:
		if (bNowDraw)
		{
			hdc = GetDC(hWnd);
			MyPen = CreatePen(PS_SOLID, width, penColor);
			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);
			ReleaseDC(hWnd, hdc);
		}
		return 0;
	case WM_LBUTTONUP:
		bNowDraw = FALSE;
		return 0;
	case WM_RBUTTONDOWN:
		//대화상자 불러오기
		if (DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DIALOG1),
			hWnd, PaintDlgProc) == IDOK)
		{
			InvalidateRect(hWnd, NULL, FALSE);
		}
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return (DefWindowProc(hWnd, iMessage, wParam, lParam));
}

//반복문을 이용하여 라디오 버튼 체크 확인
int GetCheckRadioButton(HWND hDlg, int First, int Last)
{
	int id;

	for (id = First; id <= Last; id++)
	{
		if (IsDlgButtonChecked(hDlg, id) == BST_CHECKED)
		{
			return id;
		}
	}
	return -1;
}
댓글
댓글쓰기 폼