在线程里开启等待对话框,在主线程里发送关闭消息,FindWindow得不到等待框的句柄!

slj_win 2010-12-14 01:22:11
int InitDlgRes;

static LRESULT CALLBACK InitDlgProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_INITDIALOG:
{
CWnd::FromHandle(hwnd)->CenterWindow(); //AfxGetMainWnd()->CenterWindow(hwnd);
return TRUE;
}

case WM_CLOSE:
{
EndDialog(hwnd,InitDlgRes);// InitDlgRes TRUE
return TRUE;
}

}
return FALSE;
}


DWORD WINAPI InitDlgThreadProc(LPVOID lpParameter)//pApp->m_instanceDIALOG_
{
InitDlgRes = DialogBox(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDD_INITDLG),AfxGetMainWnd()->m_hWnd, (DLGPROC) InitDlgProc);
return TRUE;
}

void ShowInitDlg()
{
LPSECURITY_ATTRIBUTES lpta = NULL;
DWORD dwsz = 0;
LPTHREAD_START_ROUTINE lpsr = InitDlgThreadProc;
LPVOID lpp = NULL;
DWORD dwcf = 0;
DWORD lptid;

HANDLE handle = CreateThread(
lpta, // pointer to security attributes
dwsz, // initial thread stack size
lpsr, // pointer to thread function
lpp, // argument for new thread
dwcf, // creation flags
&lptid // pointer to receive thread ID
);

}

void EndInitDlg()
{
Sleep(2000);
HWND hwnd = FindWindowEx(AfxGetMainWnd()->m_hWnd,NULL,NULL,_T("123"));
if(hwnd)
{
PostMessage(hwnd,WM_CLOSE,0,0);
}
}
...全文
134 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
slj_win 2010-12-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jamseyang 的回复:]
把FindWindowEx(AfxGetMainWnd()->m_hWnd,NULL,NULL,_T("123"));

改成::FindWindow(NULL, _T("123"))试试
[/Quote]
试过没有效果
slj_win 2010-12-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 arong1234 的回复:]
AfxGetMainWnd只能在该窗口所在的线程内工作,你应该尝试把它的窗口句柄传给线程,而不是动态获取
[/Quote]
不行,没有改变。
slj_win 2010-12-14
  • 打赏
  • 举报
回复
我是在线程里开启的,主线程仍可以工作的,不会挂起,只是窗口不响应外部输入啊!
fthislife 2010-12-14
  • 打赏
  • 举报
回复
The DialogBox macro uses the CreateWindowEx function to create the dialog box. DialogBox then sends a WM_INITDIALOG message (and a WM_SETFONT message if the template specifies the DS_SETFONT or DS_SHELLFONT style) to the dialog box procedure. The function displays the dialog box (regardless of whether the template specifies the WS_VISIBLE style), disables the owner window, and starts its own message loop to retrieve and dispatch messages for the dialog box.

When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter specified by the dialog box procedure when it called EndDialog.

上面是MSDN上的原话,大概意思是:DialogBox宏使用CreateWindowEx函数创建一个对话框,然后发送一个WM_INITDIALOG 消息给对话框过程函数,这个函数显示对话框,使拥有它的窗口失效,且并开始它自己的消息循环,获取并分发消息给对话框。
当对话框调用EndDialog结束后,owner window才继续有效。。。

所以,,AfxGetMainWnd()->m_hWnd所指向的窗口会暂时失效。。。。。。。
fthislife 2010-12-14
  • 打赏
  • 举报
回复
DialogBox这个是产生一个模式对话框,当这个对话框产生后,主线程被挂起了,在主线程里发送信息当然没用了。而你开启两个实例,使用FindWindowEx通过窗口名从一个实例找另一个实例的窗口句柄,当然可以了
jamseyang 2010-12-14
  • 打赏
  • 举报
回复
把FindWindowEx(AfxGetMainWnd()->m_hWnd,NULL,NULL,_T("123"));

改成::FindWindow(NULL, _T("123"))试试
arong1234 2010-12-14
  • 打赏
  • 举报
回复
AfxGetMainWnd只能在该窗口所在的线程内工作,你应该尝试把它的窗口句柄传给线程,而不是动态获取
slj_win 2010-12-14
  • 打赏
  • 举报
回复
如果开启两个实例,智能得到对方的等待对话框的句柄,却得不到自己的。高手给看看吧!

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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