我被困在使用FindWindowW(NULL, "program name")
函数时搜索中文程序名称的问题。
当我搜索英语时,它可以完美运行。
有人可以给我一个有关如何使用Unicode搜索的线索吗?
我还没弄清楚,有人可以指导我怎么做吗?
#include <windows.h>
#include <stdio.h>
int main(){
HWND hWnd = FindWindowW(NULL,L"\uAA5A\uAA4C\uB873\uAB4C\uB6C7");
if(NULL == hWnd){
printf("NotFound!");
}else {
printf("Found!");
}
}
最佳答案
使用FindWindow的Unicode(宽)版本,并使用宽字符串进行搜索。我还建议以UTF-8编码保存源,并为Microsoft编译器使用/utf-8
编译器开关。否则,编译器将采用本地化的ANSI编码来解释宽字符串。如果您本地化的编码是中文变体,那很好,但是如果您使用的是美国或西欧版本的Windows,并且在字符串常量中使用中文字符,Microsoft IDE可能会提示您保存为UTF-16:
例:
#include <windows.h>
#include <stdio.h>
int main(void)
{
//HWND h = FindWindowW(NULL,L"马克"); // works if saved in UTF-8 encoding
// // and compiled with /utf-8.
HWND h = FindWindowW(NULL,L"\u9a6c\u514b");
if(h == NULL)
printf("err = %ld\n",GetLastError());
else
printf("handle = %p\n",h);
}
在Windows上,我将终端窗口更改为带有
title 马克
的匹配中文标题,此代码找到了该窗口:C:\>title 马克
C:\>test
handle = 00000000000B0258
C:\>test
handle = 00000000000B0258
Microsoft的Spy ++工具确认了此句柄:
关于c++ - 如何在C++中使用FindWindow在Unicode中进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703011/