c, git, 컴퓨터/Win32api

3. Device

컴맹학자 2022. 9. 20. 23:16
728x90

Device.cpp = Main() 함수가 있는 실행 클래스 (이름 마음에 안들면 알아서 변경)


1. 클래스 헤더 생성

Device.cpp : Main() 함수가 있는 클래스

Device.h : 전방 함수 선언 및 전역 변수용 

Global.h : 다른 클래스, 헤더에서 사용할 각종 헤더용 정의


2. Global.h 정의

#pragma once
#include <Windows.h>    // 각종 Window 함수 정의
#include <assert.h>     // 검사용

//Global
const UINT Width = 800;  // 화면의 가로길이
const UINT Height = 600; // 화면의 세로길이

3. Device.h 정의

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

전방 선언용 으로 작성, 해당 함수가 WinMain() 함수 보다 아래에 작성 하기 때문


4. Device.cpp 정의

#include "Global.h"
#include "Device.h"

include 관련

 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE prevInstance, LPSTR lPCmdLine, int nCmdShow)
{
    // 1. 윈도우 클래스 구조체 생성 & 정의
	WNDCLASSEX wc;
	{
		wc.cbSize = sizeof(WNDCLASSEX);
		wc.style = CS_HREDRAW | CS_VREDRAW;
		wc.lpfnWndProc = WindowProc;
		wc.cbClsExtra = NULL;
		wc.cbWndExtra = NULL;
		wc.hInstance = hInstance;
		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
		wc.hbrBackground = (HBRUSH)WHITE_BRUSH;
		wc.lpszMenuName = NULL;
		wc.lpszClassName = "WIN32";
		wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
		wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

		WORD check = RegisterClassEx(&wc);
		assert(check != NULL);
	}
    
	// 2. 윈도우 셋팅
	HWND hwnd = CreateWindow(
		"WIN32",
		"Window API 32",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		Width, Height, NULL, NULL, hInstance, NULL
	);
	assert(hwnd != NULL);
	
    // 3. 윈도우 출력
	ShowWindow(hwnd, nCmdShow); // WM_PAINT와 관련
	UpdateWindow(hwnd);

    // 4. 메시지 루프
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg); // 문자 & 키입력 관련 (키보드 관련)
		DispatchMessage(&msg);  // WindowProc에 전달
	}
	
	return 0;
}

2장에서 적어 놨듯이 1 ~3까진 윈도우 클래스 관련 생성 & 정의 하고 실제 동작은 4. 메시지 루프를 통해서 WindowProc() 함수를 호출

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// uMsg 안에는 키보드, 마우스 값들이 존재
	switch (uMsg)
	{
		//화면에 그리기 최초 UpdateWindow()함수에 의해 발생
		case WM_PAINT:    
		{

		} break;

		//위도우가 화면에 사라지면 보내는 메시지
		case WM_DESTROY: PostQuitMessage(0); return 0;
	}

	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

메시지루프 에서 msg에 값이 들어오면 자동으로 호출 되는 CALLBACK 함수


실행

 

'c, git, 컴퓨터 > Win32api' 카테고리의 다른 글

4. Resource  (0) 2022.09.26
2. WinMain 구조  (0) 2022.09.20
1. 프로젝트 생성&설정  (0) 2022.09.20