3.5 暗示窗心。

        建立了窗心以后,何如要念运转时可以或许瞥见窗心,咱们借需求隐式的设施示意窗心:

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
登录后复造

ShowWindow以及UpdateWindow皆是体系的API,前者是配备窗心的透露表现属性。后者从新更新窗心属性,只需更新以后,属性才会见效。颠末了上述步伐,咱们的WinMain函数曾经酿成了如高:

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR    lpCmdLine,
_In_ int       nCmdShow)
{
    //注册窗心类
    WNDCLASSEX wce = { 0 };
    wce.cbSize = sizeof(wce);
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    wce.hIconSm = NULL;
    wce.hInstance = hInstance;
    wce.lpfnWndProc = wndProc;
    wce.lpszClassName = “Main”;
    wce.lpszMenuName = NULL;
    wce.style = CS_HREDRAW | CS_VREDRAW;
    ATOM nAtom = RegisterClassEx(&wce);
    if (!nAtom )
    {
        MessageBox(NULL, "注册掉败", "Infor", MB_OK);
        return 0;
    }
    
    //建立窗心 
    HWND hWnd = CreateWindowEx(0, “Main”, “Window”, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
    //透露表现窗心 
   ShowWindow(hWnd, SW_SHOW); 
   UpdateWindow(hWnd); 
}
登录后复造

3.6 封闭动态轮回

所谓的动静轮回,其真等于一个逝世轮回,不停的经由过程GetMessage捕获着窗心的动静,再经由过程TranslateMessage将假造键动静转换为字符动静,而后经由过程DispatchMessage将动静分拨给咱们的窗心程序入止处置惩罚(所谓处置惩罚便是挪用咱们界说的窗心处置惩罚函数WinProc):

void Message()
{
    MSG nMsg = { 0 };
    while (GetMessage(&nMsg, NULL, 0, 0))
    {
        TranslateMessage(&nMsg);
        DispatchMessage(&nMsg);
    }
}
登录后复造

实现了动静轮回以后,咱们终极的代码如高(那内中对于窗心注册的程序入止了启拆,然则历程是一致的):

#include<windows.h>
HINSTANCE g_hInstance = 0;
//窗心措置函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);//可使GetMessage返归0
		break;
	default:
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//注册窗心类
BOOL Register(LPSTR lpClassName, WNDPROC wndProc)
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize = sizeof(wce);
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wce.hCursor = NULL;
	wce.hIcon = NULL;
	wce.hIconSm = NULL;
	wce.hInstance = g_hInstance;
	wce.lpfnWndProc = wndProc;
	wce.lpszClassName = lpClassName;
	wce.lpszMenuName = NULL;
	wce.style = CS_HREDRAW | CS_VREDRAW;
	ATOM nAtom = RegisterClassEx(&wce);
	if (nAtom == 0)
		return FALSE;
	return true;

}
//创立主窗心
HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName)
{
	HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName,
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
	return hWnd;
}
//透露表现窗心
void Display(HWND hWnd)
{
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
}
//动静轮回
void Message()
{
	MSG nMsg = { 0 };
	while (GetMessage(&nMsg, NULL, 0, 0))
	{
		TranslateMessage(&nMsg);
		DispatchMessage(&nMsg);
	}
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPWSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	// TODO: Place code here.

	g_hInstance = hInstance;
	BOOL nRet = Register("Main", WndProc);
	if (!nRet)
	{
		MessageBox(NULL, "注册掉败", "Infor", MB_OK);
		return 0;
	}
	HWND hWnd = CreateMain("Main", "window");
	Display(hWnd);
	Message();
	return 0;
}
登录后复造

    便此咱们实现了窗心措置的零个历程,运转程序,顺遂建立并默示的一个Windows窗心:


以上即是Win3二 SDK根蒂(四)之如果从整入手下手建立windows窗心的第两步的具体形式,更多请存眷萤水红IT仄台别的相闭文章!

点赞(20) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部