For a long time without contact with the knowledge of MFC, I was probably in the sophomore study of MFC-related knowledge and image processing, now because to help a friend to complete a C + + program, so reviewed the relevant knowledge. It is true that any knowledge after a period of time is easier to forget, but the review is still very impressive.
This article is mainly to review the previous MFC basics, add background images to the dialog box and add background images to button buttons, I hope this basic article will help you! At the same time for the next time to do MFC related knowledge to provide points at this time feel it. The content is relatively simple, the master drifting over ~
I. Dialog box background add a picture
You first create an MFC dialog application through VS2012, the project name is Recoverymovie, and then you open DIALOG in the resource View, Idd_recoverymovie_dialog, and then edit the dialog box to delete the default content.
Second step, right-click the project name in Resource view to add the resource.
The third step is to add the selection bitmap import picture, the picture file preferably in the project Res directory, but also the BMP format picture. After uploading the picture as shown in Idb_bitmap1.
The fourth step opens the XXXDlg.cpp file in the solution (the corresponding source file for the dialog box), finds the OnPaint () function, modifies the else part of the code, as follows:
else{//comment The statement: Prevent repeated calls to redraw function//cdialogex::onpaint ();//Add Code dialog box background map CPAINTDC DC (this); CRect rect; GetClientRect (&rect); Gets the dialog box length-width CDC dcbmp; Define and create a memory device environment DCBMP.CREATECOMPATIBLEDC (&DC); Create compatibility Dccbitmap bmpbackground; Bmpbackground.loadbitmap (IDB_BITMAP1); Idb_bitmap1 picture bitmap M_bitmap loaded into the resource; Picture variable bmpbackground.getbitmap (&M_BITMAP); Load the picture into the bitmap CBitmap *pbmpold=dcbmp.selectobject (&bmpbackground); Select bitmap into temporary memory device environment//Call function Display picture StretchBlt display shape variable dc.stretchblt (0,0,rect. Width (), Rect. Height (), &dcbmp,0,0,m_bitmap.bmwidth,m_bitmap.bmheight,srccopy); /******************************************************//** StretchBlt () **//** parameter x, y bit The coordinate values of the upper-left corner x and y of the graph target rectangle are centered **//** nwidth, nheigth the logical width and height of the bitmap target rectangle **//** PSRCDC represents the source device CDC pointer **//** Xsrc, YSRC represents the x, y logical coordinate value of the upper-left corner of the bitmap source rectangle **//** Dwrop Represents the raster operation that shows the bitmap **//** srccopy for copying bitmaps directly into the target environment **//******************************************************/}
running the program now displays the background picture, and you may see a flashing bug when you stretch the dialog box.
So you can set the properties of the dialog border to dialog frame (the dialog box), and the default resizing is the size you can regulate.
two. Buttons button to add a background picture First add 3 buttons to dialog in the resource View, Idc_button1, Idc_button2, and Idc_button3, as shown in:
The second step is to add the resources in the previous way: the button background picture, which is the "play video" picture. Once added, select the picture resource in the Resource View and click Properties to modify the ID value. As shown, change the IDB_BITMAP2 to Idb_startmovie1.
The third step is to open the Class Wizard with the shortcut key (ctrl+shift+x or View-Class wizard), select the class name Crecovermoviedlg, and click Add variable in the member variable, such as adding a member variable of 3 buttons.
At this point you will find that the following DDX_Control () code is automatically added to the function DoDataExchange () in XXXDlg.cpp:
void Crecoverymoviedlg::D odataexchange (cdataexchange* pdx) {cdialogex::D odataexchange (PDX);DD X_control (PDX, IDC_ BUTTON1, M_buttonstart);DD X_control (PDX, Idc_button2, M_buttonfix);DD X_control (PDX, Idc_button3, m_buttonexit);}
The fourth step is to find the initialization function OnInitDialog () in XXXDlg.cpp, and add the following code in the "//TODO: Add additional initialization code here":
TODO: Add extra initialization code//initialization window to 500*500 size MoveWindow (0,0,500,500) here; /*******************************************************************************//* (Key Knowledge: Implement button position setting) *//* 1. Use Ctrl+shift+x to turn on class resource orientation add the Start \ End button member variable button type *//* 2. Use function SetWindowPos () to set the position of two buttons *//*******************************************************************************//******** * SetWindowPos () function *//* parameter: const cwnd* pwndinsertafter,int x,int y,int cx,int cy,uint nflags *//* hwnd is the handle of the window, X , Y, CX, Cy are the x and Y coordinates of the window, and the width and height *//* hwndinsertafter are used to specify the z position of the window, which represents the depth. This parameter accepts 5 values: *//* Hwnd_bottom, Hwnd_not The handle to the Opmost, Hwnd_top, hwnd_topmost, or another window *//* wflags is used to specify additional options *//****** WindowThe port handle is set to null Swp_showwindow the display window Swp_nosize keep the current size ignored cx\xy so set to 0m_buttonstart. SetWindowPos (null,20,280,0,0,swp_showwindow| Swp_nosize); M_buttonfix. SetWindowPos (null,20,325,0,0,swp_showwindow| Swp_nosize); M_buttonexit. SetWindowPos (null,20,370,0,0,swp_showwindow| Swp_nosize); Set Button Properties Click the button background image transform M_buttonstart. Loadbitmaps (idb_startmovie1,idb_startmovie2); M_buttonfix. Loadbitmaps (idb_startmovie1,idb_startmovie2); M_buttonexit. Loadbitmaps (idb_startmovie1,idb_startmovie2); return TRUE; Returns TRUE unless focus is set to the control
at this point you run the program with the following error: the error class "CButton" has no member "Loadbitmaps". function Loadbitmaps () is the function of clicking the button to achieve the replacement of two background images.
The fifth step is to modify the public member variable CButton all to CBitmapButton in the XXXDlg.h header file, as shown in modifying the first variable:
//CRECOVERYMOVIEDLG Dialog class Crecoverymoviedlg:public cdialogex{//construct Public:crecoverymoviedlg (cwnd* pparent = NULL);//Standard constructor//dialog data enum { IDD = idd_recoverymovie_dialog};p rotected:virtual void DoDataExchange (cdataexchange* pDX);//DDX/DDV support//Implement protected: Hicon m_hicon;//generated message map function virtual BOOL OnInitDialog () afx_msg void OnSysCommand (UINT NID, LPARAM LPARAM); afx_msg void OnP Aint (); afx_msg hcursor Onquerydragicon ();D eclare_message_map () Public:cbitmapbutton M_buttonstart; CBitmapButton M_buttonfix; CBitmapButton m_buttonexit;};
Sixth step: Although you can run the program at this time, but the button background is still not, because you also need to set the properties of three buttons, "owner Draw" is true, it specifies the button for the owner-drawn button.
The results of the operation are as follows:
Finally hope that the article is helpful to everyone, if the article has shortcomings or the wrong place, please Haihan! Personal recommendations for beginners, especially many teachers to decorate their homework to be done with MFC can learn, no language popular, they are connected, all need to step by step to learn.
(By:eastmount late 2015-4-27 3 o'clock http://blog.csdn.net/eastmount/)
MFC Basics: Dialog box background add pictures and buttons button to add pictures