0.先说一下如何在win32程序中打开控制台,复制以下代码即可:
#include "io.h" #include "fcntl.h" void InitConsole() { int nRet= 0; FILE* fp; AllocConsole(); // 调用产生控制台的窗口 nRet= _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT); //GetStdHandle 获取控制台的句柄,若没有AllocConsole(),获取标准的输入输出窗口句柄 fp = _fdopen(nRet, "w"); //fdopen()会将nRet的文件描述词,转换为对应的文件指针后返回 *stdout = *fp; setvbuf(stdout, NULL, _IONBF, 0);//输出流 }
下面来介绍一下如何在win32 中实现点击右键弹出菜单,步骤如下:
1:先在头文件中中先定义三个IDM资源,这三个参数很重要,在监听菜单点击事件的时候会用到。
#define IDM_AAA 200 #define IDM_BBB 201 #define IDM_CCC 202
static HMENU hMenu;
3.在Init()方法中添加代码:
hMenu=CreatePopupMenu(); AppendMenu(hMenu,MF_STRING,IDM_AAA,_T("&AAA")); AppendMenu(hMenu,MF_SEPARATOR,0,NULL); AppendMenu(hMenu,MF_STRING,IDM_BBB,_T("&BBB")); AppendMenu(hMenu,MF_STRING,IDM_CCC,_T("&CCC"));
4.在回调函数WndProc的WM_RBUTTONDOWN中添加右击响应代码
case WM_RBUTTONDOWN: { printf("Right"); POINT point; point.x=LOWORD(lParam); point.y=HIWORD(lParam); ClientToScreen(hWnd,&point); TrackPopupMenu(hMenu,TPM_RIGHTALIGN,point.x,point.y,0,hWnd,NULL); break; }
还可以设置该项位选中状态:
CheckMenuItem(hMenu,IDM_AAA,MF_CHECKED);
cout<<wParam<<endl;