![]() |
![]() |
![]() |
| //=============================================================== // first.h 文件 // 避免头文件的重复包含 #ifndef _CHUYUNFENG_FIRST_H_ #define _CHUYUNFENG_FIRST_H_ // 计算需要处理的消息数目 #define MSG_NUM(x) (sizeof(x) / sizeof(x[0])) // 整个程序的消息映射结构 typedef struct tagMSG_MAP_MAIN { UINT nMsg; LRESULT (*pMsgProcess)(HWND, UINT, WPARAM, LPARAM); }MSG_MAP_MAIN_S; // 命令消息(WM_COMMAND)的消息映射结构,因为两者处理函数 // 的参数不同,调用场合也不同,因此这里需要单独建立,类似的有 // WM_NOTOFY消息,本程序中不涉及 typedef struct tagMSG_MAP_CMD { UINT nMsg; LRESULT (*pMsgProcess)(HWND, WORD, HWND, WORD); }MSG_MAP_CMD_S; // 声明程序中用到的全局变量 extern const TCHAR g_szAppName[]; extern HINSTANCE g_hInstance; extern HWND g_hMainWnd; extern const MSG_MAP_MAIN_S g_mainMsg[]; extern const MSG_MAP_CMD_S g_cmdMsg[]; // 窗口处理函数原型 LRESULT CALLBACK mainWndProc(HWND, UINT, WPARAM, LPARAM); // 需要处理的主框架消息定义函数 LRESULT onCreate(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); LRESULT onPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); LRESULT onCommand(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); LRESULT onDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); // 需要处理的菜单命令消息定义函数 LRESULT onAbout(HWND hWnd, WORD idItem, HWND hwndCtl,WORD wNotifyCode); LRESULT onExit(HWND hWnd, WORD idItem, HWND hwndCtl,WORD wNotifyCode); #endif //_CHUYUNFENG_FIRST_H_ //================================================================= // First.cpp 文件 #include #include #include #include "resource.h" #include "First.h" // 定义程序中用到的全局变量 const TCHAR g_szAppName[] = _T("First"); HINSTANCE g_hInstance; HWND g_hMainWnd; // 程序中需要处理的消息映射,如果要增加,在此处增加一 // 对{msgID,onMsgProcFun},然后写对应的消息处理函数即可。 const MSG_MAP_MAIN_S g_mainMsg[] = { {WM_PAINT, onPaint}, {WM_COMMAND, onCommand}, {WM_CREATE, onCreate}, {WM_DESTROY, onDestroy} }; //此处放置需要处理的命令消息 const MSG_MAP_CMD_S g_cmdMsg[] = { {IDM_ABOUT, onAbout}, {IDM_EXIT,onExit} }; //================================================================= // WinMain,入口函数,由操作系统调用 //================================================================= int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; MSG msg; // 注册窗口类 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 = g_szAppName; //窗口类的名字 if ( 0 == RegisterClass (&wc)) return 0; //注册窗口类失败 // 建立并显示程序主窗口 g_hInstance = hInstance; g_hMainWnd = CreateWindow (g_szAppName, // 窗口类 _T("第一个演示程序"), //窗口标题 WS_VISIBLE, //样式 CW_USEDEFAULT, // x坐标 CW_USEDEFAULT, // y 坐标 CW_USEDEFAULT, // 初始宽度 CW_USEDEFAULT, // 初始高度 NULL, // 父窗口 NULL, //菜单,必须为NULL,WINCE窗口不支持菜单。 hInstance, // 实例 NULL); //建立参数的指针,用于WM_CRATE消息期间。 if ( !IsWindow (g_hMainWnd)) //建立窗口失败 return 0; ShowWindow (g_hMainWnd, nCmdShow); UpdateWindow (g_hMainWnd); // 消息循环 while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // WinMain函数成功则返回退出消息的wParam return msg.wParam; } //====================================================================== // MainWndProc,窗口过程,回调函数,由操作系统调用 //====================================================================== LRESULT CALLBACK mainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { // 处理我们需要处理的消息 int i; for (i = 0; i < MSG_NUM(g_mainMsg); i++) { if (wMsg == g_mainMsg[i].nMsg) return (*g_mainMsg[i].pMsgProcess)(hWnd, wMsg, wParam, lParam); } // 有编写对应的函数则调用默认的 return DefWindowProc (hWnd, wMsg, wParam, lParam); } //================================================================= // onCreate,WM_CREATE 消息的处理函数 //================================================================= LRESULT onCreate(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { SHMENUBARINFO mbi; // 初始化menubar结构 memset(&mbi, 0, sizeof(SHMENUBARINFO)); mbi.cbSize = sizeof(SHMENUBARINFO); // 必须填充 mbi.hwndParent = hWnd; mbi.nToolBarId = IDR_MAIN_MENUBAR; mbi.hInstRes = g_hInstance; // 建立menubar控制 if (!SHCreateMenuBar(&mbi)) { MessageBox (hWnd, _T("建立menubar失败"), g_szAppName, MB_OK); DestroyWindow(hWnd); } // return 0; } //================================================================= // onPaint,WM_PAINT 消息的处理函数 //================================================================= LRESULT onPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; RECT rtClient; GetClientRect (hWnd, &rtClient); // 开始绘图 hdc = BeginPaint (hWnd, &ps); // 在屏幕中间写一句话 DrawText (hdc, _T("Smartphone第一个程序^_^"), -1, &rtClient, DT_CENTER | DT_VCENTER | DT_SINGLELINE); // 结束绘图 EndPaint (hWnd, &ps); return 0; } //================================================================= // onCommand,WM_COMMAND 消息的处理函数 //================================================================= LRESULT onCommand(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { //处理我们需要处理的命令消息 WORD wID, wNotifyCode; HWND hwndCtl; int i; // 解析出参数 wID = (WORD) LOWORD (wParam); wNotifyCode = (WORD) HIWORD (wParam); hwndCtl = (HWND) lParam; // 寻找消息映射结构,调用对应的消息处理函数 for (i = 0; i < MSG_NUM(g_cmdMsg); i++) { if (wID == g_cmdMsg[i].nMsg) return (*g_cmdMsg[i].pMsgProcess)( hWnd, wID, hwndCtl,wNotifyCode); } return 0; } //================================================================= // onDestroy,WM_DESTROY 消息的处理函数 //================================================================= LRESULT onDestroy (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { DestroyWindow(hWnd); return 0; } //================================================================= // onAbout,菜单消息IDM_ABOUT处理函数 //================================================================= LRESULT onAbout(HWND hWnd, WORD idItem, HWND hwndCtl,WORD wNotifyCode) { TCHAR szAboutMsg[] = _T(" First for smartphone 2003"); MessageBox(hWnd,szAboutMsg,_T("关于First"),MB_OK); return 0; } //================================================================= // onExit,菜单消息IDM_Exit处理函数 //================================================================= LRESULT onExit(HWND hWnd, WORD idItem, HWND hwndCtl,WORD wNotifyCode) { if ( IDOK == MessageBox(hWnd, _T("真的要退出这个程序?"), _T("退出确认"),MB_OKCANCEL | MB_ICONASTERISK) ) { PostQuitMessage(0); } return 0; } |
![]() |
![]() |
| #ifndef __NEWRES_H__ #define __NEWRES_H__ #if !defined(UNDER_CE) #define UNDER_CE _WIN32_WCE #endif #if defined(_WIN32_WCE) #if !defined(WCEOLE_ENABLE_DIALOGEX) #define DIALOGEX DIALOG DISCARDABLE #endif #include #define SHMENUBAR RCDATA #if defined(WIN32_PLATFORM_WFSP) && (_WIN32_WCE >= 300) #include #define AFXCE_IDR_SCRATCH_SHMENU 28700 #else #define I_IMAGENONE (-2) #define NOMENU 0xFFFF #define IDS_SHNEW 1 #define IDM_SHAREDNEW 10 #define IDM_SHAREDNEWDEFAULT 11 #endif // _WIN32_WCE_PSPC #define AFXCE_IDD_SAVEMODIFIEDDLG 28701 #endif // _WIN32_WCE #ifdef RC_INVOKED #ifndef _INC_WINDOWS #define _INC_WINDOWS #include "winuser.h" // extract from windows header #include "winver.h" #endif #endif #ifdef IDC_STATIC #undef IDC_STATIC #endif #define IDC_STATIC (-1) #endif //__NEWRES_H__ |
| 类型 | ID | Caption |
| Menubar | IDR_MAIN_MENUBAR | |
| Left menu | IDM_ABOUT | 关于 |
| Right menu | IDM_OPTION | 选项 |
| Right menu subitem 1 | IDM_ABOUT | 关于(和左建菜单是同一个ID) |
| Right menu subitem 2 | IDM_EXIT | 退出 |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-126870/ ,如需转载,请注明出处,否则将追究法律责任。