Copyright Notice
Respect Original Works. Reprinted, please maintain the integrity of the article, and in the form of a hyperlink to indicate the original author "tingsking18" and the main site address, so that other friends can ask and correct.
First, download qtwinmigrate-2.8-opensource.zip from the qtwebsite, And Then inherit a class from qwinhost.
Class hostwindow: Public qwinhost <br/>{< br/> q_object <br/> Public: <br/> hostwindow (qwidget * parent = 0, QT :: wflags f = 0) <br/>: qwinhost (parent, f) <br/>{< br/> setfocuspolicy (QT: strongfocus ); <br/>}</P> <p> hwnd createwindow (hwnd parent, hinstance instance) <br/>{< br/> static atom windowclass = 0; <br/> If (! Windowclass) <br/>{< br/> wndclassex wcex; </P> <p> wcex. cbsize = sizeof (wndclassex); <br/> wcex. style = cs_hredraw | cs_vredraw; <br/> wcex. lpfnwndproc = (wndproc) wndproc; <br/> wcex. cbclsextra = 0; <br/> wcex. cbwndextra = 0; <br/> wcex. hinstance = instance; <br/> wcex. hicon = NULL; <br/> wcex. hcursor = loadcursor (null, idc_arrow); <br/> wcex. hbrbackground = (hbrush) (color_window + 1); <br/> wcex. lpszmenuname = NULL; <br/> Wcex. lpszclassname = l "qtest"; <br/> wcex. hiconsm = NULL; </P> <p> windowclass = registerclassex (& wcex); <br/>}</P> <p> hwnd = createwindow (tchar *) windowclass, 0, ws_child | ws_clipsiblings | ws_tabstop, <br/> cw_usedefault, 0, cw_usedefault, 0, parent, null, instance, null); <br/> return hwnd; <br/>}</P> <p> signals: <br/> void message (const qstring & MSG, int timeout); </P> <p> Public slots: <br/> void retu Rnpressed () <br/>{< br/> qmessagebox: Information (toplevelwidget (), "Message from QT", "Return pressed in qlineedit! "); <Br/>}</P> <p> protected: <br/> static lresult callback wndproc (hwnd, uint message, wparam, lparam) <br/>{< br/> qwidget * widget = qwidget: Find (getparent (hwnd )); <br/> hostwindow * window = qobject_cast <postwindow *> (widget); </P> <p> If (window) Switch (Message) <br/>{</P> <p> case wm_lbuttondown: <br/> window-> message ("clicked", 1000); <br/> break; <br/> case wm_setfocus: <br/> window-> Message ("setfocus for Win32 window! ", 1000); <br/> break; <br/> case wm_killfocus: <br/> window-> message (" killfocus for Win32 window! ", 1000); <br/> break; <br/> case wm_mousemove: <br/> window-> message (" moving the mouse, aren't we? ", 200); <br/> break; <br/> case wm_keydown: <br/> If (wparam! = Vk_tab) <br/> window-> message ("Key pressed! ", 500); <br/> break; <br/> default: <br/> return defwindowproc (hwnd, message, wparam, lparam ); <br/>}< br/> return 0; <br/>}< br/> };
To explain the above hostwindow class,
1. The hostwindow class inherits qwinhost and implements the virtual function createwindow () defined in qwinhost. createwindow () calls the API function registerclassex to register the window class. Then, call the Windows API function createwindow to create a window.
2. The following signals and slots are simple. Singals is a signal to display information for status; slots is the confirmation information for qlineedit.
3. The wndproc function is also very simple. It is the callback function of registerclassex. Used to process Windows messages.
Note:
BecauseWndprocIs static, so we can use qwidget: FindHwnd to qwidget.
Qwinhost is used to process messages. Therefore, we use qobject_cast
The following is the call method:
# Include "Main. MOC "</P> <p> int main (INT argc, char ** argv) <br/> {<br/> qapplication A (argc, argv ); </P> <p> qmainwindow MW; <br/> mW. menubar ()-> addmenu ("& file")-> addaction ("& Exit", & A, slot (quit ())); </P> <p> qwidget Central (& MW); </P> <p> qlineedit edit (hierarchical RAL); <br/> hostwindow host (hierarchical RAL ); <br/> qobject: connect (& host, signal (message (const qstring &, INT), mW. statusbar (), slot (showmessage (const qstring &, INT); <br/> qobject: connect (& edit, signal (returnpressed (), & host, slot (returnpressed (); </P> <p> qvboxlayout vbox (partition RAL); <br/> vbox. addwidget (& edit); <br/> vbox. addwidget (& host); </P> <p> mW. setcentralwidget (Rule RAL); <br/> mW. show (); <br/> return a.exe C (); <br/>}< br/>
Note the following:
When our class is implemented in the. cpp file and contains the q_object macro. We need to # include "Main. MOC" when using it"
Contains the. MoC file. Because the. MoC file is the result after MOC expands the q_object macro. If we compile it directly, the compiler does not know it.
Q_object macro.