Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ FindWindow(…)不是';发现';创建消息窗口_C++_Windows_C++11_Findwindow_Findwindowex - Fatal编程技术网

C++ FindWindow(…)不是';发现';创建消息窗口

C++ FindWindow(…)不是';发现';创建消息窗口,c++,windows,c++11,findwindow,findwindowex,C++,Windows,C++11,Findwindow,Findwindowex,在我的代码中,我有一个消息类,我想从另一个进程中“查找” 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);

在我的代码中,我有一个消息类,我想从另一个进程中“查找”

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 tstring;
LRESULT回调WindowProc(HWND HWND、UINT uMsg、WPARAM WPARAM、LPARAM LPARAM)
{
返回DefWindowProc(hwnd、uMsg、wParam、lParam);
}
类MyWindow/:public CWnd
{
公众:
tstring类名称;
HWND-HWND;
HWND hwndMsgOnly;
HWND-find1;
HWND-find2;
HWND-find3;
MyWindow::MyWindow(LPCTSTR pszClassName)
{
className=pszClassName;
//自动wcn=::AfxRegisterWndClass(空);
//自动创建=此->CreateEx(0,wcn,pszClassName,0,0,0,0,0,HWND_消息,0);
//对于控制台应用程序,“hInstance”是该程序的实例
HINSTANCE HINSTANCE=GetModuleHandle(0);
WNDCLASSEX窗口类;
windowClass.lpszClassName=pszClassName;
windowClass.cbClsExtra=NULL;
windowClass.cbWndExtra=NULL;
windowClass.cbSize=sizeof(WNDCLASSEX);
windowClass.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(150,0,0));
windowClass.hCursor=LoadCursor(空,IDC_箭头);
windowClass.hIcon=LoadIcon(NULL,MAKEINTRESOURCE(IDI_应用程序));
windowClass.hIconSm=(HICON)LoadImage(NULL,MAKEINTRESOURCE(IDI_应用程序),IMAGE_图标,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(“基本窗口”),WS_重叠窗口| WS_可见,100100500500,NULL,NULL,hInstance,NULL);
hwndMsgOnly=CreateWindowEx(NULL,pszClassName,_T(“伪名称”),0,0,0,0,HWND_消息,NULL,NULL,NULL);
}
};
无效窗口测试(MyWindow&wnd)
{
LPCTSTR pszClassName=wnd.className.c_str();
wnd.find1=FindWindow(pszClassName,nullptr);//=nullptr(对于OP版本);对于此版本,可以
//或者使用FindWindowExW(…)
/**
HWND hwndParent是要搜索其子窗口的父窗口的句柄。
如果hwndParent为NULL,则函数将桌面窗口用作父窗口。函数将在作为桌面子窗口的窗口之间进行搜索。
如果hwndParent是HWND_消息,则函数将搜索所有仅消息窗口。
**/
HWND hWndParent=nullptr;
wnd.find2=FindWindowExW(hwndpresent,nullptr,pszClassName,nullptr);//=nullptr(对于OP版本);对于此版本,可以
hWndParent=HWND_消息;
wnd.find3=FindWindowExW(hWndParent,nullptr,pszClassName,nullptr);//=nullptr(对于OP版本);查找此版本中的“仅邮件”窗口
}
void main()
{
MyWindow wnd(测试窗口);

当您创建窗口时,您使用的不是类名称而是窗口名称。类名在
wcn
中,您必须将其传递给
FindWindow
。因此基本上我需要创建自己的
WNDCLASS
并调用
RegisterClass(…)
您可以使用
pszClassName
调用
RegisterClass
,也可以使用
AfxRegisterWndClass
的返回值作为
FindWindow
,这可能更好,因为
AfxRegisterWndClass
可能确保名称是唯一的。也许UIPI限制了您?
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;
}