今天正式开始了win32 SDK非模态化的编程,不使用资源添加来写窗口,算是一次新的修炼吧,即使学的有些晚,但是想要更加详细的了解windows各种消息机制还得从最基本的API了解,这对以后无论是何种编程,对MFC的封装使用都会有更大的帮助,下面是一个非模态的窗口建立。
#include "windows.h"
HINSTANCE g_Instance;
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wapram,LPARAM lparam)
{
switch(uMsg)
{
case WM_DESTROY:{
PostQuitMessage(0);
}break;
}
return DefWindowProc(hwnd,uMsg,wapram,lparam);
}
BOOL CALLBACK Registe(LPCTSTR pszName)
{
WNDCLASSEX wce;
wce.cbSize = sizeof(wce);
wce.style = CS_HREDRAW|CS_VREDRAW;
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hbrBackground = HBRUSH (COLOR_BTNFACE + 1);
wce.hIconSm = NULL;
wce.hInstance = g_Instance;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszName;
wce.lpszMenuName = NULL;
ATOM nAtom = RegisterClassEx(&wce);
if (nAtom == 0)
{
return FALSE;
}
return TRUE;
}
HWND CALLBACK CreateWnd(LPCTSTR pszName)
{
HWND hwnd = CreateWindow(pszName,L"MyWnd",WS_OVERLAPPEDWINDOW,0,0,400,500,NULL,NULL,g_Instance,NULL);
if (hwnd == NULL)
{
return NULL;
}
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
return hwnd;
}
void MessageInfo(HWND hwnd)
{
MSG msg = {0};
while(GetMessage(&msg,hwnd,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
g_Instance = hInstance;
Registe(L"EDIT");
HWND hwnd = CreateWnd(L"EDIT");
MessageInfo(hwnd);
return 0;
}