HWND windowHandle = FindWindow(nullptr, "Window Title");报错form.cpp:22:25: No matching function for call to 'FindWindowW' winuser.h:3942:26: candidate function not viable: no known conversion from 'const char[13]' to 'LPCWSTR' (aka 'const wchar_t *') for 2nd argument
时间: 2024-04-25 17:25:58 浏览: 15
这个错误是因为FindWindow函数的第二个参数需要传入一个LPCWSTR类型的参数,而你传入了一个const char*类型的参数。解决这个问题的方法是将"Window Title"转换成LPCWSTR类型。
你可以使用以下两种方法中的任意一种来解决这个问题:
1. 使用L"Window Title"代替"Window Title",这将会将字符串转换成LPCWSTR类型。例如:
HWND windowHandle = FindWindow(nullptr, L"Window Title");
2. 使用MultiByteToWideChar函数将"Window Title"转换成LPCWSTR类型。例如:
const char* windowTitle = "Window Title";
int length = strlen(windowTitle) + 1;
int size = MultiByteToWideChar(CP_UTF8, 0, windowTitle, length, nullptr, 0);
std::wstring wideWindowTitle(size, 0);
MultiByteToWideChar(CP_UTF8, 0, windowTitle, length, &wideWindowTitle[0], size);
HWND windowHandle = FindWindow(nullptr, wideWindowTitle.c_str());
相关问题
GetWindowText(hwnd, title.data(), title.size());form.cpp:24:9: No matching function for call to 'GetWindowTextW' winuser.h:3638:25: candidate function not viable: no known conversion from 'QChar *' to 'LPWSTR' (aka 'wchar_t *') for 2nd argument
这个错误的原因是 GetWindowText 函数需要的第二个参数是 wchar_t* 类型,而 QString::data() 函数返回的是 QChar* 类型。
解决这个问题的方法是将 QString 转换为 wchar_t* 类型。可以使用 QString::toStdWString() 函数将 QString 转换为 std::wstring 类型,然后使用 std::wstring::c_str() 函数将 std::wstring 转换为 wchar_t* 类型。
以下是修改后的代码示例:
```c++
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
textEdit = new QTextEdit(this);
layout->addWidget(textEdit);
setLayout(layout);
QVector<HWND> windows;
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows));
for (auto hwnd : windows)
{
wchar_t title[1024];
GetWindowText(hwnd, title, sizeof(title));
textEdit->append(QString::fromWCharArray(title));
}
}
BOOL CALLBACK MainWindow::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
QVector<HWND>* windows = reinterpret_cast<QVector<HWND>*>(lParam);
if (IsWindowVisible(hwnd))
{
windows->push_back(hwnd);
}
return TRUE;
}
```
在这个示例程序中,我们将获取窗口标题的部分修改为了使用 wchar_t 类型的 title 数组,并将其传递给 GetWindowText 函数。然后,我们使用 QString::fromWCharArray() 函数将 wchar_t* 类型的标题转换为 QString 类型并添加到 QTextEdit 控件中。
HWND windowHandle = FindWindow(nullptr, "Window Title"); if (windowHandle != nullptr) { RECT rect; if (GetWindowRect(windowHandle, &rect)) { qDebug() << "Window Position: " << rect.left << "," << rect.top; qDebug() << "Window Size: " << rect.right - rect.left << "x" << rect.bottom - rect.top; } }有相同错误如何修改
你可以使用以下两种方法中的任意一种来解决这个问题:
1. 使用L"Window Title"代替"Window Title",这将会将字符串转换成LPCWSTR类型。例如:
HWND windowHandle = FindWindow(nullptr, L"Window Title");
if (windowHandle != nullptr) {
RECT rect;
if (GetWindowRect(windowHandle, &rect)) {
qDebug() << "Window Position: " << rect.left << "," << rect.top;
qDebug() << "Window Size: " << rect.right - rect.left << "x" << rect.bottom - rect.top;
}
}
2. 使用MultiByteToWideChar函数将"Window Title"转换成LPCWSTR类型。例如:
const char* windowTitle = "Window Title";
int length = strlen(windowTitle) + 1;
int size = MultiByteToWideChar(CP_UTF8, 0, windowTitle, length, nullptr, 0);
std::wstring wideWindowTitle(size, 0);
MultiByteToWideChar(CP_UTF8, 0, windowTitle, length, &wideWindowTitle[0], size);
HWND windowHandle = FindWindow(nullptr, wideWindowTitle.c_str());
if (windowHandle != nullptr) {
RECT rect;
if (GetWindowRect(windowHandle, &rect)) {
qDebug() << "Window Position: " << rect.left << "," << rect.top;
qDebug() << "Window Size: " << rect.right - rect.left << "x" << rect.bottom - rect.top;
}
}