#define IDM_EXIT 1001 case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_EXIT:
SendMessage(hwnd,WM_DESTROY,0,0L);
break;
}
break;如上面代码,正常应该是一点这个菜单就退出了,可是现在没有反映。我换成MessageBox(hwnd, "退出", "退出菜单", MB_YESNO );也是没反映。我怀疑是没有接收到消息。请大家帮帮我。谢谢
switch(LOWORD(wParam))
{
case IDM_EXIT:
SendMessage(hwnd,WM_DESTROY,0,0L);
break;
}
break;如上面代码,正常应该是一点这个菜单就退出了,可是现在没有反映。我换成MessageBox(hwnd, "退出", "退出菜单", MB_YESNO );也是没反映。我怀疑是没有接收到消息。请大家帮帮我。谢谢
解决方案 »
- 简单问题 这两个有什么区别?
- unsigned short __stdcall SListen(void *)' : overloaded function differs only by
- 请教:VC中位图充满主窗口的问题!
- jrtp的问题。。
- 如何动态禁用上下文菜单项?
- 编写的ISAPI程序(dll)地址栏里为何能运行而表单提交就报错?详细情况是...
- List Control中如何让里面的文本可以编辑
- 哪位帮帮忙提供bmp图像操作的代码?
- 我的vc还是我有问题?
- float f1=300.1;这句话竟然有警告信息!!
- 我的信誉值又减少了,不知道怎么搞的,没有做什么过分的事情啊
- 超菜问题,将0x00赋给unsigned char
或
PostMessage(hwnd,WM_QUIT,0,0L);老弟,WM_DESTROY不是这样用的!
把你的CPP文件和RS文件贴出来看看吧,大家帮你找找原因~~~``````
#define IDB_A 1000
#define IDM_EXIT 1001(study1.cpp)
#include "stdafx.h"
#include "study1.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);//初始化函数,初始化窗口数据,并注册窗口
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);HWND hwnd ; //主窗口句柄
HWND hwndBotton; //按钮句柄int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
// TODO: Place code here.
MSG msg;
if (!InitApplication(hInstance))
return (FALSE);
if (!InitInstance(hInstance,nCmdShow))
return (FALSE);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
} return msg.wParam ;
}BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL,"IDI_WINICON");
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH);
wndclass.lpszMenuName = "IDR_FRAMEWIND";
wndclass.lpszClassName = "HelloWin";
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"), "HelloWin", MB_ICONERROR);
return 0;
}
return (TRUE);
}BOOL InitInstance( HINSTANCE hInstance,int nCmdShow)
{
hwnd = CreateWindow ("HelloWin",TEXT ("Vwind"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if (!hwnd)
return (FALSE);
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
return (TRUE);
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
//退出菜单
case IDM_EXIT:
MessageBox(hwnd, "wParam", "关闭", MB_YESNO );
break;
}
break;
case WM_DESTROY: //消息:本窗口正将被销毁
//请求退出窗口和应用程序
PostQuitMessage (0);
break;
default:
//调用默认窗口过程对未处理的消息进行必要的处理
return DefWindowProc (hwnd, message, wParam, lParam);
}
return (0);
}程序就是这样的。
估计是id冲突或者什么原因,尽量不要自己定义id
还有#define IDM_EXIT 10001最好改成#define IDM_EXIT WM_USER+10001,以避免跟系统消息冲突
还少了一个#include <windows.h>,
#include "study1.h"
如果没有错误或警告,你的程序应该就可以收到消息了:)
case IDM_EXIT:
DestroyWindow(hWnd);
break;
简单快捷
//(study1.h)//(study1.cpp)
#include "stdafx.h"
//#include "study1.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);#define IDB_A 1000
#define IDM_EXIT 1001
//初始化函数,初始化窗口数据,并注册窗口
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);HWND hwnd ; //主窗口句柄
HWND hwndBotton; //按钮句柄int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
// TODO: Place code here.
MSG msg;
if (!InitApplication(hInstance))
return (FALSE);
if (!InitInstance(hInstance,nCmdShow))
return (FALSE);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
} return msg.wParam ;
}BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL,"IDI_WINICON");
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH);
wndclass.lpszMenuName = "IDR_FRAMEWIND";
wndclass.lpszClassName = "HelloWin";
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"), "HelloWin", MB_ICONERROR);
return 0;
}
return (TRUE);
}BOOL InitInstance( HINSTANCE hInstance,int nCmdShow)
{
hwnd = CreateWindow ("HelloWin",TEXT ("Vwind"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if (!hwnd)
return (FALSE);
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
return (TRUE);
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SYSCOMMAND:
switch(LOWORD(wParam))
{
//退出菜单
case SC_CLOSE:
MessageBox(hwnd, "wParam", "关闭", MB_YESNO );
SendMessage(hwnd,WM_CLOSE,0,0);
break;
}
break;
case WM_DESTROY: //消息:本窗口正将被销毁
//请求退出窗口和应用程序
PostQuitMessage (0);
break;
default:
//调用默认窗口过程对未处理的消息进行必要的处理
return DefWindowProc (hwnd, message, wParam, lParam);
}
return (0);
}
在选X(或是选菜单) 的时候,那么系统收的消息是WM_SYSCOMMAND 而不是WM_COMMAND。
对DefWindowProc的理解不沟深入。
对ID理解有问题。你可能混淆了Dialog 的和Windows 的区别。