在线程里开启等待对话框,在主线程里发送关闭消息,FindWindow得不到等待框的句柄!
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);
}
}
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);
}
}
...全文
相关推荐
FindWindow的用法
= NULL) { PostMessage(hWnd, WM_CLOSE, 0, 0); }}```需要注意的是,`PostMessage`会在发送完消息后立即返回,不会等待接收窗口处理完毕;而`SendMessage
C++查看窗口句柄的源码findwindow
SPY++是一款强大的微软开发工具,用于可视化地查看和调试Windows消息、线程、进程以及窗口句柄。
基于MFC根据句柄关闭窗口
使用MFC,我们可以通过`CWinApp`类的`SendDlgItemMessage`或`SendMessage`成员函数来发送消息。但是,这两种方法通常用于与对话框控件交互,而非直接关闭一个窗口。
C#实现winform自动关闭MessageBox对话框的方法
我们将调用`FindWindow`函数来查找具有特定标题的`MessageBox`窗口,然后使用`PostMessage`函数发送一个`WM_CLOSE`消息,这将模拟用户点击对话框上的“关闭”按钮。
使用JNA获取窗口句柄,并发送消息
句柄通常用于进一步操作,如发送消息。发送消息到窗口则需要用到`SendMessage`函数。这个函数允许我们将特定的消息类型和参数发送到目标窗口。


