句柄 HANDLE
图标句柄HICON
光标句柄HCURSOR
窗口句柄HWND
应用程序实例句柄HINSTANCE
//消息队列的消息结构体
This structure contains message information from a thread's message queue.
typedef struct tagMSG {
HWND hwnd; //句柄
UINT message; //Unsigned Int 以WM_开头的宏作为消息的类型
WPARAM wParam; //整数, 指示消息的附加信息
LPARAM lParam; //整数, 指示消息的附加信息
DWORD time; //32位整数, 指示消息传递的时间
POINT pt; //消息传递的时候当前光标的位置
} MSG;
消息传递的宏, 以WM_开头
WM_message
WM_KEYDOWN
WM_LBUTTONDOWN
按下A键, 收到WM_CHAR消息, (message), 通过wParam 获取A键的ASCII码.
typedef struct tagPOINT {
LONG x;
LONG y;
} POINT;
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //commmand line
int nCmdShow //show state 程序的现实状态.
);
LPSTR LP 表示LONG POINT 长指针, 指向字符串的长指针.
构建一个完整的窗口需要的四个操作步骤
设计一个窗口类
注册窗口类
创建窗口
显示及更新窗口
窗口类
typedef struct {
UINT style; // 类的类型, 很多.
WNDPROC lpfnWndProc; //接收函数指针, 回调函数.
int cbClsExtra;//额外参数, 通常为0
int cbWndExtra;//额外参数, 通常为0
HINSTANCE hInstance;//当前实例
HICON hIcon;//图标句柄使用LoadIcon()
HCURSOR hCursor;//光标句柄LoadCursor()
HBRUSH hbrBackground;//画刷句柄, 使用(HBRUSH)GetStockObject()
LPCTSTR lpszMenuName;//长指针指向常量字符串, 设置菜单的名字.
LPCTSTR lpszClassName;//长指针指向常量字符串, 窗口的名字.
} WNDCLASS, *PWNDCLASS;
注册窗口RegisterClass(&wndclass)
创建窗口
HWND hwnd;
HWND CreateWindow(
LPCTSTR lpClassName, //注册的窗口类的类名, 即lpszClassName
LPCTSTR lpWindowName,//窗口的名字, 即窗口的标题
DWORD dwStyle, // 窗口的类型.
int x, // 窗口的x坐标, CW_USEDEFAULT
int y, // 窗口的y坐标
int nWidth, //窗口的宽度 CW_USEDEFAULT
int nHeight, //窗口的高度
HWND hWndParent, // 指示父窗口的句柄
HMENU hMenu, //菜单的句柄
HINSTANCE hInstance, //当前应用程序的句柄
LPVOID lpParam // 作为WM_CREATE的参数
);
显示窗口及更新窗口
ShowWindow(hwnd, SW_SHOWNORMAL)
UpdateWindow(hwnd)
消息循环!!!
BOOL GetMessage(
LPMSG lpMsg, // 消息
HWND hWnd, // 获取哪一个窗口的信息, NULL 为调用线程的任何窗口的信息
UINT wMsgFilterMin, // 最小的消息值
UINT wMsgFilterMax // 最大的消息值
);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)
{
TranslateMessage(&msg); //转换消息
DispatchMessage(&msg);//将消息交给回调函数处理
}
回调函数 , 窗口过程函数
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,
}
}
int MessageBox(
HWND hWnd, //拥有该消息的窗口句柄
LPCTSTR lpText, // 显示的文本
LPCTSTR lpCaption, // 标题
UINT uType //消息窗口类型
);
// 设备上下本句柄, DC句柄
HDC hDC; // DC Device Context
hDC = GetDC(hwnd);
TextOut(hDC, 0, 50, "Hello World.", strlen("Hello World."));
ReleaseDC(hwnd, hDC); // 要释放, 不然会内存泄露.
break;
WM_PAINT
窗口重绘的处理
hdc = BeginPaint(hwnd, &ps);//只能在WM_PAINT使用
TextOut(hdc, 0, 0, "Hello", strlen("Hello"));
EndPaint(hwnd, &ps);//只能在WM_PAINT使用
break;
WM_CLOSE
if(IDYES == MessageBox(hwnd, "Exit ?", "Notice", MB_YESNO)
{
DestroyWindow(hwnd); //销毁窗口
}
break;
WM_DESTROY
PostQuitMessage(0);
break;
default
DefWindowProc(hwnd, uMsg, wParam, lParam);
CALLBACK __stdcall
WINAPI __stdcall
WINAPIV __cdecl // VC默认使用此项
#include
#include
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
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 = WinSunProc;
wndclass.lpszClassName = "Xiaodong";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd = CreateWindow("Xiaodong", "DongDongWindow", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar, "char is %d", wParam);
MessageBox(hwnd, szChar, "Xiaodong...", 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked...", "XiaoDong...", 0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 0, 50, "你好啊...", strlen("你好啊..."));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);
TextOut(hDC, 0, 0, "练习窗口...", strlen("练习窗口..."));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd, "Exit Yes OR No ?", "Notice", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}