VC对话框控件基本操作2
2015-04-18 15:59
211 查看
本文以一个简单的例子实现对话框控件的基本操作。
新建对话框应用程序,然后添加相关变量
首先我们来看看效果,然后再看看如何实现的
处理后
实现如上功能的源码
头文件
源文件
源码下载
新建对话框应用程序,然后添加相关变量
首先我们来看看效果,然后再看看如何实现的
处理后
实现如上功能的源码
头文件
// SELECTBOXDlg.h : header file // #if !defined(AFX_SELECTBOXDLG_H__CE5E097E_272B_4033_8CF8_C3037607F43C__INCLUDED_) #define AFX_SELECTBOXDLG_H__CE5E097E_272B_4033_8CF8_C3037607F43C__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CSELECTBOXDlg dialog class CSELECTBOXDlg : public CDialog { // Construction public: CSELECTBOXDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CSELECTBOXDlg) enum { IDD = IDD_SELECTBOX_DIALOG }; CComboBox m_Record; CEdit m_ResultEdit; CButton m_AgeRadio; CButton m_SexRadio; CEdit m_TimeEdit; CEdit m_DateEdit; BOOL m_DateCheck; BOOL m_TimeCheck; int m_English; CString m_ComboEdit; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSELECTBOXDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CSELECTBOXDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnDateCheck(); afx_msg void OnTimeCheck(); afx_msg void OnEnableButton(); afx_msg void OnDisbaleButton(); afx_msg void OnShowButton(); afx_msg void OnHideButton(); afx_msg void OnShowSexAgeBUTTON(); afx_msg void OnShowComboButton(); afx_msg void OnButton(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_SELECTBOXDLG_H__CE5E097E_272B_4033_8CF8_C3037607F43C__INCLUDED_)
源文件
// SELECTBOXDlg.cpp : implementation file // #include "stdafx.h" #include "SELECTBOX.h" #include "SELECTBOXDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(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 // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSELECTBOXDlg dialog CSELECTBOXDlg::CSELECTBOXDlg(CWnd* pParent /*=NULL*/) : CDialog(CSELECTBOXDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSELECTBOXDlg) m_DateCheck = FALSE; m_TimeCheck = FALSE; m_English = -1; m_ComboEdit = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSELECTBOXDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSELECTBOXDlg) DDX_Control(pDX, IDC_RECORD_COMBO, m_Record); DDX_Control(pDX, IDC_Result_EDIT, m_ResultEdit); DDX_Control(pDX, IDC_Age1_RADIO, m_AgeRadio); DDX_Control(pDX, IDC_Boy_RADIO, m_SexRadio); DDX_Control(pDX, IDC_TIME_EDIT, m_TimeEdit); DDX_Control(pDX, IDC_DATE_EDIT, m_DateEdit); DDX_Check(pDX, IDC_DATE_CHECK, m_DateCheck); DDX_Check(pDX, IDC_TIME_CHECK, m_TimeCheck); DDX_Radio(pDX, IDC_ENGLISH_RADIO, m_English); DDX_Text(pDX, IDC_SHOW_COMBO_EDIT, m_ComboEdit); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSELECTBOXDlg, CDialog) //{{AFX_MSG_MAP(CSELECTBOXDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_DATE_CHECK, OnDateCheck) ON_BN_CLICKED(IDC_TIME_CHECK, OnTimeCheck) ON_BN_CLICKED(IDC_ENABLE_BUTTON, OnEnableButton) ON_BN_CLICKED(IDC_DISBALE_BUTTON, OnDisbaleButton) ON_BN_CLICKED(IDC_SHOW_BUTTON, OnShowButton) ON_BN_CLICKED(IDC_HIDE_BUTTON, OnHideButton) ON_BN_CLICKED(IDC_Show_Sex_Age_BUTTON, OnShowSexAgeBUTTON) ON_BN_CLICKED(IDC_SHOW_COMBO_BUTTON, OnShowComboButton) ON_BN_CLICKED(IDC_BUTTON, OnButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSELECTBOXDlg message handlers BOOL CSELECTBOXDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be 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); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //初始化---------------------------------添加部分------------------------------------------------------- CheckRadioButton(IDC_Boy_RADIO,IDC_Girl_RADIO,IDC_Boy_RADIO); CheckRadioButton(IDC_Age1_RADIO,IDC_Age3_RADIO,IDC_Age2_RADIO); UpdateData(TRUE); m_English = 0; m_Record.AddString("85"); m_Record.AddString("90"); m_Record.AddString("95"); m_Record.SelectString(-1,"95"); UpdateData(FALSE); //----------------------------------------添加部分------------------------------------------------------- return TRUE; // return TRUE unless you set the focus to a control } void CSELECTBOXDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSELECTBOXDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client 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 the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSELECTBOXDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //----------------------------------------------------------------------------------------------------------------------------- void CSELECTBOXDlg::OnDateCheck() //----------------日期---------------------- { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_DateCheck == TRUE) { CTime tNow; tNow=CTime::GetCurrentTime(); CString sNow=tNow.Format("%y.%m.%d"); m_DateEdit.SetSel(0,-1); m_DateEdit.ReplaceSel(sNow); } else { m_DateEdit.SetSel(0,-1); m_DateEdit.ReplaceSel(""); } UpdateData(FALSE); } void CSELECTBOXDlg::OnTimeCheck() //----------------时间------------------------ { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_TimeCheck == TRUE) { CTime tNow; tNow=CTime::GetCurrentTime(); CString sNow=tNow.Format("%I.%M.%S"); m_TimeEdit.SetSel(0,-1); m_TimeEdit.ReplaceSel(sNow); } else { m_TimeEdit.SetSel(0,-1); m_TimeEdit.ReplaceSel(""); } UpdateData(FALSE); } void CSELECTBOXDlg::OnEnableButton() //-------------复选框有效------------------- { // TODO: Add your control notification handler code here GetDlgItem(IDC_DATE_CHECK)->EnableWindow(TRUE); GetDlgItem(IDC_TIME_CHECK)->EnableWindow(TRUE); m_DateEdit.EnableWindow(TRUE); m_TimeEdit.EnableWindow(TRUE); } void CSELECTBOXDlg::OnDisbaleButton() //-------------复选框无效------------------- { // TODO: Add your control notification handler code here GetDlgItem(IDC_DATE_CHECK)->EnableWindow(FALSE); GetDlgItem(IDC_TIME_CHECK)->EnableWindow(FALSE); m_DateEdit.EnableWindow(FALSE); m_TimeEdit.EnableWindow(FALSE); } void CSELECTBOXDlg::OnShowButton() //--------------重新显示被隐藏的复选框和编辑框------------------------ { // TODO: Add your control notification handler code here GetDlgItem(IDC_DATE_CHECK)->EnableWindow(SW_SHOW); GetDlgItem(IDC_TIME_CHECK)->EnableWindow(SW_SHOW); GetDlgItem(IDC_DATE_EDIT)->EnableWindow(SW_SHOW); m_DateEdit.ShowWindow(SW_SHOW); GetDlgItem(IDC_TIME_EDIT)->EnableWindow(SW_SHOW); m_TimeEdit.ShowWindow(SW_SHOW); } void CSELECTBOXDlg::OnHideButton() //--------------隐藏的复选框和编辑框使不可见------------------------ { // TODO: Add your control notification handler code here GetDlgItem(IDC_DATE_CHECK)->EnableWindow(SW_HIDE); GetDlgItem(IDC_TIME_CHECK)->EnableWindow(SW_HIDE); m_DateEdit.ShowWindow(SW_HIDE); m_TimeEdit.ShowWindow(SW_HIDE); } void CSELECTBOXDlg::OnShowSexAgeBUTTON() //-------------显示性别和年龄----------------------- { // TODO: Add your control notification handler code here char sEdit[50]; int iSexRADIO; int iAgeRADIO; iSexRADIO = GetCheckedRadioButton(IDC_Boy_RADIO, IDC_Girl_RADIO); if(iSexRADIO == IDC_Boy_RADIO) strcpy(sEdit,"The boy's age is"); if(iSexRADIO == IDC_Girl_RADIO) strcpy(sEdit,"The girl's age is"); iAgeRADIO = GetCheckedRadioButton(IDC_Age1_RADIO, IDC_Age3_RADIO); if(iAgeRADIO == IDC_Age1_RADIO) strcat(sEdit," great than 20"); if(iAgeRADIO == IDC_Age2_RADIO) strcat(sEdit," between 15 and 20"); if(iAgeRADIO == IDC_Age3_RADIO) strcat(sEdit," less than 15"); m_ResultEdit.SetSel(0,-1); m_ResultEdit.ReplaceSel(sEdit); } void CSELECTBOXDlg::OnShowComboButton() //---------------------报告状态---------------------- { // TODO: Add your control notification handler code here UpdateData(TRUE); char sCourseEdit[30]; char sRecordEdit[15]; int iCourseRadio; iCourseRadio = GetCheckedRadioButton(IDC_ENGLISH_RADIO, IDC_NATURE_RADIO); if(iCourseRadio == IDC_ENGLISH_RADIO) strcpy(sCourseEdit,"English record is "); if(iCourseRadio == IDC_COMPUTER_RADIO) strcpy(sCourseEdit,"Computer record is "); if(iCourseRadio == IDC_NATURE_RADIO) strcpy(sCourseEdit,"Nature record is "); m_Record.GetWindowText(sRecordEdit,15); strcat(sCourseEdit,""); strcat(sCourseEdit,sRecordEdit); m_ComboEdit = _T(sCourseEdit); UpdateData(FALSE); } void CSELECTBOXDlg::OnButton() //---------------------退出-------------------------- { // TODO: Add your control notification handler code here OnOK(); }
源码下载
相关文章推荐
- 一.【Silverlight】Ldh.Maps控件地图基本操作示例
- ListView 控件写的基本操作
- jQuery操作基本控件方法实例分析
- 窗体中控件的基本操作
- 树形控件的基本操作(一)
- 控件:PopupWindow --- 弹出窗口(基本操作)
- VC控件的基本操作
- 树形控件的基本操作(二)
- MFC一些基本控件操作的总结
- 基本控件操作
- 控件基本操作与基本事件响应
- JQuery对基本Html控件的操作TextBox、Select、Radio、CheckBox .
- VC编辑控件Edit基本操作
- VC编辑控件Edit基本操作
- C#线程相关问题总结:基本操作及UI控件交互
- CTreeList 控件基本操作,响应点击某一行
- C#线程相关问题总结:基本操作及UI控件交互
- CListCtrl控件(1)基本操作
- jQuery之基本Html控件操作
- CListCtrl控件(1)基本操作