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

知识点:  在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_INIT

	m_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 handlers

void 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 here
   CString str;
   if(GetDlgItem(IDC_NUMBER1)->GetWindowText(str),str="Number1:")
   {
	   GetDlgItem(IDC_NUMBER1)->SetWindowText("数值1:");
   }
   else
   {
		GetDlgItem(IDC_NUMBER1)->SetWindowText("Number1:");
   }
	
}


//---

//---

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值