Object-oriented Windows programming actual combat (below)

xiaoxiao2021-03-06  89

2. WINDOW class, we implemented a WinApp class, which has a WNDCLASSEX type member variable, with a constructor of the initialization member variable, with a registered member function below, we have to package a window class, in WinApp On the basis, create a window, display window. Window class definition: //Window.h# ifef window_h __ # Define window_h __ / * name: window.h Copyright: (c) Huyoo, Changsha, Hunan Province, China Author: huyoo email: huyoo353 @ Hotmail.com Tel: 0731-4534289 Date: 2004-10-13 01:28 Description: Definition window * /// # prgma overce # include

/ / -------------------------------------------------------------------------------------------- --------------------------- Class Window {public: // Default constructor declaration window WINDOW (); // Using Create () The method of initializing window HWND Create (HINSTANCE hinst, LPCTSTR clsname, LPCTSTR wndname, HWND parent = NULL, DWORD dStyle = WS_OVERLAPPEDWINDOW, DWORD dXStyle = 0L, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT, int width = CW_USEDEFAULT, int height = CW_USEDEFAULT) ;

// Display window bool show (int dcmdshow = sw_shownormal);

// Get the unique positioning identity of the window in the application, the operator is Operator HWnd ();

Protected: // This window handle hwnd _hwnd;}; // ------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- # Endif

It can be seen that the above definition is simple ~~~ Many functions believe that everyone will have seen it, let's take it !! ~~

#include "window.h" / * name: window.cpp Copyright: (c) Huyoo, Changsha, Hunan Province, China Author: huyoo email: huyoo353@hotmail.com tel: 0731-4534289 date: 2004-10-13 01 : 28 DESCRIPTION: Implement window * /// --------------------------------------- ------------------------------------ WINDOW :: Window () {// State a default method Window, make the handle as null, because there is no window yet !! ~ _hWnd = null;} // --------------------------- ------------------------------------------------ hwnd window :: Create (Hinstance Hinst, LPCTSTR CLSNAME, LPCTSTSTSTSTSTSNAME, LPCTSTSTYLE, DWORD PARENT, DWORD DSTYLE, DWORD DXSTYLE, INT X, INT Y, INT WIDTH, INT Height) {// crete () method uses a more powerful createWindowex () function Create a new window. // General, I like and worship the power / / / to assign the returned handle to the member variable _hWnd = CREATEWINDOWEX (DxStyle, Clsname, Width, DStyle, x, y, width, height, parent, NULL, HINST, NULL; // The window is correctly created, the handle should not be empty if (_hwnd! = Null) return _hwnd; // The creation window is wrong, the handle is empty, I have to return, who is able to open the white wolf Return null;} // ------------------------- -------------------------------------------------- Bool Window :: Show (int DCMDSHOW) {// Display and update window IF (ShowWindow (_HWND, DCMDSHOW) && updateWindow (_HWND)) Return true; return false;} // ----------- -------------------------------------------------- -------------- WINDOW :: Operator hWnd () {// Return to our unique logo this window to use the handle return_hwnd;} // -------- -------------------------------------------------- -----------------

This is simple. With WinApp and Window class, we only need a Winmain () function, then what is hesitant? Hurry up !! Add a WinMain () function, start our application trip ~~~ 3.WinMain () function and main program //main.cpp / * name: main.cpp Copyright: () Huyoo, Changsha, Hunan Province, China Author: huyoo email: huyoo353@hotmail.com tel: 0731-4534289 DATE : 2004-10-13 01:28 Description: Main program * / # include #include "winApp.h" #include "window.h" // ------------ -------------------------------------------------- ------------- Int WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinst, LPSTR LPCMDLINE, INT NCMDSHOW) {msg msg; char * clsname = "Win32CPP"; char * wndname = "Object-oriented Windows Program "

// Initialize application class WinApp theApp (Hinstance, Clsname); theApp.register ();

// Create a main window Window mainwnd; mainwnd.create (hinstance, clsname, wndname); // Display window mainwnd.show ();

// Handling and matching message while (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);

Return 0;} // -------------------------------------------- ---------------------------- 4. Screenshot

Let's take a look !!! ~~ 5. Subsequent topics here, we only have a single window, no resources and standard controls, we also need to do multi-window programs and dialog box programs, we also want to implement the ActiveX control, Realize the COM interface ... Ren, a long way ~~~ Here I have an idea, give you a good reference. The window must be a window process and its member points to one window process, since I will put the window here. Class definitions and window processes are encapsulated in WinApp, then implement multi-window, as long as they add other sub-window classes and sub-window procedures in WinApp, I hope everyone will be successful !! ~~~

PS: If you copy the code here, create a Win32 Application, add the source code here, please be careful when compiling, please do not let the code are in the comment, because the online line break is not very good .. I hope that you can successfully compile, and see the window I have seen. Thank you for your patience, I'm paying tribute to you !!! ~~~~

转载请注明原文地址:https://www.9cbs.com/read-106760.html

New Post(0)