接上一篇:
- case WM_CREATE:
- {
- HMENU hMenu, hSubMenu;
- HICON hIcon, hIconSm;
- hMenu = CreateMenu();//创建主菜单
- hSubMenu = CreatePopupMenu();//创建弹出菜单
- AppendMenu(hSubMenu,MF_STRING,IDM_FILE_OPEN,TEXT("打开(&O)"));//打开菜单
- AppendMenu(hSubMenu,MF_SEPARATOR,0,NULL);//分隔线
- AppendMenu(hSubMenu, MF_STRING, IDM_FILE_EXIT, TEXT("退出(&X)"));//退出菜单
- AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, TEXT("文件(&F)"));//加入File菜单
- hSubMenu = CreatePopupMenu();
- AppendMenu(hSubMenu, MF_STRING, IDM_EDIT_UNDO, TEXT("撤销(&U)"));
- AppendMenu(hSubMenu,MF_STRING,IDM_EDIT_REDO,TEXT("重做(&R)"));
- AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, TEXT("编辑(&E)"));//加入编辑菜单
- SetMenu(hWnd, hMenu);//设置为窗口的主菜单
- hIcon = (HICON)LoadImage(NULL, TEXT("tao.ico"), IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
- if(hIcon)
- SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
- else
- MessageBox(hWnd, TEXT("不能加载大图标!"), TEXT("错误"), MB_OK | MB_ICONERROR);
- hIconSm = (HICON)LoadImage(NULL, TEXT("tao.ico"), IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
- if(hIconSm)
- SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
- else
- MessageBox(hWnd, TEXT("不能加载小图标!"), TEXT("错误"), MB_OK | MB_ICONERROR);
- return 0;
- }
当然也可以在窗口创建时动态设置菜单,而不是加载资源菜单。
这里同时修改了窗口的图标,通过WM_SETICON消息。
有了菜单,但是现在单击没有什么反应,我们还需要处理WM_COMMAND窗体通告消息。
- The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.
对应菜单来说,wParam和lParam这2个参数具体说明如下:
wParam这个32位参数的高位字(高16位)为0,低位字存储的菜单的标识符IDM_*那些。lParam这个参数为0.
我们只需要处理WM_COMMAND,根据标识符不同处理每个菜单的行为。
- case WM_COMMAND:
- {
- switch(LOWORD(wParam))
- {
- case IDM_FILE_EXIT:
- PostMessage(hWnd,WM_CLOSE,0,0);
- break;
- case IDM_FILE_OPEN:
- OPENFILENAME ofn;
- TCHAR szFileName[256];
- TCHAR title[50];
- ZeroMemory(&ofn,sizeof(ofn));
- ofn.lStructSize=sizeof(ofn);
- ofn.hwndOwner=hWnd;//Owner窗口
- ofn.lpstrFile=szFileName;//接收文件名的buffer
- ofn.lpstrFile[0] = '\0';
- ofn.nMaxFile = sizeof(szFileName);
- ofn.lpstrFilter = TEXT("所有文件 (*.*)\0*.*\0文本文件 (*.txt)\0*.TXT\0图片文件 (*.bmp;*.jpg;*.png;*.gif)\0*.bmp;*.jpg;*.png;*.gif\0\0");
- ofn.nFilterIndex = 2;/*默认显示过滤器:图片文件的*/
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- ofn.lpstrTitle=TEXT("打开一个文件吧");//设置对话框的标题
- if(GetOpenFileName(&ofn)==TRUE)
- {
- MessageBox(hWnd,szFileName,TEXT("文件名"),0);
- }
- break;
- }
- return 0;
- }
详细的OPENFILENAME结构体,参考MSDN.
完整代码:
- #include <windows.h>
- #include "resource.h"
- LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
- int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
- {
- WNDCLASSEX wClass;
- ZeroMemory(&wClass,sizeof(WNDCLASSEX));
- wClass.cbClsExtra=NULL;
- wClass.cbSize=sizeof(WNDCLASSEX);
- wClass.cbWndExtra=NULL;
- wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
- wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wClass.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_MYICON));
- wClass.hIconSm=(HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
- wClass.hInstance=hInst;
- wClass.lpfnWndProc=(WNDPROC)WinProc;
- wClass.lpszClassName=TEXT("窗口类名");
- wClass.lpszMenuName=NULL;/*MAKEINTRESOURCE(IDR_MYMENU)*/
- wClass.style=CS_HREDRAW|CS_VREDRAW;
- if(!RegisterClassEx(&wClass))
- {
- int nResult=GetLastError();
- MessageBox(NULL,
- TEXT("注册窗口类失败."),
- TEXT("失败"),
- MB_ICONERROR);
- }
- //HMENU hMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDR_CNMENU));
- HWND hWnd=CreateWindowEx(NULL,
- TEXT("窗口类名"),
- TEXT("win32应用程序"),
- WS_OVERLAPPEDWINDOW,
- 200,
- 200,
- 640,
- 480,
- NULL,
- NULL,/*hMenu*/
- hInst,
- NULL);
- if(!hWnd)
- {
- int nResult=GetLastError();
- MessageBox(NULL,
- TEXT("窗口创建失败."),
- TEXT("失败"),
- MB_ICONERROR);
- }
- ShowWindow(hWnd,nShowCmd);
- MSG msg;
- ZeroMemory(&msg,sizeof(MSG));/*清零*/
- while(GetMessage(&msg,NULL,0,0) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- switch(msg)
- {
- case WM_CREATE:
- {
- HMENU hMenu, hSubMenu;
- HICON hIcon, hIconSm;
- hMenu = CreateMenu();//创建主菜单
- hSubMenu = CreatePopupMenu();//创建弹出菜单
- AppendMenu(hSubMenu,MF_STRING,IDM_FILE_OPEN,TEXT("打开(&O)"));//打开菜单
- AppendMenu(hSubMenu,MF_SEPARATOR,0,NULL);//分隔线
- AppendMenu(hSubMenu, MF_STRING, IDM_FILE_EXIT, TEXT("退出(&X)"));//退出菜单
- AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, TEXT("文件(&F)"));//加入File菜单
- hSubMenu = CreatePopupMenu();
- AppendMenu(hSubMenu, MF_STRING, IDM_EDIT_UNDO, TEXT("撤销(&U)"));
- AppendMenu(hSubMenu,MF_STRING,IDM_EDIT_REDO,TEXT("重做(&R)"));
- AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, TEXT("编辑(&E)"));//加入编辑菜单
- SetMenu(hWnd, hMenu);//设置为窗口的主菜单
- hIcon = (HICON)LoadImage(NULL, TEXT("tao.ico"), IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
- if(hIcon)
- SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
- else
- MessageBox(hWnd, TEXT("不能加载大图标!"), TEXT("错误"), MB_OK | MB_ICONERROR);
- hIconSm = (HICON)LoadImage(NULL, TEXT("tao.ico"), IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
- if(hIconSm)
- SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
- else
- MessageBox(hWnd, TEXT("不能加载小图标!"), TEXT("错误"), MB_OK | MB_ICONERROR);
- return 0;
- }
- case WM_COMMAND:
- {
- switch(LOWORD(wParam))
- {
- case IDM_FILE_EXIT:
- PostMessage(hWnd,WM_CLOSE,0,0);
- break;
- case IDM_FILE_OPEN:
- OPENFILENAME ofn;
- TCHAR szFileName[256];
- TCHAR title[50];
- ZeroMemory(&ofn,sizeof(ofn));
- ofn.lStructSize=sizeof(ofn);
- ofn.hwndOwner=hWnd;//Owner窗口
- ofn.lpstrFile=szFileName;//接收文件名的buffer
- ofn.lpstrFile[0] = '\0';
- ofn.nMaxFile = sizeof(szFileName);
- ofn.lpstrFilter = TEXT("所有文件 (*.*)\0*.*\0文本文件 (*.txt)\0*.TXT\0图片文件 (*.bmp;*.jpg;*.png;*.gif)\0*.bmp;*.jpg;*.png;*.gif\0\0");
- ofn.nFilterIndex = 2;/*默认显示过滤器:图片文件的*/
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- ofn.lpstrTitle=TEXT("打开一个文件吧");//设置对话框的标题
- if(GetOpenFileName(&ofn)==TRUE)
- {
- MessageBox(hWnd,szFileName,TEXT("文件名"),0);
- }
- break;
- }
- return 0;
- }
- case WM_LBUTTONDOWN:
- {
- TCHAR szFileName[MAX_PATH];
- HINSTANCE hInstance = GetModuleHandle(NULL);
- GetModuleFileName(hInstance, szFileName, MAX_PATH);
- MessageBox(hWnd, szFileName, TEXT("此程序是:"), MB_OK | MB_ICONINFORMATION);
- return 0;
- }
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- }
- break;
- }
- return DefWindowProc(hWnd,msg,wParam,lParam);
- }
注:不足,字符串都是硬编码的。
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/754758,如需转载请自行联系原作者