您的位置:首页 > 编程语言 > C语言/C++

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

2013-07-06 19:28 399 查看
知识点: 在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:");
}

}


//---

//---
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐