Creating an MFC framework using AfxRegisterWndClass, create instead of LoadFrame

Source: Internet
Author: User

For the MFC Single Document program (SDI), in the initialization of CWinApp's InitInstance function, it is really easy to create the main frame using the LoadFrame function. But the flexibility is very poor, if the software has special needs, or use afxregisterwndclass create more flexible, arbitrary. There are 3 key steps:

1. Using AfxRegisterWndClass to register the main window, you can pass in the hicon handle as needed;

2. Creates a window with the CREATE function of the object of the CFrameWnd class or its subclasses, where you can pass in the hmenu to load the menu bar;

3. Finally do not forget to send wm_initupdate messages to all sub-windows in the header file <afxpriv.h>.

OK, the direct sticker code is intuitive:

1cmainframe* pframe =NewCMainFrame;2     if(!pframe)3         returnFALSE;4m_pMainWnd =pframe;5     //Create and load a framework and its resources6 //Pframe->loadframe (Idr_mainframe,ws_overlappedwindow |  Fws_addtotitle, Null,null); //a convenient way//i.e. Cwnd::createex (HMenu ...)7     8     //If you do a full-screen software, do not load menus, toolbars, you can use Afregisterwndclass9 Ten     //1. OneHicon Hicon =LoadIcon (idr_mainframe); ALPCTSTR Lzclass = AfxRegisterWndClass (cs_dblclks|cs_vredraw|Cs_hredraw,null,null,hicon); - /* - //2. the pframe->create (Lzclass, _t ("Employee Management System")); There is no menu bar at this time and no messages are sent to the self-exposure.  - HMENU HMENU = LoadMenu (M_hinstance, Makeintresource (idr_mainframe)); - :: SetMenu (Pframe->m_hwnd, hMenu); - */ +     //3. You can do this: -Pframe->create (Lzclass, _t ("Employee Management System"), Ws_overlappedwindow,cframewnd::rectdefault,null,makeintresource (Idr_mainframe));  +Pframe->loadacceltable (Makeintresource (idr_mainframe));//loading shortcut keys APframe->sendmessagetodescendants (Wm_initialupdate,0,0, true,true);//message distribution mechanism, true to send all child windows at  -     //from 1 to 3 MFC only needs LoadFrame function can be done, so it is convenient, but not flexible, if the software has special needs, use the above code
Sendmessagetodescendants () sends the WM_INITUPDATE message back to all descendants, similar to the OnInitDialog () of the dialog box program.

OK, the above just creates the CMainFrame framework class in CWinApp, and we can create our own CView view class in the CMainFrame class according to our own requirements. This can probably be divided into 2 steps, I understand:
1. Create your own view class, can inherit from Ceditview,cricheditview,clistview, CTreeView, Cformview,chtmlview and so on, according to the need to decide, and then to rewrite the inside of the Oninitupdate ( ) function Ah, is some of the initial chemical made;
2. It is necessary to create our view class, which can be in the OnCreate function of CMainFrame, or in oncreateclient, usually in the latter:
1 BOOL cmainframe::oncreateclient (lpcreatestruct lpcs, ccreatecontext* pContext)2{  3    ccreatecontext cc; 4     Cc.m_pnewviewclass = runtime_class (cmainview); 5     M_pmainview = (cmainview*) createview (&CC);  // combining frame and view closely  with the LoadFrame function is the same as loading the view.  67     return  cframewnd::oncreateclient (lpcs, pContext); 8 }

Specifically, you first create the context CCreateContext struct, and then populate your new view class:

1 structCCreateContext//Creation Information Structure2     //All fields is optional and may be NULL3 {4     //For creating new views5Cruntimeclass* M_pnewviewclass;//Runtime class of view to create or NULL6cdocument*M_pcurrentdoc;7 8     //For creating MDI Children (Cmdichildwnd::loadframe)9cdoctemplate*m_pnewdoctemplate;Ten  One     //For sharing view/frame state from the original View/frame Acview*M_plastview; -cframewnd*M_pcurrentframe; -  the //Implementation - CCreateContext (); -};

My CMainView is inherited from the CListView, you can get the CListCtrl control object inside. You can use CreateView to create a view (if you want to record a cmainview pointer), or you can

The CC object is passed to the parent class CFrameWnd::OnCreateClient (LPCs, pContext); PContext to create.



Record.

Creating an MFC framework using AfxRegisterWndClass, create instead of LoadFrame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.