?? 123.txt
字號:
注:技術上,Windows CE應用程序的每個線程都有一個消息隊列。稍后我將在本書里討論線程。
Hello3
回顧的夠多了,是時候做一個完整的Windows 應用程序--Hello3了。雖然Hello3的整個程序文件以及書中全部例子都可以在附書光盤里找到,但我還是建議,對于初期的例子,您應當避免簡單的從CD上裝載工程文件,而是應該手工輸入整個例子。通過這種略微有些枯燥的工作,你會體會到標準Win32程序與Windows CE程序之間在開發過程的不同以及在程序上的細微差別。清單1-3給出了Hello3的全部源代碼。
清單1-3:程序Hello3
Hello3.cpp
//======================================================================
// Hello3 - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hWnd;
MSG msg;
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = TEXT("MyClass"); // Window class name
if (RegisterClass (&wc) == 0) return -1;
// Create main window.
hWnd = CreateWindowEx(WS_EX_NODRAG, // Ex style flags
TEXT("MyClass"), // Window class
TEXT("Hello"), // Window title
// Style flags
WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
if (!IsWindow (hWnd)) return -2; // Fail code if not created.
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return msg.wParam;
}
//======================================================================
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
switch (wMsg) {
case WM_PAINT:
// Get the size of the client rectangle
GetClientRect (hWnd, &rect);
hdc = BeginPaint (hWnd, &ps);
DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint (hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
break;
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
Hello3展示了Windows程序的各個方面,從注冊窗口類到創建窗口及窗口過程。和頭兩個例子一樣,Hello3有著相同的入口點--WinMain。但是因為Hello3創建了自己的窗口,所以它必須為主窗口注冊一個窗口類,創建窗口并且提供一個消息循環來為窗口處理消息。
注冊窗口類
在WinMain中,Hello3為主窗口注冊了窗口類。注冊一個窗口類只是簡單的填充一個描述窗口類的有些大的結構并調用RegisterClass函數。RegisterClass和WNDCLASS結構定義如下:
ATOM RegisterClass (const WNDCLASS *lpWndClass);
typedef struct _WNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HANDLE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS;
給WNDCLASS結構各個域賦的值為Hello3主窗口的所有實例定義了行為表現。style域為窗口設置了類的風格。在Windows CE中,類風格被限制為:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -