Windows内部运行机制,一直是有个大略的了解,主要是了解了这几个方面:
一、Windows窗口的设计、注册、创建、显示更新;
二、消息循环;了解windows消息的基本情况,还是要把代码敲一下,有个初步的肌肤之亲。
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinTianProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_CROSS);
wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WinTianProc;
wndclass.lpszClassName = "tian";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd = CreateWindow("tian","abc",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
BOOL bRet;
MSG msg;
while(bRet = GetMessage(&msg,NULL,0,0))
{
if (-1 == bRet)
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK WinTianProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wparam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,0,9,"程序员之家",strlen("程序员之家"));
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd,&ps);
TextOut(hDC,0,50,"abc",strlen("abc"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wparam,lparam);
}
return 0;
}