首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FindWindow找不到窗口

FindWindow找不到窗口
EN

Stack Overflow用户
提问于 2013-05-13 20:48:38
回答 4查看 72.1K关注 0票数 11

我有一个用C++做一个简单的教练控制台的计划,但是第一步我对FindWindow()有了问题。

代码语言:javascript
运行
复制
#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();
    }
}

上面的代码用于尝试命令FindWindow(),但在执行输出时总是显示

找不到

我已经替换了属性项目中的字符集

使用Unicode字符集

使用多字节字符集

LPCTSTR

LPCSTR

LPCWSTR

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-05-13 20:52:43

代码语言:javascript
运行
复制
 HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
票数 9
EN

Stack Overflow用户

发布于 2013-05-13 21:15:42

只有当窗口有确切指定的标题时,FindWindow才能找到窗口,而不仅仅是子字符串。

另外,你也可以:

搜索窗口类名:

代码语言:javascript
运行
复制
HWND hWnd = FindWindow("MozillaWindowClass", 0);

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

代码语言:javascript
运行
复制
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;
}
票数 17
EN

Stack Overflow用户

发布于 2016-12-15 14:10:47

根据MSDN

lpWindowName in,可选 类型: LPCTSTR窗口名称(窗口的标题)。如果此参数为NULL,则所有窗口名称都匹配。

因此,您的WindowName不可能是"Mozilla“,因为Firefox窗口的标题永远不是"Mozilla”,但是它可能是"Mozilla起始页面- Mozilla“,或者取决于网页的名称。这是一个例子

因此,您的代码应该是这样的(下面的代码只工作-只工作,如果您有确切的窗口名称:"Mozilla - Mozilla“,如上图所示。我已经在Windows8.1上测试过了,它成功了)

代码语言:javascript
运行
复制
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.
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16530871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档