在QT应用程序中使用win32的窗体
时间:2010-06-02 来源:landuochong
http://blog.csdn.net/tingsking18/archive/2009/11/07/4781619.aspx
首先要从QT的网站下载qtwinmigrate-2.8-opensource.zip,然后从QWinHost继承一个类
view plaincopy to clipboardprint?- class HostWindow : public QWinHost
- {
- Q_OBJECT
- public:
- HostWindow(QWidget *parent = 0, Qt::WFlags f = 0)
- : QWinHost(parent, f)
- {
- setFocusPolicy(Qt::StrongFocus);
- }
- HWND createWindow(HWND parent, HINSTANCE instance)
- {
- static ATOM windowClass = 0;
- if (!windowClass)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = instance;
- wcex.hIcon = NULL;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = L"qtest";
- wcex.hIconSm = NULL;
- windowClass = RegisterClassEx(&wcex);
- }
- HWND hwnd = CreateWindow((TCHAR*)windowClass, 0, WS_CHILD|WS_CLIPSIBLINGS|WS_TABSTOP,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, parent, NULL, instance, NULL);
- return hwnd;
- }
- signals:
- void message(const QString &msg, int timeout);
- public slots:
- void returnPressed()
- {
- QMessageBox::information(topLevelWidget(), "Message from Qt", "Return pressed in QLineEdit!");
- }
- protected:
- static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- QWidget *widget = QWidget::find(GetParent(hWnd));
- HostWindow *window = qobject_cast<HostWindow*>(widget);
- if (window) switch (message)
- {
- case WM_LBUTTONDOWN:
- window->message("clicked", 1000);
- break;
- case WM_SETFOCUS:
- window->message("SetFocus for Win32 window!", 1000);
- break;
- case WM_KILLFOCUS:
- window->message("KillFocus for Win32 window!", 1000);
- break;
- case WM_MOUSEMOVE:
- window->message("Moving the mouse, aren't we?", 200);
- break;
- case WM_KEYDOWN:
- if (wParam != VK_TAB)
- window->message("Key Pressed!", 500);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- };
解释一下上面的HostWindow类,
1. HostWindow类继承了QWinHost并且实现了在QWinHost中定义的虚函数createWindow(),createWindow()调用API函数RegisterClassEx注册窗口类。然后再调用Windows的API函数CreateWindow来创建窗口。
2. 下面的signals和slots都很简单。singals是给status发出显示信息的信号;slots是QLineEdit的确认信息。
3. WndProc函数也很简单,是RegisterClassEx的回调函数。用来处理windows消息的。
注意:
由于WndProc是static的,所以我们可以使用QWidget::find来将HWND转换为QWidget。
处理消息的时候要使用QWinHost所以我们通过qobject_cast<HostWindow*>将QWidget转换为HostWindow。
下面是调用方法:
view plaincopy to clipboardprint?- #include "main.moc"
- int main(int argc, char **argv)
- {
- QApplication a(argc, argv);
- QMainWindow mw;
- mw.menuBar()->addMenu("&File")->addAction("&Exit", &a, SLOT(quit()));
- QWidget central(&mw);
- QLineEdit edit(¢ral);
- HostWindow host(¢ral);
- QObject::connect(&host, SIGNAL(message(const QString&,int)), mw.statusBar(), SLOT(showMessage(const QString&,int)));
- QObject::connect(&edit, SIGNAL(returnPressed()), &host, SLOT(returnPressed()));
- QVBoxLayout vbox(¢ral);
- vbox.addWidget(&edit);
- vbox.addWidget(&host);
- mw.setCentralWidget(¢ral);
- mw.show();
- return a.exec();
- }
大家要注意一点:
我们的class在.cpp文件中实现的时候,并且包含Q_OBJECT宏的时候。我们在使用的时候要#include "main.moc"
包含.moc文件。因为.moc文件是moc将Q_OBJECT宏展开后的结果。如果我们直接编译的话,编译器是不认识
Q_OBJECT宏的。
相关阅读 更多 +