查找窗口( ... ) 未"查找"创建的消息窗口
FindWindow( ... ) is not 'finding' created message window
在我的代码中,我有一个消息类,我想从另一个过程中'查找'。
class MyWindow : public CWnd
{
public:
MyWindow::MyWindow(LPCTSTR pszClassName)
{
auto wcn = ::AfxRegisterWndClass(NULL);
auto created = this->CreateEx(0, wcn, pszClassName, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
}
};
然后在我的主要应用中的某个地方。
...
auto pszClassName = _T("MyWindowClass");
auto wnd = new MyWindow(pszClassName);
auto p = FindWindow(pszClassName, nullptr); // = nullptr
// or using FindWindowExW( ... )
p = FindWindowExW(nullptr, nullptr, pszClassName, nullptr);// = nullptr
p = FindWindowExW(HWND_MESSAGE, nullptr, pszClassName, nullptr);// = nullptr
所以,无论我做什么,我似乎都从未找到"创建"窗口。
如何使用Findwindow [ex]
我正在使用VS2013控制台应用程序。我已经稍微修改了您的代码,以创建一个普通的窗口和一个只有消息的窗口,并按班级名称找到两个手柄。
输出:
Normal window=00000000003A06FE Message-only window=00000000001F06CA
FindWindow=00000000003A06FE
FindWindowEx=00000000003A06FE
FindWindowEx(HWND_MESSAGE,...)=00000000001F06CA
代码:
typedef std::basic_string<TCHAR> tstring;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
class MyWindow //: public CWnd
{
public:
tstring className;
HWND hwnd;
HWND hwndMsgOnly;
HWND find1;
HWND find2;
HWND find3;
MyWindow::MyWindow(LPCTSTR pszClassName)
{
className = pszClassName;
//auto wcn = ::AfxRegisterWndClass(NULL);
//auto created = this->CreateEx(0, wcn, pszClassName, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
// For a console app, `hInstance` is the instance of the program
HINSTANCE hInstance = GetModuleHandle(0);
WNDCLASSEX windowClass;
windowClass.lpszClassName = pszClassName;
windowClass.cbClsExtra = NULL;
windowClass.cbWndExtra = NULL;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(150, 0, 0));
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
windowClass.hIconSm = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 16, 16, NULL);
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = WindowProc;
windowClass.lpszMenuName = NULL;
windowClass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&windowClass);
hwnd = CreateWindowEx(NULL, pszClassName, _T("Basic Window"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500, NULL, NULL, hInstance, NULL);
hwndMsgOnly = CreateWindowEx(NULL, pszClassName, _T("Dummy name"), 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
}
};
void window_test(MyWindow & wnd)
{
LPCTSTR pszClassName = wnd.className.c_str();
wnd.find1 = FindWindow(pszClassName, nullptr); // = nullptr for OP version; okay for this version
// or using FindWindowExW( ... )
/**
HWND hwndParent is a handle to the parent window whose child windows are to be searched.
If hwndParent is NULL, the function uses the desktop window as the parent window.The function searches among windows that are child windows of the desktop.
If hwndParent is HWND_MESSAGE, the function searches all message-only windows.
**/
HWND hWndParent = nullptr;
wnd.find2 = FindWindowExW(hWndParent, nullptr, pszClassName, nullptr);// = nullptr for OP version; okay for this version
hWndParent = HWND_MESSAGE;
wnd.find3 = FindWindowExW(hWndParent, nullptr, pszClassName, nullptr);// = nullptr for OP version; finds the message-only window in this version
}
void main()
{
MyWindow wnd(_T("test_window"));
cout << "Normal window=" << wnd.hwnd << " Message-only window=" << wnd.hwndMsgOnly << endl;
window_test(wnd);
cout << "FindWindow=" << wnd.find1 << endl;
cout << "FindWindowEx=" << wnd.find2 << endl;
cout << "FindWindowEx(HWND_MESSAGE,...)=" << wnd.find3 << endl;
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 问:如何使用C++中的按钮从窗口打开窗口
- 查找窗口找不到进程
- 如何从.exe查找名称窗口
- 如何从exe文件的名称中查找窗口句柄
- 查询/查找启动过程窗口
- 查找窗口( ... ) 未"查找"创建的消息窗口
- 在最小化的 MFC 窗口中查找 CListCtrl 的有效大小
- 在窗口上查找 c++ 中的大文件
- 查找窗口并更改其名称
- 无法使用查找窗口查找窗口
- 查找控件相对于其父窗口的位置的最佳方法是什么?
- 在具有重复项的长数据序列的滚动窗口中查找模式
- 使用接口类型查找子窗口小部件
- 查找窗口不起作用 C++
- c++ FindWindow查找当前窗口