📄 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 + -