当前位置: 代码迷 >> 综合 >> 孙鑫VC++深入详解:Lesson7 Part1---模态对话框,非模态对话框,静态文本控件的访问
  详细解决方案

孙鑫VC++深入详解:Lesson7 Part1---模态对话框,非模态对话框,静态文本控件的访问

热度:20   发布时间:2024-01-19 15:17:59.0

知识点:  在MFC中要对一个资源,就应该先为该资源创建一个关联的类,通过类的对象来操作该资源


//----创建模态对话框

CTestDlg dlg;
        dlg.DoModal();

//----创建非模态对话框
        CTestDlg *pDlg=new CTestDlg();
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW);

//------静态文本控件的访问
(1)改变控件的ID
(2)Style中勾选Notify
(3)用Cwnd* GetDlgItem(int nID) const 函数获得指向控件的指针

(4)用GetWindowText(str),SetWindowText(str)函数.


//---

// TestDlg.cpp : implementation file //#include "stdafx.h" #include "Mybole.h" #include "TestDlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CTestDlg dialog//构造函数,用基类CDialog构造函数传入IDD,这个IDD就是IDD_DIALOG1,IDD在TestDlg.h中定义的. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/): CDialog(CTestDlg::IDD, pParent) {//{
    {AFX_DATA_INIT(CTestDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INITm_bIsCreate = false; }void CTestDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{
    {AFX_DATA_MAP(CTestDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CTestDlg, CDialog)//{
    {AFX_MSG_MAP(CTestDlg)ON_BN_CLICKED(ID_BTN_ADD, OnBtnAdd)ON_BN_CLICKED(IDC_NUMBER1, OnNumber1)//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CTestDlg message handlersvoid CTestDlg::OnBtnAdd() {// TODO: Add your control notification handler code here//--- 创建按钮 /*static bool m_bIsCreate = false;if(m_bIsCreate==false){m_btn.Create("微信",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,100),this,123);m_bIsCreate = true;}else{m_btn.DestroyWindow();m_bIsCreate = false;} *///---利用m_btn对象中的成员m_hWnd来判断按钮是否创建./*if(m_btn.m_hWnd==NULL)m_btn.Create("微信",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,100),this,123); else m_btn.DestroyWindow();*///------ 加法int num1,num2,num3;char ch1[10],ch2[10],ch3[10];GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);num1 = atoi(ch1);num2 =atoi(ch2);num3 = num1+num2;itoa(num3,ch3,10);GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);}// -----静态文本的访问 void CTestDlg::OnNumber1() {// TODO: Add your control notification handler code hereCString str;if(GetDlgItem(IDC_NUMBER1)->GetWindowText(str),str="Number1:"){GetDlgItem(IDC_NUMBER1)->SetWindowText("数值1:");}else{GetDlgItem(IDC_NUMBER1)->SetWindowText("Number1:");}} 


//---

//---

  相关解决方案