An MFC control
1 Control Introduction
1.1 Static controls include pictures, static text, and group boxes. The ID of the generated control is unified
Idc_static. Rarely accessed and manipulated in programs, just using static controls to display information
1.2 edit box control, the control class is CEdit, which typically uses it to receive input from the user, showing
Information to the user.
1.3 button controls include general buttons, check buttons, and cell buttons, and control classes are CButton
Radio buttons Note that the number of buttons in the same group is sequential (Ctrl+d displays the tab order), and the number is small
button to set the Group property to implement multiple radio buttons in the same dialog box.
Multiple groupings.
1.4 Combo box (drop-down box), the control class is CComboBox. You can select one of several options
You can also enter a new option. Data items in a combo box can be added through attributes.
In the Data tab of the Properties dialog box, when you add an item, the line break is CTRL + ENTER.
Note When the control is set, a sufficient height is given.
1.5 list box, control class is CListBox, support single and multi-select.
1.6 Animation control, the control class is CAnimateCtrl. Ability to play a simple frame animation.
2 Examples of playing animations
2.1 Add the message handler function to open the file button:
2.1.1 Popup Open File dialog box
Open File dialog box CFileDialog class, constructor
CFileDialog (
BOOL Bopenfiledialog, whether the identity is open or Save As dialog box
LPCTSTR LPSZDEFEXT = null,//default extension
LPCTSTR lpszfilename = null,//Default file name
The style of the DWORD dwflags//dialog box
LPCTSTR lpszfilter = null,//Set file type for type filtering
cwnd* pParentWnd = NULL//parent window, set to null.
);
Set file type string format:
1 string with "| |" As a terminator.
2 with "|" Between each data item Segmentation
3 Each item consists of two parts, the first part is the display part, the second part sets the filter
Suffix, these two parts are also in the "|" Segmentation.
"Video file (*.avi) |*.avi| All Files (*. *) |*.*| |";
2.1.2 Show file path in edit box
To bind a member variable of a value type to an edit box control, assign a file path to the variable
2.1.3 Add a file name to the file list
To bind a variable of the control type to a ListBox control, call the AddString () function to add
The file name.
2.1.4 playing an animated file
To bind a variable of the control type to the animation control, call its open, play function to play
2.2 Double-click Play function in List control
2.2.1 Use the Class Wizard to add a double-click Message handler for a list control, in a function,
You want to get the file path for the selection, so in 2.1, the list control
Add file name, append data, add file path at the same time
2.2.2 Gets the file path and implements the double-click Playback function. Note that for the playback function,
Extracted into the play function.
2.3 Set the Repeat playback function
2.3.1 is a variable of a check box, combo box bound control type.
2.3.2 in the OnInitDialog () function of the dialog box, set the combo box control to
Not available.
2.3.3 Double-click the check box button to generate the message handler function, in the function, according to the
Check Status of the checkbox, setting the status of the component box
2.3.4 Modify the Play () function to set the number of plays based on the values of the two controls above
2.4 Add radio button to play and stop the message handler function, play just call play function
Instead of stopping, simply call the animation control's stop function.
3 How static controls and radio buttons implement binding operations
3.1 Static controls need to modify ID
3.2 radio buttons you need to set the group property, and each group of radio buttons binds a variable.
4 Common functions for combo boxes and list boxes
4.1 Adding data items
AddString
4.2 Deleting data items
DeleteString
4.3 Finding a string
FindString
If it is not found, the combo box returns CB_ERR, and the list box returns LB_ERR
4.4 Setting/Getting additional data for data items
Setitemdata/getitemdata
4.5 Set/Get current selection
Setcursel/getcursel
...
5 Example of completing a list box
CListBox, the numerical binding computes the addition operation.
An MFC control Introduction
1 Control Introduction
1.1 Spinner button (swivel button)
Usually with the edit box to complete the relevant functions, before use, set the properties of the spinner, increase or decrease, and edit the box to form a partnership, you can also set the alignment. Ctrl+d Show tab order, edit box and fine tune to adjacent, fine-tune style, sort select "Right", select checkbox "Auto Companion", "Set companion integer"
1.2 Progress bar
This is typically used for setup or for copying files to show the progress of completion.
1.3 Sliding block
You can set the scale by property to display a function that can adjust the value.
2 controls use
2.1 These three controls are usually required to set the value range of a table before they are used
Setrange/getrange
Setrange32/getrange32
2.2 Setting the stride of a control
Cprogressctrl::setstep
Csliderctrl::setpagesize
Csliderctrl::setlinesize
Cspinbuttonctrl::setaccel
2.3 Set/Get current position
Setpos/getpos
Two-list control
1 related classes
The clistctrl-parent class is a CWnd, one of the control classes that is commonly used on dialog boxes.
The clistview-parent class is a cctrlview, one of the view classes.
Clistview=cview+clistctrl,clistview equivalent to
A CListCtrl control was added to the client area of CView. Call
Clistview::getlistctrl, gets the control.
2 Use of CListCtrl
2.1 Design and set the list of icons for the control (large icons and small icons)
2.1.1 Add bitmap resource, design icon list
2.1.2 Creating CImageList
Cimagelist::create
2.1.3 to set the icon list to the control
Clistctrl::setimagelist
2.1.4 Adding columns
Clistctrl::insertcolumn
2.1.5 Adding data items
Clistctrl::insertitem
2.1.6 Setting the text of a column
Clistctrl::setitemtext
2.1.7 Set/Get additional data for a data item
Clistctrl::setitemdata/getitemdata
2.1.8 setting the background picture of a control
Clistctrl::setbkimage
Note: The initialization of the OLE library is required before calling the function
AfxOleInit ();
In addition, it is often necessary to set the background color of the font to transparent
M_wndlist.settextbkcolor (Clr_none);
Example 1:
1. New MFC dialog box Application Dlgctrl.
2. Visual editing Interface control
3. Ctrl+w the member variable corresponding to the bound control
4. Ctrl+w The Click event of a double-click event and button for a bound list box item
5. The main implementation code in ***DLG.CPP (partially auto-generated)
#include "stdafx.h" #include "DlgCtrl.h" #include "DlgCtrlDlg.h" #ifdef _debug#define new Debug_new#undef This_ Filestatic char this_file[] = __file__; #endif///////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App aboutclass caboutdlg:public Cdialog{public:caboutdlg ();//Dialog Data//{{afx_da TA (CAboutDlg) enum {IDD = Idd_aboutbox};//}}afx_data//ClassWizard generated virtual function overrides//{{afx_virtual ( CABOUTDLG) protected:virtual void DoDataExchange (cdataexchange* pDX); DDX/DDV support//}}afx_virtual//implementationprotected://{{afx_msg (CABOUTDLG)//}}AFX_MSGDECLARE_MESSAGE_MAP ( )}; Caboutdlg::caboutdlg (): CDialog (Caboutdlg::idd) {//{{afx_data_init (CAboutDlg)//}}afx_data_init}void CABOUTDLG::D Odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map (CAboutDlg)//}}afx_data_map} Begin_message_map (CAboutDlg, CDialog)//{{afx_msg_map (CABOUTDLG)/No MESSAGE Handlers//}}afx_msg_mapend_message_ MAP ()////////////Cdlgctrldlg Dialogcdlgctrldlg::cdlgctrldlg ( cwnd* pparent/*=null*/): CDialog (Cdlgctrldlg::idd, pparent) {//{{afx_data_init (cdlgctrldlg) M_strfilepath = _T ("");// }}afx_data_init//Note that LoadIcon does not require a subsequent destroyicon in Win32m_hicon = AfxGetApp ()->loadicon ( IDR_MAINFRAME);} void Cdlgctrldlg::D odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map (CDLGCTRLDLG) DDX_Control (PDX, Idc_playtimes, M_cboplaycount);DD X_control (PDX, Idc_replay, M_btnreplay);DD X_control (PDX, IDC_ VIDEO, M_animatevideo);DD X_control (PDX, Idc_list_filename, M_listfilename);DD x_text (PDX, Idc_edit_filepath, m_ strFilePath);//}}afx_data_map}begin_message_map (Cdlgctrldlg, CDialog)//{{afx_msg_map (CDLGCTRLDLG) ON_WM_ Syscommand () On_wm_paint () On_wm_querydragicon () on_bn_clicked (Idc_open_file, Onopenfile) ON_LBN_DBLCLK (IDC_LIST_ FILENAME, Ondblclklistfilename) on_wm_close () on_bn_clicked (Idc_replay, Onreplay) On_bn_clickeD (Idc_play, Onplay) on_bn_clicked (Idc_stop, OnStop)//}}afx_msg_mapend_message_map ()////////////////////////////// Cdlgctrldlg message Handlersbool Cdlgctrldlg::oninitdialog () { CDialog::OnInitDialog ();//Add "About ..." menu item to System menu.//Idm_aboutbox must is in the system command Range.ass ERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox); ASSERT (Idm_aboutbox < 0xf000); cmenu* Psysmenu = GetSystemMenu (FALSE), if (psysmenu! = NULL) {CString straboutmenu;straboutmenu.loadstring (Ids_ AboutBox), if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);p Sysmenu->appendmenu (MF_STRING, Idm_aboutbox, Straboutmenu);}} Set The icon for this dialog. The framework does this automatically//when the application ' s main window was not a Dialogseticon (M_hicon, TRUE);//Set B IG Iconseticon (M_hicon, false);//Set small icon//todo:add extra initialization Herem_cboplaycount.enablewindow (false) ;//Times not available return TRUE; return TRUE unlessYou set the focus to a control}void Cdlgctrldlg::onsyscommand (UINT nid, LPARAM LPARAM) {if (NID & 0xfff0) = = Idm_about BOX) {CAboutDlg dlgabout;dlgabout.domodal ();} Else{cdialog::onsyscommand (NID, LParam);}} If you add a Minimize button to your dialog, you'll need the code below//to draw the icon. For MFC applications using the Document/view model,//This is automatically do for your by the framework.void CDLGCTRLDL G::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for Paintingsendmessage (Wm_iconerasebkgnd, (WPARAM) dc. GETSAFEHDC (), 0);//Center icon in client rectangleint 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 the ICONDC. DrawIcon (x, y, m_hicon);} Else{cdialog::onpaint ();}} The system calls the obtain the cursor to display while the user drags//the minimized window. Hcursor Cdlgctrldlg::onqueryDragIcon () {return (hcursor) M_hicon;} void Cdlgctrldlg::onopenfile () {//Todo:add your control notification handler code Herechar lpszfilter[]= "Video (*.avi) |*. avi| Video (*.RMVB) |*.rmvb| All Files (*. *) |*.*| | "; CFileDialog Dlg (true,null,null,ofn_hidereadonly| Ofn_overwriteprompt,lpszfilter); if (Idok!=dlg. DoModal ()) return;//update the file path M_strfilepath=dlg in the edit box. GetPathName (); UpdateData (FALSE);//list added filename//m_listfilename.addstring (dlg. GetFileName ()); CString Strname=dlg. GetFileName (); if (lb_err==m_listfilename.findstring ( -1,strname)) {//Add file name int nindex=m_listfilename.addstring ( StrName); CString *ppath=new cstring;*ppath=m_strfilepath;//Save the file path M_listfilename.setitemdata (NIndex, (DWORD) Ppath) with additional data;} Animation controls play animation play (); void Cdlgctrldlg::p lay () {int ncount=1;//saves the number of plays if (M_btnreplay.getcheck ()) {int Nsel=m_cboplaycount.getcursel (); SWI TCH (Nsel) {case 0://repeats ncount=-1;break;case 1://repeats once ncount=2;break;case 2://repeats two times ncount=3;break;} }m_animatevideo.open (M_strfilepath); M_animatevideo.play (0,-1,ncount);} void Cdlgctrldlg::Ondblclklistfilename () {//Gets double-clicked when the current selection int Nsel=m_listfilename.getcursel (); if (Lb_err==nsel) return;// Play the currently selected file CString *ppath= (cstring*) m_listfilename.getitemdata (Nsel); m_strfilepath=*ppath;//play play (); UpdateData (FALSE); AfxMessageBox (*ppath);} void Cdlgctrldlg::onclose () {///string path in append data, to delete for (int i=0;i<m_listfilename.getcount (); i++) {CString *ppath= ( CString *) M_listfilename.getitemdata (i);d elete Ppath;} Cdialog::onclose ();} void Cdlgctrldlg::onreplay () {//Todo:add your control notification handler code HEREif (M_btnreplay.getcheck ()) {m_ Cboplaycount.enablewindow (TRUE);} Else{m_cboplaycount.enablewindow (FALSE);}} void Cdlgctrldlg::onplay () {//Todo:add your control notification handler code Hereplay ();} void Cdlgctrldlg::onstop () {//Todo:add your control notification handler code herem_animatevideo.stop ();}
Example 2:
1. New MFC dialog box Application DLGCTRL2.
2. Visual editing Interface control
3. Ctrl+w the member variable corresponding to the bound control
4. Ctrl+w The Click event of a double-click event and button for a bound list box item
5. The main implementation code in ***DLG.CPP (partially auto-generated)
#include "stdafx.h" #include "DlgCtrl2.h" #include "DlgCtrl2Dlg.h" #ifdef _debug#define new Debug_new#undef This_ Filestatic char this_file[] = __file__; #endif///////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App aboutclass caboutdlg:public Cdialog{public:caboutdlg ();//Dialog Data//{{afx_da TA (CAboutDlg) enum {IDD = Idd_aboutbox};//}}afx_data//ClassWizard generated virtual function overrides//{{afx_virtual ( CABOUTDLG) protected:virtual void DoDataExchange (cdataexchange* pDX); DDX/DDV support//}}afx_virtual//implementationprotected://{{afx_msg (CABOUTDLG)//}}AFX_MSGDECLARE_MESSAGE_MAP ( )}; Caboutdlg::caboutdlg (): CDialog (Caboutdlg::idd) {//{{afx_data_init (CAboutDlg)//}}afx_data_init}void CABOUTDLG::D Odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map (CAboutDlg)//}}afx_data_map} Begin_message_map (CAboutDlg, CDialog)//{{afx_msg_map (CABOUTDLG)/No MESSAGE Handlers//}}afx_msg_mapend_message_ MAP ()//////////Cdlgctrl2dlg Dialogcdlgctrl2dlg:: Cdlgctrl2dlg (cwnd* pparent/*=null*/): CDialog (Cdlgctrl2dlg::idd, pparent) {//{{afx_data_init (CDLGCTRL2DLG) m_edit_a = 0;m_edit_b = 0;m_edit_sum = 0;//}}afx_data_init//Note that LoadIcon does is require a subsequent destroyicon in win32m _hicon = AfxGetApp ()->loadicon (IDR_MAINFRAME);} void Cdlgctrl2dlg::D odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map (Cdlgctrl2dlg ) DDX_Control (PDX, Idc_list_right, m_list_right);DD X_control (PDX, Idc_list_left, M_list_left);DD x_text (PDX, IDC_EDIT _a, M_edit_a);DD x_text (PDX, Idc_edit_b, M_edit_b);DD x_text (PDX, Idc_edit_sum, m_edit_sum);//}}afx_data_map}begin_ Message_map (Cdlgctrl2dlg, CDialog)//{{afx_msg_map (CDLGCTRL2DLG) On_wm_syscommand () On_wm_paint () ON_WM_ Querydragicon () on_bn_clicked (idc_button_equal, onbuttonequal) on_bn_clicked (Idc_button_toleft, OnBUTTONToLeft) ON_ Bn_clicked (Idc_button_toleftall, Onbuttontoleftall) On_bn_cLICKED (Idc_button_toright, Onbuttontoright) on_bn_clicked (Idc_button_torightall, Onbuttontorightall)//}}AFX_MSG_ Mapend_message_map ()/////////////////////////////////////////////////////////////////////////////// Cdlgctrl2dlg message Handlersbool Cdlgctrl2dlg::oninitdialog () {cdialog::oninitdialog ();//Add "About ..." menu item to System menu.//Idm_aboutbox must is in the System command range. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox); ASSERT (Idm_aboutbox < 0xf000); cmenu* Psysmenu = GetSystemMenu (FALSE), if (psysmenu! = NULL) {CString straboutmenu;straboutmenu.loadstring (Ids_ AboutBox), if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);p Sysmenu->appendmenu (MF_STRING, Idm_aboutbox, Straboutmenu);}} Set The icon for this dialog. The framework does this automatically//when the application ' s main window was not a Dialogseticon (M_hicon, TRUE);//Set B IG Iconseticon (M_hicon, FALSE);//Set small icon//todo:add extra initialization here//initialize left list box m_list_left. AddstrING ("Beijing"); M_list_left. AddString ("Shanghai"); M_list_left. AddString ("Guangzhou"); M_list_left. AddString ("Shenzhen"); return TRUE; Return TRUE unless you set the focus to a control}void Cdlgctrl2dlg::onsyscommand (UINT nid, LPARAM LPARAM) {if (NID &A mp 0XFFF0) = = Idm_aboutbox) {CAboutDlg dlgabout;dlgabout.domodal ();} Else{cdialog::onsyscommand (NID, LParam);}} If you add a Minimize button to your dialog, you'll need the code below//to draw the icon. For MFC applications using the Document/view model,//This is automatically do for your by the framework.void cdlgctrl2d Lg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for Paintingsendmessage (Wm_iconerasebkgnd, (WPARAM) dc. GETSAFEHDC (), 0);//Center icon in client rectangleint 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 the ICONDC. DrawIcon (x, y, m_hicon);} Else{cdialog::onpaint ();}} The system calls the obtain the cursor to display while the user drags//the minimized window. Hcursor Cdlgctrl2dlg::onquerydragicon () {return (hcursor) M_hicon;} void Cdlgctrl2dlg::onbuttonequal () {//Todo:add your control notification handler code hereupdatedata (TRUE); m_edit_sum= M_edit_a+m_edit_b; UpdateData (FALSE);} void Cdlgctrl2dlg::onbuttontoleft () {//Todo:add your control notification handler code Hereint sel=m_list_right. GetCurSel (); if (Sel==lb_err) return; CString Str;m_list_right. GetText (SEL,STR);//Get Right value m_list_left. AddString (str);//Add to left m_list_right. DeleteString (SEL);//Delete right}void Cdlgctrl2dlg::onbuttontoleftall () {//Todo:add your control notification handler code Herecstring Str;while (m_list_right. GetCount () >0) {m_list_right. GetText (0,STR); M_list_left. AddString (str); m_list_right. DeleteString (0);}} void Cdlgctrl2dlg::onbuttontoright () {//Todo:add your control notification handler code here//get left list selection int sel=m_list_ Left. GetCurSel (); if (Sel==lb_err) return; CString Str;m_liSt_left. GetText (SEL,STR);//Gets the left value m_list_right. AddString (str);//Add to right m_list_left. DeleteString (SEL);//delete left}void Cdlgctrl2dlg::onbuttontorightall () {//Todo:add your control notification handler code Herecstring Str;while (m_list_left. GetCount () >0) {M_list_left. GetText (0,STR); m_list_right. AddString (str); M_list_left. DeleteString (0);}}
"The sample code is added later"