Directx11学习笔记【二】 将HelloWin封装成类

时间:2022-12-24 11:18:26

我们把上一个教程的代码封装到一个类中来方便以后的使用。

首先新建一个空工程叫做MyHelloWin,添加一个main.cpp文件,然后新建一个类叫做MyWindow,将于窗体有关的操作封装到里面

MyWindow.h文件

 /************************************************************************
Directx11学习笔记【2】 将HelloWin封装成类
2016.01 by zhangbaochong
/************************************************************************/
#pragma once
#include <windows.h> static bool isPushEsc = false;//是否按下Esc键 class MyWindow
{
public:
MyWindow();
~MyWindow();
public:
HWND GetHandle();//返回窗口句柄
bool Create(int &width, int &height);//创建窗口
void Run();//处理消息循环
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);//消息处理
private:
HWND m_hwnd;
HINSTANCE m_hinstance;
LPCWSTR m_name;
};

MyWindow.cpp

因为定义窗口的时候必须指定一个回调函数,所以我们定义一个静态的WndProc,因为在WndProc中需要调用其他消息的处理函数MessageHandler,所以我们又定义一个类的实例句柄applicationHandle。

 /************************************************************************
Directx11学习笔记【2】 将HelloWin封装成类
2016.01 by zhangbaochong
/************************************************************************/ #include "MyWindow.h" static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);//静态回调函数
static MyWindow *applicationHandle;//类的一个静态实例 MyWindow::MyWindow()
{
isPushEsc = false;
m_hwnd = NULL;
m_name = L"HelloWin";
} MyWindow::~MyWindow()
{
} HWND MyWindow::GetHandle()
{
return m_hwnd;
} bool MyWindow::Create(int &width, int &height)
{
WNDCLASSEX wnd;
applicationHandle = this;
m_hinstance = GetModuleHandle(NULL);
wnd.cbClsExtra = ;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.cbWndExtra = ;
wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wnd.hIconSm = wnd.hIcon;
wnd.hInstance = m_hinstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = m_name;
wnd.lpszMenuName = m_name;
wnd.style = CS_VREDRAW | CS_HREDRAW; //注册窗口
if ( !RegisterClassEx(&wnd) )
{
MessageBox(NULL, L"注册窗口失败", L"error", );
return false;
}
m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_name, m_name, WS_OVERLAPPEDWINDOW, , , width, height,
NULL, NULL, m_hinstance, NULL);
//显示窗口设置其为焦点
ShowWindow(m_hwnd, SW_SHOW);
UpdateWindow(m_hwnd);
return true;
} static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage();
return ;
//其他消息发送MessageHandler处理
default:
return applicationHandle->MessageHandler(hwnd, message, wparam, lparam);
}
} LRESULT CALLBACK MyWindow::MessageHandler(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
//检测按键消息
case WM_KEYDOWN:
if (wparam == VK_ESCAPE)//用户按下退出键
isPushEsc = true;
return ; //其他消息发送windows缺省处理
default:
return DefWindowProc(hwnd, message, wparam, lparam);
}
} void MyWindow::Run()
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
bool isRuning = true;//控制是否退出消息循环
while (isRuning)
{
//处理windows消息
if (PeekMessage(&msg, NULL, , , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
{
isRuning = false;
}
else//按下esc键也退出
{
isRuning = !isPushEsc; //渲染等处理可以放在这儿
} }
}

main.cpp

 /************************************************************************
Directx11学习笔记【2】 将HelloWin封装成类
2016.01 by zhangbaochong
/************************************************************************/
#include "MyWindow.h" int WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
int width = , height = ;
MyWindow *window = new MyWindow;
if (window->Create(width, height))
{
window->Run();
}
return ;
}

运行结果和上次一样:

Directx11学习笔记【二】 将HelloWin封装成类