//====================================================================== // Header file:helloce.h //====================================================================== // 返回元素的数量,主要用于搜索消息列表 #define dim(x) (sizeof(x) / sizeof(x[0])) //---------------------------------------------------------------------- //数据类型定义 // struct decodeUINT { //消息和消息函数的关联结构 UINT Code; LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM); //这里用到了函数指针 }; struct decodeCMD { //菜单和处理函数的关联结构 UINT Code; LRESULT (*Fxn)(HWND, WORD, HWND, WORD); //这里用到了函数指针 };
//---------------------------------------------------------------------- #define IDC_CMDBAR 1 // 命令条ID
//---------------------------------------------------------------------- // 函数原型 // int InitApp (HINSTANCE); //初始化应用函数原型 HWND InitInstance (HINSTANCE, LPWSTR, int); //初始化实例函数原型 int TermInstance (HINSTANCE, int); //实例终止函数原型 // 窗口处理函数原型 LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM); // 消息句柄 LRESULT DoCreateMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoHibernateMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoActivateMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);
//====================================================================== // HelloCE - helloce.c //====================================================================== #include <windows.h> #include <commctrl.h> #include "helloce.h" //---------------------------------------------------------------------- // 全局数据 // const TCHAR szAppName[] = TEXT ("HelloCE"); HINSTANCE hInst; // 程序的实例句柄 //主窗口过程函数的消息映射表用到decodeUINT结构 const struct decodeUINT MainMessages[] = { WM_CREATE, DoCreateMain, WM_PAINT, DoPaintMain, WM_HIBERNATE, DoHibernateMain, WM_ACTIVATE, DoActivateMain, WM_DESTROY, DoDestroyMain, };
//====================================================================== // 程序的入口 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; int rc = 0; HWND hwndMain; // 初始应用 rc = InitApp (hInstance); if (rc) return rc; // 初始化实例 hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndMain == 0) return 0x10; // 应用程序消息循环 while (GetMessage (&;msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } //实例清除 return TermInstance (hInstance, msg.wParam); } //---------------------------------------------------------------------- // 应用程序初始化函数 // int InitApp (HINSTANCE hInstance) { WNDCLASS wc; //注册应用程序的主窗口类 wc.style = 0; // 窗口样式 wc.lpfnWndProc = MainWndProc; // 回调函数 wc.cbClsExtra = 0; // 扩展的类数据 wc.cbWndExtra = 0; // 扩展的窗口数据 wc.hInstance = hInstance; //实例句柄 wc.hIcon = NULL, // 图标 wc.hCursor = NULL; // 鼠标 wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; //菜单 wc.lpszClassName = szAppName; //窗口类的名字
if (RegisterClass (&wc) == 0) return 1; return 0; } //---------------------------------------------------------------------- //初始化实例 // HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { HWND hWnd; // 存储程序实例句柄到全局变量 hInst = hInstance; // 建立主窗口 hWnd = CreateWindow (szAppName, // 窗口类 TEXT("你好蜥蜴"), //窗口标题 WS_VISIBLE, //样式 CW_USEDEFAULT, // x坐标 CW_USEDEFAULT, // y 坐标 CW_USEDEFAULT, // 初始宽度 CW_USEDEFAULT, // 初始高度 NULL, // 父窗口 NULL, //菜单,必须为NULL,WINCE窗口不支持菜单。 hInstance, // 实例 NULL); //建立参数的指针,用于WM_CRATE消息期间。 // 如果不能建立主窗口返回失败 if (!IsWindow (hWnd)) return 0; // 显示和更新窗口函数 ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd; } //---------------------------------------------------------------------- // TermInstance -程序清除 // int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC; } //====================================================================== // 下面是主窗口的消息处理函数 // //---------------------------------------------------------------------- // MainWndProc - 主过程函数,这是一个回调函数 // LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; //搜索消息列表,如果编写了对应的函数来处理这个消息则调用这个函数 for (i = 0; i < dim(MainMessages); i++) { if (wMsg == MainMessages[i].Code) return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); //没有编写对应的函数则调用默认的 } //---------------------------------------------------------------------- // DoCreateMain - 处理窗口建立(WM_CREATE)消息的函数. // LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HWND hwndCB; // 建立命令条. hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR); // 添加退出按钮到命令条上 CommandBar_AddAdornments (hwndCB, 0, 0); return 0; } //---------------------------------------------------------------------- // DoPaintMain - 处理窗口重画(WM_PAINT)消息的函数 // LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rect; HDC hdc; // 调整客户区域的大小并考虑命令条的高度 GetClientRect (hWnd, &rect); rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR)); hdc = BeginPaint (hWnd, &ps); DrawText (hdc, TEXT ("你好晕倒的蜥蜴!"), -1, &rect, //被改成了中文 DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint (hWnd, &ps); return 0; } //---------------------------------------------------------------------- // DoHibernateMain - 处理窗口挂起消息(WM_HIBERNATE)的函数,这是WINCE独有的消息,目的//是将内存的使用量将到最小. // LRESULT DoHibernateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { // 如果窗口不是活动的,则取消命令条,释放内存 if (GetActiveWindow () != hWnd) CommandBar_Destroy (GetDlgItem (hWnd, IDC_CMDBAR)); return 0; } //---------------------------------------------------------------------- // DoActivateMain - 处理窗口激活(WM_ACTIVATE)消息的函数 // LRESULT DoActivateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HWND hwndCB; // 如果窗口正处在活动状态而没有命令条则建立它 if ((LOWORD (wParam) != WA_INACTIVE) && (GetDlgItem (hWnd, IDC_CMDBAR) == 0)) { // 建立命令条 hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR); // 添加退出按钮到命令条 CommandBar_AddAdornments (hwndCB, 0, 0); } return 0; } //---------------------------------------------------------------------- // DoDestroyMain - 处理窗口销毁(WM_DESTROY)消息函数. // LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0; } |