Data type | Size | Signed? |
---|---|---|
BYTE | 8 bits | Unsigned |
DWORD | 32 bits | Unsigned |
INT32 | 32 bits | Signed |
INT64 | 64 bits | Signed |
LONG | 32 bits | Signed |
LONGLONG | 64 bits | Signed |
UINT32 | 32 bits | Unsigned |
UINT64 | 64 bits | Unsigned |
ULONG | 32 bits | Unsigned |
ULONGLONG | 64 bits | Unsigned |
WORD | 16 bits | Unsigned |
WinDef.h中定义了BOOL值
#define FALSE 0 #define TRUE 1
头文件WinNT.h定义了如下typedef (基于UTF-16编码,即是16bit),为了支持Unicode
typedef wchar_t WCHAR;
wchar_t a = L'a'; wchar_t *str = L"hello";其他一些相关的typedef
Typedef | Definition |
---|---|
CHAR | char |
PSTR or LPSTR | char* |
PCSTR or LPCSTR | const char* |
PWSTR or LPWSTR | wchar_t* |
PCWSTR or LPCWSTR | const wchar_t* |
#ifdef UNICODE #define SetWindowText SetWindowTextW #else #define SetWindowText SetWindowTextA #endif通过宏UNICODE来控制
Macro | Unicode | ANSI |
---|---|---|
TCHAR | wchar_t |
char |
TEXT("x") | L"x" |
"x" |
SetWindowTextW(L"My Application"); // Unicode function with wide-character string. SetWindowTextA("My Application"); // ANSI function.
#ifdef _UNICODE #define _tcslen wcslen #else #define _tcslen strlen #endif
BOOL MoveWindow(HWND hWnd, int X, int Y, int nWidth, int nHeight, BOOL bRepaint);另外句柄不是指针,不存在
*hwnd
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);hInstance:标识executable
// Register the window class. const wchar_t CLASS_NAME[] = L"Sample Window Class"; WNDCLASS wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME;lpfnWndProc:窗口过程,定义了绝多部分的动作
RegisterClass(&wc);(2)创建窗口新实例
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
第一个参数:
Optional window styles.
Window class
Window text窗口标题,如果窗口存在标题栏则显示在其中。
Window style
窗口的风格,定义了一些窗口外观和表现的标志组合,WS_OVERLAPPEDWINDO是几个标志结合的位或,包含最小化,最大化按钮,边框,标题栏等等
第五个参数:
Size and position
位置和大小 CW_USEDEFAULT 使用默认值
第六个参数:
Parent window
父窗口。
第七个参数:
Menu
菜单
第八个参数:
Instance handle
实例句柄
第九个参数:
Additional application data
指向void*的任意数据结构,传递数据所用
最后CreateWindowEx 返回句柄创建新窗口,如果失败返回0.
(3)显示窗口
要显示窗口则将窗口句柄传递给ShowWindow函数
ShowWindow(hwnd, nCmdShow);
hwnd 参数来自 CreateWindowExnCmdShow参数用于最大化或者最小化窗口。操作系统将通过wWinMain函数传递该参数给程序
(4)窗口消息
窗口消息
MSG msg;GetMessage(&msg, NULL, 0, 0);
提取消息使用以下两个函数
TranslateMessage(&msg); DispatchMessage(&msg);
退出提取消息循环
退出提取消息循环
PostQuitMessage(0);
// Correct. MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);hwnd:窗口句柄
switch (uMsg) { case WM_SIZE: // Handle window resizing // etc }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_SIZE: { int width = LOWORD(lParam); // Macro to get the low-order word. int height = HIWORD(lParam); // Macro to get the high-order word. // Respond to the message: OnSize(hwnd, (UINT)wParam, width, height); } break; } } void OnSize(HWND hwnd, UINT flag, int width, int height); { // Handle resizing }假如不处理窗口过程特定的消息,可以直接返回默认的DefWindowProc函数,
return DefWindowProc(hwnd, uMsg, wParam, lParam);hui
switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // All painting occurs here, between BeginPaint and EndPaint. FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1)); EndPaint(hwnd, &ps); } return 0; }
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
#ifndef UNICODE
#define UNICODE
#endif
#include
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
//Register the window class
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
//Create the window.
HWND hwnd = CreateWindowEx(
0, //Optional window styles.
CLASS_NAME, //Window class
L"Learn to Program Windows", //Window text
WS_OVERLAPPEDWINDOW, //Window style
//Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, //Parent window
NULL, //Menu
hInstance, //Instance handle
NULL //Additional application data
);
if(hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
//Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}