C++ QT 与 win32 窗口可以互操作
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->setWindowTitle("天下第一剑");
// HWND hwnd = FindWindow(L"天下第一剑",L"天下第一剑");
// qDebug()<<hwnd;EnumWindows([](HWND hwnd, LPARAM lParam)->WINBOOL{qDebug() <<hwnd;WCHAR title[100];GetWindowText(hwnd,title,100);qDebug() << QString::fromWCharArray(title);},0);WCHAR title[100];GetWindowText((HWND)this->winId(),title,100);qDebug() << QString::fromWCharArray(title);auto wndProc = [](HWND hwnd,UINT uMsg,WPARAM p1,LPARAM p2)->LRESULT{//lambda的局部变量必须定义在最前面否则报错,//是不是与switch有关?PAINTSTRUCT ps;RECT rect;HDC hdc;switch (uMsg) {case WM_PAINT:hdc = BeginPaint(hwnd,&ps);GetClientRect(hwnd,&rect);DrawText(hdc,L"天下无敌的 天剑术",-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);EndPaint(hwnd,&ps);break;case WM_QUIT:PostQuitMessage(0);break;case WM_DESTROY:PostQuitMessage(0);break;default:break;}return DefWindowProc(hwnd, uMsg, p1, p2);};//有点问题这个强转 lambda => funptr => longSetWindowLongPtr((HWND)this->winId(),GWLP_WNDPROC,reinterpret_cast<LONG_PTR>(static_cast<LRESULT(*)(HWND, UINT, WPARAM, LPARAM)>(wndProc)));}