我有一个用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();
}
}
上面的代码用于尝试命令FindWindow(),但在执行输出时总是显示
找不到
我已经替换了属性项目中的字符集
使用Unicode字符集
至
使用多字节字符集
和
LPCTSTR
至
LPCSTR
或
LPCWSTR
但是结果总是一样的,我希望任何人都能帮助我。
发布于 2013-05-13 20:52:43
HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
发布于 2013-05-13 21:15:42
只有当窗口有确切指定的标题时,FindWindow
才能找到窗口,而不仅仅是子字符串。
另外,你也可以:
搜索窗口类名:
HWND hWnd = FindWindow("MozillaWindowClass", 0);
枚举所有窗口并对标题执行自定义模式搜索:
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;
}
发布于 2016-12-15 14:10:47
根据MSDN
lpWindowName in,可选 类型: LPCTSTR窗口名称(窗口的标题)。如果此参数为NULL,则所有窗口名称都匹配。
因此,您的WindowName不可能是"Mozilla“,因为Firefox窗口的标题永远不是"Mozilla”,但是它可能是"Mozilla起始页面- Mozilla“,或者取决于网页的名称。这是一个例子
因此,您的代码应该是这样的(下面的代码只工作-只工作,如果您有确切的窗口名称:"Mozilla - Mozilla“,如上图所示。我已经在Windows8.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.
}
https://stackoverflow.com/questions/16530871
复制相似问题