非模态对话框问题,以及消息循环问题,请教

tfq 2005-07-12 11:03:59
1 我建了个非模态对话框,为什么在主窗口中一点击按钮激活就一闪而过?

2 。
while(GetMessage(&Msg,NULL,0,0))
{
if(!IsDialogMessage(hdlg,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}

如果不是对话框消息,那TranslateMessage(&Msg);DispatchMessage(&Msg);还执行吗?不执行其他消息又如何处理?要不是对话框消息而是其他消息仍然可以处理啊, 怎么处理的?
...全文
1550 点赞 收藏 22
写回复
22 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tfq 2005-07-13
这样也不行。
另外非模态对话框不是用destroywindow吗?用EndDialog(hDlg, LOWORD(wParam));也行?

快崩溃啦!!!!!!!!!!!!!!!
回复
tfq 2005-07-13
好。非常非常感谢,马上揭帖!
回复
goodboyws 2005-07-13
一般直接加到resource.h里就可以了
回复
goodboyws 2005-07-13
很少有这么干的,我也懒得琢磨为什么,呵呵
回复
tfq 2005-07-13
假如我在头文件中加一个
#define ID_OK1 1001
然后在资源文件中包含头文件,DlgProcAdd函数中的ID0K也相应改为ID_OK1,为何出现:
D:\VC2\MSDEV98\MYPROJECTS\tangchao\manage.h(8) : fatal error RC1004: unexpected end of file found


回复
tfq 2005-07-13
啊!!!!!!!!!!!!!!!!!!!!
搞晕掉了。
感激涕零,无以言表。。。。。
回复
goodboyws 2005-07-13
case WM_COMMAND:

case WM_DESTROY:
之间少了个break;
呵呵
回复
tfq 2005-07-13
资源文件:manage.rc
-----------------------
#include <windows.h>
//#include "resource.h"
#include "afxres.h"

Add DIALOG 50,50,100,50
STYLE WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_VISIBLE|DS_MODALFRAME
CAPTION "添加对话框"
FONT 18,"楷体" //定义对话框中的文本字体类型及大小
begin
CTEXT "代号:",1002,13,10,80,10
// EDITTEXT ID_EDITTEXT1,30,30,30,30
PUSHBUTTON "确定",IDOK,35,30,30,12
end

Delete DIALOG 100,100,200,100
STYLE WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_VISIBLE
CAPTION "添加对话框"
FONT 18,"楷体" //定义对话框中的文本字体类型及大小
begin
CTEXT "代号:",1003,20,20,30,20
// EDITTEXT ID_EDITTEXT1,30,30,30,30
PUSHBUTTON "确定",IDOK,35,30,30,12

end

Find DIALOG 100,100,200,100
STYLE WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_VISIBLE
CAPTION "添加对话框"
FONT 18,"楷体" //定义对话框中的文本字体类型及大小
begin
// CTEXT "代号:",1004,20,20,30,20
// EDITTEXT ID_EDITTEXT1,30,30,30,30
DEFPUSHBUTTON "确定",IDOK,35,30,30,12
end

头文件:manage.h
-----------------------
#define ID_EDIT 100
#define ID_ADD 200
#define ID_DELETE 201
#define ID_FIND 202
#define ID_UPDATE 203
#define ID_STATIC 300
//#define ID_OK1 1001
//#define ID_OK2 1002

源文件:manage.cpp
---------------------
#include <windows.h>
#include "manage.h"

BOOL CALLBACK DlgProcAdd(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DlgProcDelete(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DlgProcFind(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

HWND hdlg;
HWND hEditItem,hAdd,hDelete,hFind,hUpdate,hStatic;
HINSTANCE hInst;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS WndClass;
char lpszTitle[] = "唐朝户部大内总管";
char lpszClassName[]="实例1";


WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
// WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName=lpszClassName;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&WndClass))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
400,
400,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

hInst = hInstance;
while(GetMessage(&Msg,NULL,0,0))
{
if(!IsDialogMessage(hdlg,&Msg)) //截获非模态对话框消息并发往处理函数
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}



LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
hEditItem = CreateWindow("EDIT",NULL,WS_CHILD|ES_AUTOHSCROLL|ES_AUTOVSCROLL
|ES_LEFT|ES_MULTILINE|WS_VISIBLE|WS_BORDER,
50,200,300,150,hwnd,(HMENU)ID_EDIT,hInst,NULL);

hAdd = CreateWindow("BUTTON","添加",WS_CHILD|WS_VISIBLE,
50,20,50,30,hwnd,(HMENU)ID_ADD,hInst,NULL);

hDelete = CreateWindow("BUTTON","删除",WS_CHILD|WS_VISIBLE,
50,60,50,30,hwnd,(HMENU)ID_DELETE,hInst,NULL);

hFind = CreateWindow("BUTTON","查找",WS_CHILD|WS_VISIBLE,
50,100,50,30,hwnd,(HMENU)ID_FIND,hInst,NULL);

hUpdate = CreateWindow("BUTTON","更新",WS_CHILD|WS_VISIBLE,
50,140,50,30,hwnd,(HMENU)ID_UPDATE,hInst,NULL);

hStatic = CreateWindow("STATIC","更新结果状态栏:",WS_CHILD|WS_VISIBLE,
50,175,150,20,hwnd,(HMENU)ID_STATIC,hInst,NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_ADD:
hdlg = CreateDialog(hInst,"Add",hwnd,(DLGPROC)DlgProcAdd);
ShowWindow(hdlg,SW_SHOW);

// ShowWindow(hdlg,SW_NORMAL);
break;
case ID_DELETE:
hdlg = CreateDialog(hInst,"Delete",hwnd,(DLGPROC)DlgProcDelete);
ShowWindow(hdlg,SW_SHOW);
break;
case ID_FIND:
hdlg = CreateDialog(hInst,"Find",hwnd,(DLGPROC)DlgProcFind);
ShowWindow(hdlg,SW_SHOW);
break;
case ID_UPDATE:
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:

return DefWindowProc(hwnd,message,wParam,lParam);

}
return false;
}

BOOL CALLBACK DlgProcAdd(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:

return 1; //初始化对话框
case WM_COMMAND: //处理消息框消息
switch(LOWORD(wParam))
{
case IDOK:
DestroyWindow(hdlg); //关闭对话框

return true;
}
break;
case WM_CLOSE:
DestroyWindow(hdlg);
return 1;
}
return 0;
}

BOOL CALLBACK DlgProcDelete(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 1; //初始化对话框
case WM_COMMAND: //处理消息框消息
switch(LOWORD(wParam))
{
case IDOK:
DestroyWindow(hdlg); //关闭对话框
return 1;
}
break;
case WM_CLOSE:
DestroyWindow(hdlg);
return 1;
}
return 0;
}

BOOL CALLBACK DlgProcFind(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 1; //初始化对话框
case WM_COMMAND: //处理消息框消息
switch(LOWORD(wParam))
{
case IDOK:
DestroyWindow(hdlg); //关闭对话框
return 1;
}
break;
case WM_CLOSE:
DestroyWindow(hdlg);
return 1;
}
return 0;
}

另外我想定义一些其他的按钮ID放于头文件中,然后在资源文件中包含,会出现一个错误出现在头文件中,大概是
unexpected end of file ,这是怎么回事?
回复
huanghua20000 2005-07-13
使用new创建对话框
回复
goodboyws 2005-07-13
我的意思你这段代码有问题,全贴出来看看
回复
tfq 2005-07-13
case ID_ADD:
hdlg = CreateDialog(hInst,"Add",hwnd,(DLGPROC)DlgProcAdd);
没有问题啊。贴出来换行乱了
回复
goodboyws 2005-07-13
case ID_ADD:
hdlg = CreateDialog(hInst,"Add",hwnd,(DLGPROC)
后面的代码有问题,呵呵
回复
tfq 2005-07-13
谢谢goodboyws(深夜不眠者)
这样也不行。

to:ForTopLevel(Beahead) ( )
我没有重绘。一触发对话框一闪连整个程序就退出了
回复
ForTopLevel 2005-07-13
在主窗口接收到重绘消息时必须要重绘对话框,不然你的对话框就会被重绘过的主窗口覆盖掉.
回复
goodboyws 2005-07-13
case WM_INITDIALOG:
DefWindowProc(hDlg, message, wParam, lParam);
return 1;
或者
case WM_INITDIALOG:
return 0;
试试,这个我也没有把握
回复
goodboyws 2005-07-13
不好意思,上面是胡说的
回复
goodboyws 2005-07-13
BOOL CXXXApp::InitInstance()
{
......
return TRUE;
}
回复
tfq 2005-07-13
好。消息循环已理解。是我看错了IsDialogMessage(hdlg,&Msg的返回值。

剩下的问题哪位高人说说什么情况下对话框会一闪而过?
回复
goodboyws 2005-07-13
1. if (!IsDialogMessage(hdlg,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}

这段话的意思是,如果是对话框消息,由IsDialogMessage处理,否则TranslateMessage(&Msg);
DispatchMessage(&Msg);
2.非模态对话框EndDialog隐藏窗口,destroywindow销毁窗口
回复
xing_xing_xing 2005-07-12
HWND hwndDlg = CreateDialog(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
ShowWindow(hwndDlg,SW_NORMAL);

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) //这样判断
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
回复
加载更多回复
相关推荐
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2005-07-12 11:03
社区公告
暂无公告