C++ FindWindow 没有找到一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16530871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:27:20  来源:igfitidea点击:

FindWindow does not find the a window

c++winapivisual-c++

提问by ginc0de

Hey guys I've plan for make a simple trainer console with C++ but first step I've got problem with FindWindow()

嘿伙计们,我计划用 C++ 制作一个简单的教练控制台,但第一步我遇到了 FindWindow() 的问题

#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <winuser.h>
#include <conio.h>

LPCTSTR WindowName = "Mozilla Firefox";
HWND Find = FindWindow(NULL,WindowName);
int main(){
    if(Find)
    {
        printf("FOUND\n");
        getch();
    }
    else{
        printf("NOT FOUND");
        getch();
    }
}

The above code I use to try whether the command FindWindow() but when I execute the output always show

上面的代码我用来尝试命令 FindWindow() 但是当我执行输出时总是显示

NOT FOUND

未找到

I've replaced Character Set on property Project from

我已经替换了属性 Project 上的字符集

Use Unicode Character Set

使用 Unicode 字符集

to

Use Multi-Byte Character Set

使用多字节字符集

and

LPCTSTR

LPCTSTR

to

LPCSTR

LPCSTR

or

或者

LPCWSTR

LPCWSTR

but the result always the same, I hope anyone can help me.

但结果总是一样,我希望任何人都可以帮助我。

采纳答案by David Brabant

 HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);

回答by typ1232

FindWindowonly finds the window if it has the exact specified title, not just a substring.

FindWindow仅找到具有确切指定标题的窗口,而不仅仅是子字符串。

Alternatively you can:

或者,您可以:



search for the window class name:

搜索窗口类名:

HWND hWnd = FindWindow("MozillaWindowClass", 0);


enumerateall windows and perform custom pattern searches on the titles:

枚举所有窗口并对标题执行自定义模式搜索:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char buffer[128];
    int written = GetWindowTextA(hwnd, buffer, 128);
    if (written && strstr(buffer,"Mozilla Firefox") != NULL) {
        *(HWND*)lParam = hwnd;
        return FALSE;
    }
    return TRUE;
}

HWND GetFirefoxHwnd()
{
    HWND hWnd = NULL;
    EnumWindows(EnumWindowsProc, &hWnd);
    return hWnd;
}

回答by tungnguyen

You need to use the full name of the application (as seen in Windows Task Manager -> Application tab)

您需要使用应用程序的全名(如 Windows 任务管理器 -> 应用程序选项卡中所示)

Example:

例子:

Google - Mozilla Firefox

Google - Mozilla Firefox

(after opening a Google tab in Firefox)

(在 Firefox 中打开 Google 标签后)

回答by 123iamking

According to MSDN

根据MSDN

lpWindowName [in, optional]

Type: LPCTSTR

The window name (the window's title). If this parameter is NULL, all window names match.

lpWindowName [输入,可选]

Type: LPCTSTR

The window name (the window's title). If this parameter is NULL, all window names match.

Thus your WindowName can't be "Mozilla Firefox", because the Firefox window's title is never "Mozilla Firefox" but it could be "Mozilla Firefox Start Page - Mozilla Firefox" or something depends on the web page's name. Here is the example picture Firefox's real tiltle

因此,您的 WindowName 不能是“Mozilla Firefox”,因为 Firefox 窗口的标题永远不会是“Mozilla Firefox”,而可能是“Mozilla Firefox Start Page - Mozilla Firefox”或取决于网页名称的内容。这是示例图片 Firefox 的真正倾斜

Thus your code should be like this, (the code below only work - only workif you have the exact window's title name: "Mozilla Firefox Start Page - Mozilla Firefox" like the image above. I have tested on Windows 8.1 and it worked)

因此,您的代码应该是这样的,(下面的代码仅适用 -当您具有确切的窗口标题名称时才有效:“Mozilla Firefox Start Page - Mozilla Firefox”,如上图。我已经在 Windows 8.1 上进行了测试并且它有效)

void CaptureWindow()
{


RECT rc;
HWND hwnd = ::FindWindow(0, _T("Mozilla Firefox Start Page - Mozilla Firefox"));//::FindWindow(0,_T("ScreenCapture (Running) - Microsoft Visual Studio"));//::FindWindow(0, _T("Calculator"));//= FindWindow("Notepad", NULL);    //You get the ideal?
if (hwnd == NULL)
{
    return;
}
GetClientRect(hwnd, &rc);

//create
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
    rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);

//Print to memory hdc
PrintWindow(hwnd, hdc, PW_CLIENTONLY);

//copy to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbmp);
CloseClipboard();

//release
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);

//Play(TEXT("photoclick.wav"));//This is just a function to play a sound, you can write it yourself, but it doesn't matter in this example so I comment it out.
}