对话框怎么控制消息循环

peachbrandy1 2011-03-13 09:13:00
像mfc可以重载pretranslatemessage,那sdk的dialogbox怎么控制其消息循环呢,难道是hook getmesssage?太复杂了把...
...全文
497 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwd100815 2011-03-14
  • 打赏
  • 举报
回复
while (GetMessage(&msg, NULL, 0, 0))
amazheng 2011-03-14
  • 打赏
  • 举报
回复
用asp.net多了,消息循环都全丢光了,咋办呀
jshoong 2011-03-13
  • 打赏
  • 举报
回复
DialogBox Windows在其内部就建立了一个Message Loop
不用自己写就是了
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
也谢谢所有人帮忙解答,总结两点,dialogbox想改变消息循环只有hook,2.想处理wm_keydown可以参照21#,若有不对请指正
没有管理员改日在结,谢谢所有回答的哥们
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
郁闷,手机结不了贴,有管理员在么,麻烦给13楼和21各20,谢谢
_Sodo_ 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 peachbrandy1 的回复:]
像mfc可以重载pretranslatemessage,那sdk的dialogbox怎么控制其消息循环呢,难道是hook getmesssage?太复杂了把...
[/Quote]

你想怎么控制?
你妹的特盗不 2011-03-13
  • 打赏
  • 举报
回复
lz 你怎么创建的dialog啊?

sdk 我记得是用CreateDialog这个函数吧
msdn 直接输入 CreateDialog 就出来了

This function creates a modeless dialog box from a dialog box template resource.

HWND CreateDialog(
HINSTANCE hInstance,
LPCTSTR lpTemplate,
HWND hWndParent,
DLGPROC lpDialogFunc //这个就是消息响应回函数
);
Parameters
hInstance
[in] Handle to the module whose executable file contains the dialog box template.
lpTemplate
[in] Long pointer to the dialog box template. This parameter is either the pointer to a null-terminated character string that specifies the name of the dialog box template or an integer value that specifies the resource identifier of the dialog box template. If the parameter specifies a resource identifier, its high-order word must be zero and its low-order word must contain the identifier. You can use the MAKEINTRESOURCE macro to create this value.
hWndParent
[in] Handle to the window that owns the dialog box.
lpDialogFunc
[in] Long pointer to the dialog box procedure. For more information about the dialog box procedure, see DialogProc.
Return Values
The handle to the dialog box indicates success. NULL indicates failure. To get extended error information, call GetLastError.

Remarks
The CreateDialog function uses the CreateWindowEx function to create the dialog box. CreateDialog then sends a WM_INITDIALOG message to the dialog box procedure. The function displays the dialog box if the template specifies the WS_VISIBLE style. Finally, CreateDialog returns the window handle to the dialog box.

After CreateDialog returns, the application displays the dialog box (if it is not already displayed) by using the ShowWindow function. The application destroys the dialog box by using the DestroyWindow function.

Microsoft® Windows® CE does not support all styles in the DLGTEMPLATE structure. This structure is in the template identified by the lpTemplate parameter.

Requirements
OS Versions: Windows CE 1.0 and later.
Header: Winuser.h.



ysjyniiq 2011-03-13
  • 打赏
  • 举报
回复
MFC的DoModal内部是用CreateDialogIndirect创建对话框,然后在再加消息循环。
而你用SDK的DialogBox创建后,就直接卡着了,函数内部搞消息循环,所以你截不了它的原始消息。
至你说到不能处理WM_KEYDOWN,只是表面现象,DialogBox创建一样有方法处理。
解决方法:
方法1。用CreateDialogIndirect创建对话框,外加消息循环,然后在GetMessage或PeekMessage后里截住WM_KEYDOWN,实现所谓pretranslatemessage,跟MFC处理方法类似。
方法2。用DialogBox创建,然后处理消息WM_GETDLGCODE,返回的HRESULT值为DLGC_WANTALLKEYS|DLGC_WANTCHARS,然后你WM_KEYDOWN就有反应了!!
Lactoferrin 2011-03-13
  • 打赏
  • 举报
回复
应该没有这么复杂,你在调用DialogBoxParam或CreateDialogParam时要规定对话框过程的,你在对话框过程中处理消息就行了


#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#define ImageBase ((HINSTANCE)0x40000)

INT_PTR __stdcall DialogFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList,hwndEdit[3];
switch (message)
{
case WM_COMMAND://这里处理WM_COMMAND消息
switch(LOWORD(wParam))
{
case IDC_ADD:
{
static wchar_t Buffer[512];
static LVITEM lvitem={LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM,2147483647,0,0,0,Buffer},lvsubitem={LVIF_TEXT,0,1,0,0,Buffer},lvsubitem2={LVIF_TEXT,0,2,0,0,Buffer};
SendMessage(hwndEdit[0],WM_GETTEXT,512,(LPARAM)Buffer);
lvsubitem.iItem=lvsubitem2.iItem=ListView_InsertItem(hwndList,&lvitem);
SendMessage(hwndEdit[1],WM_GETTEXT,512,(LPARAM)Buffer);
ListView_SetItem(hwndList,&lvsubitem);
SendMessage(hwndEdit[2],WM_GETTEXT,512,(LPARAM)Buffer);
ListView_SetItem(hwndList,&lvsubitem2);
}
break;
}
return 1;
case WM_INITDIALOG:
{
static LVCOLUMN col_H1={LVCF_TEXT|LVCF_WIDTH,0,50,TEXT("H1")},col_H2={LVCF_TEXT|LVCF_WIDTH,0,50,TEXT("H2")},col_H3={LVCF_TEXT|LVCF_WIDTH,0,50,TEXT("H3")};
hwndList=GetDlgItem(hDlg,IDC_LIST1);
hwndEdit[0]=GetDlgItem(hDlg,IDC_EDIT1);
hwndEdit[1]=GetDlgItem(hDlg,IDC_EDIT2);
hwndEdit[2]=GetDlgItem(hDlg,IDC_EDIT3);
ListView_InsertColumn(hwndList,0,&col_H1);
ListView_InsertColumn(hwndList,1,&col_H2);
ListView_InsertColumn(hwndList,2,&col_H3);
}
return 1;
case WM_CLOSE:
EndDialog(hDlg,0);
}
return 0;
}

void EntryPoint(void)
{
static INITCOMMONCONTROLSEX icce={sizeof(INITCOMMONCONTROLSEX),ICC_WIN95_CLASSES};
InitCommonControlsEx(&icce);
ExitProcess(DialogBoxParam(ImageBase,MAKEINTRESOURCE(IDD_FORMVIEW),0,DialogFunc,0));
}
Lactoferrin 2011-03-13
  • 打赏
  • 举报
回复
mfc下重载对话框消息

mfc在自己的对话框过程中调用你提供的消息处理例程
Lactoferrin 2011-03-13
  • 打赏
  • 举报
回复
使用SetWindowsHookEx安装消息钩子,或者使用SetWindowLong修改窗口过程
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
to 15楼:我知道,我想如何接管它的消息
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
to 13l:感谢您的解答,因为没查资料,所以我大概估摸着说是hook getmesssage的,其实我就是想知道mfc下重载对话框消息是怎么实现的,莫非如同hook一般,只是觉得hook太繁琐
Lactoferrin 2011-03-13
  • 打赏
  • 举报
回复
模式对话框的消息循环在DialogBoxParam中,不归你管
非模式对话框由CreateDialogParam创建,需要你的代码维持消息循环
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
to 12l:我是手机所以没法,另比较郁闷,莫非兄弟没用过dialogbox,dialogbox有自己的消息循环,可视13l,
aiwnx 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 peachbrandy1 的回复:]
像mfc可以重载pretranslatemessage,那sdk的dialogbox怎么控制其消息循环呢,难道是hook getmesssage?太复杂了把...
[/Quote]
首先,标准对话框使用PeekMessage()而不是GetMessage()来获取消息,所以你hook GetMessage()是徒劳的
其次,PeekMessage()调用后,如果是对话框消息(IsDialogMessage()),则接着调用TranslateMessage()和DispatchMessage();如果不是,则调用WaitMessage()让出处理器给其它线程,该线程睡眠
再次,以上步骤均在User32.dll模块中完成,所以如果你想改变消息循环流程,只有hook一条路了
zwfgdlc 2011-03-13
  • 打赏
  • 举报
回复

痛苦,砬到你这样的人.
哪有窗口没有消息循环的.

又不贴代码.
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
to 10l:dialogbox哎,哪来消息循环
zjfhgdx 2011-03-13
  • 打赏
  • 举报
回复
是消息循环啊。。。
while (GetMessage(&msg, NULL, 0, 0)) 这个不是消息循环吗?
难道你的SDK代码里没有这一段?
peachbrandy1 2011-03-13
  • 打赏
  • 举报
回复
to 6l:是对话框,不是窗口,对话框有自己的messageloop
加载更多回复(5)

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧