KOR세상

Win32API 기본 윈도우 만들기

작성자

카테고리:

WndProc.h

#ifndef _WNDPROCS_H_
#define _WNDPROCS_H_
#endif
#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);

WinMain.cpp

#include "WndProcs.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);

LPCTSTR lpszWndClass = TEXT("파일 체크섬 프로그램");
HINSTANCE g_hInstance;

int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpszCmdParam, _In_ int nCmdShow)
{
	HWND hWnd;
	MSG Message;
	WNDCLASSEX WndClassEx;

	g_hInstance = hInstance;

	WndClassEx.cbSize = sizeof(WndClassEx); // 구조체 크기를 RegisterClass로 전달하기 위한 멤버이며 보통 값은 자기 자신의 크기를 넘겨주면 됨
	WndClassEx.cbClsExtra = 0; // 윈도우 클래스 레벨에서 추가적인 여분 메모리 할당용. 보안상 이슈로 거의 사용되지 않고 0으로 두는 경우가 많음
	WndClassEx.cbWndExtra = 0; // 윈도우 인스턴스마다 추가적으로 할당할 여분의 메모리 할당용. 각 윈도우별로 별도 할당되고 윈도우별로 특별한 데이터를 저정하기 위한 예약된 메모리 공간. 이쪽도 그렇게 잘 사용되지는 않음. 프로젝트 규모가 크거나 할때는 어느정도 쓰이나 규모가 작거나 복잡도가 낮은경우에는 거의 쓰일 일이 없음
	WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	WndClassEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	WndClassEx.hInstance = hInstance;
	WndClassEx.lpfnWndProc = WndProc;
	WndClassEx.lpszClassName = lpszWndClass;
	WndClassEx.lpszMenuName = NULL;
	WndClassEx.style = CS_VREDRAW | CS_HREDRAW;
	RegisterClassEx(&WndClassEx);

	hWnd = CreateWindowEx(0, lpszWndClass, lpszWndClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, g_hInstance, NULL);
	ShowWindow(hWnd, nCmdShow);

	while (TRUE)
	{
		if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
		{
			if (Message.message == WM_QUIT)
			{
				break;
			}
			else
			{
				TranslateMessage(&Message);
				DispatchMessage(&Message);
			}
		}
		else
		{
			WaitMessage();
		}
	}

	return (int)Message.wParam;
}

WndProc.cpp

#include "WndProcs.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT PaintStruct;

	OPENFILENAME OpenFileName;

	switch (iMessage)
	{
	case WM_CREATE:
		DragAcceptFiles(hWnd, TRUE);
		OpenFileName.lStructSize = sizeof(OpenFileName);
		OpenFileName.hwndOwner = hWnd;
		OpenFileName.lpstrFilter = TEXT("모든 파일\0*.*\0");
		OpenFileName.nMaxFile = MAX_PATH;
		OpenFileName.nMaxFileTitle = MAX_PATH;
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &PaintStruct);
		EndPaint(hWnd, &PaintStruct);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

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

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다