--MFC Example of policy mode

Source: Internet
Author: User

Context (Application scenario):
1, need to use the algorithm provided by Concretestrategy.
2, internal maintenance of a strategy instance.
3, responsible for the dynamic setting execution strategy detailed implementation algorithm.
4, responsible for the interaction and data transfer with strategy.
Strategy (Abstract policy Class):
1, defines a public interface, a variety of different algorithms to implement this interface in different ways, the context uses this interface to invoke different algorithms, generally using an interface or abstract class implementation.
Concretestrategy (Detailed policy Class):

2. Realize the interface of strategy definition and provide detailed algorithm implementation.

UML Class Diagrams:


MFC sample implementation of key class code:

Strategy_mfcdlg.cpp: Implement file//#include "stdafx.h" #include "strategy_mfc.h" #include "strategy_mfcdlg.h" #include " Afxdialogex.h "#ifdef _debug#define new debug_new#endif//for Application" about "menu item CAboutDlg dialog box class Caboutdlg:public cdialogex{    Public:caboutdlg ();//Dialog data enum {IDD = idd_aboutbox};p rotected:virtual void DoDataExchange (cdataexchange* pDX); DDX/DDV Support//Implement PROTECTED:DECLARE_MESSAGE_MAP ()}; Caboutdlg::caboutdlg (): CDialogEx (Caboutdlg::idd) {}void CAboutDlg::D odataexchange (cdataexchange* pDX) {CDialogEx: :D Odataexchange (PDX);} Begin_message_map (CAboutDlg, CDialogEx) End_message_map ()//Cstrategy_mfcdlg dialog box Cstrategy_mfcdlg::cstrategy_mfcdlg (cwnd* pparent/*=null*/): CDialogEx (Cstrategy_mfcdlg::idd, pparent) {m_hicon = AfxGetApp ()->loadicon (IDR_ MAINFRAME);} void Cstrategy_mfcdlg::D odataexchange (cdataexchange* pdx) {cdialogex::D odataexchange (PDX);DD X_control (PDX, idc_ Edit_price, M_edit_price);DD X_control (PDX, Idc_edit_num, M_editnum);DD X_control (PDX, Idc_edit_show, m_editshow);DD x _contrOL (PDX, Idc_combo1, M_comboboxcomp);DD X_control (PDX, Idc_edit_total, m_edittotal);} Begin_message_map (Cstrategy_mfcdlg, CDialogEx) On_wm_syscommand () On_wm_paint () On_wm_querydragicon () ON_BN_CLICKED (Idc_button_sure, &cstrategy_mfcdlg::onbnclickedbuttonsure) On_bn_clicked (Idc_button_clear, &cstrategy_mfcdlg::onbnclickedbuttonclear) End_message_map ()//CSTRATEGY_ MFCDLG Message Handler bool Cstrategy_mfcdlg::oninitdialog () {cdialogex::oninitdialog ();//Add the "About ..." menu item to the System menu. Idm_aboutbox must be within the scope of the system command. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox); ASSERT (Idm_aboutbox < 0xf000); cmenu* Psysmenu = GetSystemMenu (FALSE); if (psysmenu! = NULL) {BOOL bnamevalid; CString straboutmenu;bnamevalid = straboutmenu.loadstring (Ids_aboutbox); ASSERT (Bnamevalid), if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);p Sysmenu->appendmenu ( Mf_string, Idm_aboutbox, Straboutmenu);}} Sets the icon for this dialog box. When the application main form is not a dialog box, the framework will itself take the initiative//Run this action SetIcon (M_hicon, TRUE);//Set Large icon SetIcon (M_hicon, FALSE);//Set small icon//TODO: Join here?Outside of the initialization code m_comboboxcomp.addstring (_t ("normal charge")) m_comboboxcomp.addstring (_t ("hit 80 percent")) M_comboboxcomp.addstring (_T ("  300 yuan to 100 Yuan ")); Total=0.0;return TRUE; Returns true}void Cstrategy_mfcdlg::onsyscommand (UINT nid, LPARAM LPARAM) {if (NID & 0xfff0) = = Idm_aboutb unless the focus is set to the control OX) {CAboutDlg dlgabout;dlgabout.domodal ();} Else{cdialogex::onsyscommand (NID, LParam);}} Suppose you add a button to a dialog box, you need the following code//to draw the icon. For MFC applications that use the document/view model,//This will be completed by the framework itself. void Cstrategy_mfcdlg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for drawing SendMessage (WM_ICONERASEBKGND, Reinterpret_cast<wparam> (DC. GETSAFEHDC ()), 0);//Make the icon centered in the workspace rectangle int cxicon = GetSystemMetrics (sm_cxicon); int cyicon = GetSystemMetrics (Sm_cyicon); CRect rect; GetClientRect (&rect); int x = (rect. Width ()-Cxicon + 1)/2;int y = (rect. Height ()-Cyicon + 1)/2;//draw icon DC. DrawIcon (x, y, m_hicon);} Else{cdialogex::onpaint ();}} The system calls this function to get the cursor//display when the user drags the minimized form. Hcursor Cstrategy_mfcdlg::onquerydragicon () {return static_cast
references: Geoscience "big talk design mode"

--MFC Example of policy mode

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.