MFC简易对话框编程(一)

// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.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()

/
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_num1 = 0;
	m_num2 = 0;
	m_num3 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Control(pDX, IDC_EDIT3, m_edit3);
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
	DDX_Control(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT1, m_num1);
	DDX_Text(pDX, IDC_EDIT2, m_num2);
	DDX_Text(pDX, IDC_EDIT3, m_num3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTestDlg message handlers

BOOL CTestDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestDlg::OnAdd() 
{
/*	if(!m_bIsCreated)
	{
		m_btn.Create("NEW",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,CRect(0,0,100,100),this,123);
		m_bIsCreated=TRUE;
	}
	else
	{
		m_btn.DestroyWindow();
		m_bIsCreated=FALSE;
	}*/

/*	if(!m_btn.m_hWnd)
	{
		m_btn.Create("NEW",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,CRect(0,0,100,100),this,123);		
	}
	else
		m_btn.DestroyWindow();*/

/*	int num1,num2,num3;
	char ch1[11],ch2[11],ch3[11];
	GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,11);
	GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,11);

	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	
	itoa(num3,ch3,10);
	GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);*/

/*	int num1,num2,num3;
	char ch1[11],ch2[11],ch3[11];

	GetDlgItemText(IDC_EDIT1,ch1,11);
	GetDlgItemText(IDC_EDIT2,ch2,11);

	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	
	itoa(num3,ch3,10);//最后的数字是进制
	SetDlgItemText(IDC_EDIT3,ch3);*/

/*	int num1,num2,num3;
	num1=GetDlgItemInt(IDC_EDIT1);
	num2=GetDlgItemInt(IDC_EDIT2);
	
	num3=num1+num2;

	SetDlgItemInt(IDC_EDIT3,num3);*/
	/*
	BOOL UpdateData( BOOL bSaveAndValidate = TRUE );
	bSaveAndValidate
		Flag that indicates whether dialog box is being initialized (FALSE) or data is being retrieved (TRUE).*/
		
/*	UpdateData();
	m_num3=m_num1+m_num2;
	UpdateData(FALSE);*/

/*	int num1,num2,num3;
	char ch1[11],ch2[11],ch3[11];
	
	m_edit1.GetWindowText(ch1,11);
	m_edit2.GetWindowText(ch2,11);

	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;

	itoa(num3,ch3,10);
	m_edit3.SetWindowText(ch3);*/

/*	int num1,num2,num3;
	char ch1[11],ch2[11],ch3[11];

	m_edit1.SendMessage(WM_GETTEXT,11,(LPARAM)ch1);
	m_edit2.SendMessage(WM_GETTEXT,11,(LPARAM)ch2);

	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	
	itoa(num3,ch3,10);
	::SendMessage(m_edit3.m_hWnd,WM_SETTEXT,0,(LPARAM)ch3);*/
	
	int num1,num2,num3;
	char ch1[11],ch2[11],ch3[11];
	
	SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,11,(LPARAM)ch1);
	SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,11,(LPARAM)ch2);
	
	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	
	itoa(num3,ch3,10);
	SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);

	//复选内容
	SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,1,3);
	//设置焦点
	m_edit3.SetFocus();
}

void CTestDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString str;
	if(GetDlgItemText(IDC_BUTTON1,str),str=="收缩<<")
	{
		SetDlgItemText(IDC_BUTTON1,"扩展>>");
	}
	else
	{
		SetDlgItemText(IDC_BUTTON1,"收缩<<");
	}
	
	static CRect rectLarge;
	static CRect rectSmall;
	
	if(rectLarge.IsRectEmpty())
	{
		CRect rectSeparator;
		GetWindowRect(&rectLarge);
		GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
		
		rectSmall.left=rectLarge.left;
		rectSmall.top=rectLarge.top;
		rectSmall.right=rectSeparator.right;
		rectSmall.bottom=rectLarge.bottom;
	}
	if(str=="收缩<<")
	{
		SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
	}
	else
	{
		SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE | SWP_NOZORDER);
	}
}


效果

 

重要掌握的是访问编辑框控件的几种方式和伸缩变换对话框的实现

访问编辑框控件方面代码中用了七种方式,主要如下:

1、GetDlgItem()->Get(Set)WindowText();

2、GetDlgItemText()/SetDlgItemText();

3、GetDlgItemInt()/SetDlgItemInt();

4、将控件与整形变量相关联

5、将控件和控件变量相关联

6、SendMessage();

7、SendDlgItemMessage();

第1、4、5是比较常用的

几个函数的原型和用法都可以在MSDN中查到,这里就不再贴出了,简要说下用法

CWnd* GetDlgItem( int nID ) const; //返回指定ID号的窗口的指针

int GetWindowText( LPTSTR lpszStringBuf, int nMaxCount ) const; //将指定控件的文本信息存放在用户提供的一个Buffer中

void SetDlgItemText( int nID, LPCTSTR lpszString ); //第一种方法的两个函数的结合

UINT GetDlgItemInt( int nID, BOOL* lpTrans = NULL, BOOL bSigned = TRUE ) const; //保存控件信息的整形信息

在用第四种方法时,用到了Updatedata()这个函数

BOOL UpdateData( BOOL bSaveAndValidate = TRUE );

bSaveAndValidate

             Flag that indicates whether dialog box is being initialized (FALSE) or data is being retrieved (TRUE).

看得出当参数为FALSE时候就是初始化对话框控件,TRUE时就是接收数据初始化控件中的值.

 

Windows程序是基于消息的,因此可以用获取或设置窗口文本的消息,通过SendMessage()来发送,得到该信息后,系统就会将指定窗口中的文本复制到调用者提供的一个缓存中,获取文本的消息是WM_GETTEXT。在这个消息的另外两个附加参数中,wParam指定将复制的字符数,lParam就是那个缓存地址,记得要强制转换。

设置文本的消息是WM_SETTEXT,这个消息的wParam参数没有被调用,值为0,lParam参数指定了用来设置文本的字符串的地址。

最后一种方式是通过SendDlgItemMessage()函数直接给控件发送信息。

至于对话框的伸缩变换的功能的实现是通过SetWindowPos()函数来实现的,关于这个函数的介绍看上一篇日记。保存缩放前的对话框的大小和通过获取分割线(其实就是一张图,不过是拉成一条线)的大小 来实现的GetWindowRect()这个函数可以返回调用它的窗口的大小的一个矩形坐标

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值