Course 3 [Use of the MFC Dialog Box]

Source: Internet
Author: User

Course 1 (Part 1)

Course 1 (Part 1) "Let's play C ++ together)

Course 2 C ++ for everyone

Courseware. These include ppt and MFC learning materials (summarized by most individuals), screen recording experts, simple MFC dialog box examples, and course avi videos.

Follow ilovecode.cn or this blog to learn c ++ ~

// MFCDialogDlg. cpp: implementation file // MFC Application Design (version 2) learning notes http://blog.csdn.net/leiming32/article/details/8310010#include "stdafx. h "# include" MFCDialog. h "# include" MFCDialogDlg. h "# include" afxdialogex. h "# include" Login. h "# ifdef _ DEBUG # define new DEBUG_NEW # endif // class CAboutDlg: public CDialogEx {public: CAboutDlg () in the CAboutDlg dialog box for the" about "menu item of the application (); // dialog box data: enum {IDD = IDD_ABOUTBOX}; protected: virtual void DoData Exchange (CDataExchange * pDX); // DDX/DDV support // protected: DECLARE_MESSAGE_MAP ()}; CAboutDlg: CAboutDlg (): CDialogEx (CAboutDlg: IDD) {} void CAboutDlg: DoDataExchange (CDataExchange * pDX) {CDialogEx: DoDataExchange (pDX);} Export (CAboutDlg, CDialogEx) END_MESSAGE_MAP () // dialog box comment :: CMFCDialogDlg (CWnd * pParent/* = NULL */): CDialogEx (CMFCDialogDlg: IDD, pParent), m_str_static (_ T ("") {m_hIcon = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME);} void CMFCDialogDlg: DoDataExchange (CDataExchange * pDX) {CDialogEx: DoDataExchange (pDX ); DDX_Text (pDX, latency, m_str_static); DDX_Control (pDX, latency, m_comboBox); DDX_Control (pDX, latency, latency); DDX_Control (pDX, IDC_LIST_BOX, m_listbox); DDX_Control (pDX, IDC_LIST_DIARY, m_listControl);} BEGIN_MESSAGE_MAP (CMFCDialogDlg, C DialogEx) ON_WM_SYSCOMMAND () ON_WM_PAINT () lower (lower, & Lower: lower) ON_CBN_SELCHANGE (IDC_COMBO2, & Lower: lower) lower () ON_WM_MOUSEMOVE () lower () // CMFCDialogDlg message processing program BOOL CMFCDialogDlg: OnInitDialog () {CDialogEx: OnInitDialog (); // set "about... "menu items are added to the system menu. // IDM_ABOUTBOX must be in the range of system commands. 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); pSysMenu-> AppendMenu (MF_STRING, IDM_ABOUTBOX, strAboutMenu); }}// set the icon of this dialog box. When the main window of the application is not a dialog box, the framework will automatically // execute this operation SetIcon (m_hIcon, TRUE); // set the big icon SetIcon (m_hIcon, FALSE ); // set the small icon // TODO: add the additional initialization code m_combox.addstring (L "command 1"); m_combox.addstring (L "today's weather is good! "); M_combox.addstring (L" what do you want to eat at night? "); M_combox.addstring (L" Start timer 100 "); m_combox.addstring (L" Stop timer 100 "); m_comboBox.AddString (L" Beauty photo "); m_comboBox.SetCurSel (1); CString str; str. format (L "ComboBox has % d commands", m_combox.getcount (); m_str_static = str; UpdateData (FALSE); m_listbox.GetCount () m_listbox.AddString (_ T ("Monday ")); m_listbox.AddString (_ T ("Tuesday"); m_listbox.AddString (_ T ("Wednesday"); m_listbox.AddString (_ T ("Thursday ")); m_listbox.AddString (_ T ("Friday"); m _ Listbox. addString (_ T ("Saturday"); m_listbox.AddString (_ T ("Sunday ")); //////////////////////////////////////// /// // CString Field [4] = {L "name ", L "gender", L "Grade", L "Age"}; // table header for (int j = 0; j <4; j ++) {m_listControl.InsertColumn (j, Field [j], LVCFMT_LEFT, 73); // insert header title} int I; for (I = 0; I <4; I ++) {m_listControl.InsertItem (I, _ T ("Guo Ming"); // Add the new line m_listControl.SetItemText (I, 1, _ T ("man"); m_listControl.SetItemTe Xt (I, 2, _ T (""); m_listControl.SetItemText (I, 3, _ T ("25");} TCHAR via [20]; swprintf_s (via, 20, _ T ("% d"), I); m_listControl.InsertItem (I, ); // Add a new row //////////////////////////////////// //////////////////////////////////////// /* Login dlg; // Login Dialog Box if (dlg. doModal () {if (dlg. m_edit_name.Compare (_ T ("guoming") = 0) {if (dlg. m_edit_password.Compare (_ T ("123") = 0) {AfxMessageBox (_ T ("Login successful! ");} Else {AfxMessageBox (_ T (" Login Failed! "); // DestroyWindow (); EndDialog (0); PostMessage (WM_CLOSE) ;}} elsereturn AfxMessageBox (_ T (" Login Failed! ");} */Return TRUE; // return TRUE unless the focus is set to the control.} void CMFCDialogDlg: OnSysCommand (UINT nID, LPARAM lParam) {if (nID & 0xFFF0) = IDM_ABOUTBOX) {CAboutDlg dlgAbout; dlgAbout. doModal () ;}else {CDialogEx: OnSysCommand (nID, lParam) ;}// if you want to add the minimization button to the dialog box, you need the following code // to draw the icon. For MFC applications that use document/view models, // This will be automatically completed by the framework. Void CMFCDialogDlg: OnPaint () {if (IsIconic () {CPaintDC dc (this); // The device context SendMessage (WM_ICONERASEBKGND, reinterpret_cast <WPARAM> (dc. getSafeHdc (), 0); // center the icon in the rectangle of the workspace 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 the icon dc. drawIcon (x, y, m_hIcon);} e Lse {CDialogEx: OnPaint () ;}// the system calls this function to obtain the cursor when the user drags the minimized window. // display the cursor. HCURSOR CMFCDialogDlg: OnQueryDragIcon () {return static_cast <HCURSOR> (m_hIcon);} void CMFCDialogDlg: OnBnClickedButtonExample () {// TODO: add the control notification handler code m_str_static = L "to display this line in the static text box"; // _ T ("") is a macro, its function is to enable your program to support Unicode encoding // no matter how you compile it, UpdateData (FALSE) will be saved in UNICODE mode ); // The variable is displayed on the control} // ComboBox in MFC uses the http://blog.csdn.net/yunqi415/article/details/6963720void CMFCDialogDlg: OnCbnSelchangeCombo2 () {// TODO: add controls here Zhizhi processing program code int sel = m_combox.getcursel (); CString str; m_combox.getlbtext (sel, str); if (str. compare (_ T ("START timer 100") = 0) SetTimer (, NULL); if (str. compare (_ T ("Stop timer 100") = 0) KillTimer (100); if (str. compare (_ T ("") = 0) {// HBITMAP bmp = (HBITMAP) LoadImage (NULL, L "Pic.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); // m_static_pic.SetBitmap (bmp); HBITMAP bmp = (HBITMAP) LoadImage (NULL, L "meinv.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); loads (bmp) ;}} void CMFCDialogDlg: OnTimer (UINT_PTR nIDEvent) {// TODO: add the message processing program code and/or call the default value static int timeCount = 0; CString str; switch (nIDEvent) {case 100: str. format (_ T ("Count: % d"), timeCount ++); m_str_static = str; UpdateData (FALSE); break;} CDialogEx: OnTimer (nIDEvent );} void CMFCDialogDlg: OnMouseMove (UINT nFlags, CP Oint point) {// TODO: add the message processing program code and/or call the default CString str1, str2; str1.Format (_ T ("x: % d"), point. x); str2.Format (_ T ("y: % d"), point. y); GetDlgItem (optional)-> SetWindowText (str1); GetDlgItem (optional)-> SetWindowText (str2); CDialogEx: OnMouseMove (nFlags, point);} BOOL CMFCDialogDlg :: preTranslateMessage (MSG * pMsg) {if (pMsg-> message = WM_KEYDOWN) {switch (pMsg-> wParam) {case VK_LEFT: MessageBox (_ T ("to left! "); Break; case VK_RIGHT: MessageBox (_ T (" to the right! "); Break; case VK_UP: MessageBox (_ T (" Up! "); Break; case VK_DOWN: MessageBox (_ T (" down! "); Break ;}} return CDialog: PreTranslateMessage (pMsg); // do not write an error, or the program will die}

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.