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

孙鑫VC++深入详解:Lesson7 Part2---访问静态文本控件的七种方法

2013-07-07 16:23 375 查看
静态文本控件要能响应鼠标点击消息,两个特殊的步骤:

第一步:改变它的ID

第二部:勾选Notify

方式一 GetDlgItem()->GetWindowText();

方式二 GetDlgItemText();SetDlgItemText();

方式三 GetDlgItemInt(); SetDlgItemInt();

方式四 将三个EDIT编辑框关联到当前CTestDlg类的3个成员变量,是普通的内置数据类型如int的成员变量

方式五 将三个EDIT编辑框关联到当前CTestDlg类的控件类型的成员变量,如CEdit类型的控件

方式六 向控件发送SendMessage()的方式来获取或者设置控件窗口的文本

方式七 SendDlgItemMessage()

//--

方式一 GetDlgItem()->GetWindowText();

/*

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);

*/

方式二 GetDlgItemText();SetDlgItemText();

/*

GetDlgItemText(IDC_EDIT1,ch1,10);

GetDlgItemText(IDC_EDIT2,ch2,10);

num1 = atoi(ch1);

num2 = atoi(ch2);

num3 = num1 + num2;

itoa(num3,ch3,10);

SetDlgItemText(IDC_EDIT3,ch3);

*/

方式三 GetDlgItemInt(); SetDlgItemInt();

/*

num1 = GetDlgItemInt(IDC_EDIT1);

num2 = GetDlgItemInt(IDC_EDIT2);

num3 = num1 + num2;

SetDlgItemInt(IDC_EDIT3,num3);

*/

方式四 将三个EDIT编辑框关联到当前CTestDlg类的3个成员变量,是普通的内置数据类型如int的成员变量

// DDX_TEXT函数将EDIT1的内容转换到m_num1

// 让UpdateData()去调用DoDataExchange(), true 就是获取对话框中输入的数据并赋予变量

UpdateData(true); m_num1

m_num3 = m_num1 + m_num2;

UpdateData(false); //用变量m_mum3的值去初始化对话框IDC_EDIT3

--- 快捷键 Ctrl + M 调出MFC ClassWizard, 这是个集成的快捷设计工具

---


方式五 将三个EDIT编辑框关联到当前CTestDlg类的控件类型的成员变量,如CEdit类型的控件

m_edit1.GetWindowText(ch1,10);

m_edit2.GetWindowText(ch2,10);

num1 = atoi(ch1);

num2 = atoi(ch2);

num3 = num1 + num2;

itoa(num3,ch3,10);

m_edit3.SetWindowText(ch3);

---


方式六 向控件发送SendMessage()的方式来获取或者设置控件窗口的文本

注意: 要在MSDN中查看WM_GETTEXT和WM_SETTEXT,才能看到SendMessage()的这种参数用法,单纯的查看SendMessage()是看不到这种参数传入wParam和lParam的用法的.

是谁给谁发送啊? 是点击的那个ADD按钮给控件窗口发送?

要获得窗口文本用WM_GETTEXT,设置则用WM_SETTEXT

点击按钮是向编辑框EDIT1发送消息要求获得其文本,保存到ch1

/*

WM_GETTEXT

An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller.

To send this message, call the SendMessage function with the following parameters.

SendMessage(

(HWND) hWnd, // handle to destination window

WM_GETTEXT, // message to send

(WPARAM) wParam, // number of characters to copy

(LPARAM) lParam // text buffer这时候这里居然是buffer了,这个参数真实多变....

);

WM_SETTEXT

An application sends a WM_SETTEXT message to set the text of a window.

To send this message, call the SendMessage function with the following parameters.

SendMessage(

(HWND) hWnd, // handle to destination window

WM_SETTEXT, // message to send

(WPARAM) wParam, // not used; must be zero

(LPARAM) lParam // window-text string (LPCTSTR)

);

*/

//调用SDK函数,要加双冒号::,最后要强制转换,有点疑惑,ch1是char,lparam是LPARAM要转换

::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);

::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch2);

num1 = atoi(ch1);

num2 = atoi(ch2);

num3 = num1 + num2;

itoa(num3,ch3,10);

m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3);

/*

(1)或者用IDC_EDIT1关联的成员控件变量m_edit1去获得IDC_EDIT1的窗口句柄

::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);

(2) 或者直接调用IDC_EDIT1的SendMessage(),那是自己发给自己?看MSDN解释:"An application sends a WM_GETTEXT message,.....call SendMessage() " 没有说是谁call

GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);

(3) 直接用关联的控件m_edit1,控件也是窗口,调用SendMessage

m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1)

*/

方式七 SendDlgItemMessage()

SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);

SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);

num1 = atoi(ch1);

num2 = atoi(ch2);

num3 = num1 + num2;

itoa(num3,ch3,10);

SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);

//---

// 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)
m_num1 = 0;
m_num2 = 0;
m_num3 = 0;
//}}AFX_DATA_INIT

m_bIsCreate = false;
}

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_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()->GetWindowText();
/*
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);
*/

//------ 方式二 GetDlgItemText();SetDlgItemText();
/*
GetDlgItemText(IDC_EDIT1,ch1,10);
GetDlgItemText(IDC_EDIT2,ch2,10);
num1 = atoi(ch1);
num2 = atoi(ch2);
num3 = num1 + num2;
itoa(num3,ch3,10);
SetDlgItemText(IDC_EDIT3,ch3);
*/

//------ 方式三 GetDlgItemInt(); SetDlgItemInt();
/*
num1 = GetDlgItemInt(IDC_EDIT1);
num2 = GetDlgItemInt(IDC_EDIT2);
num3 = num1 + num2;
SetDlgItemInt(IDC_EDIT3,num3);
*/

//------ 方式四 将三个EDIT编辑框关联到当前CTestDlg类的3个成员变量,是普通的内置数据类型如int的成员变量
// DDX_TEXT函数将EDIT1的内容转换到m_num1
/*
UpdateData(true); // 让UpdateData()去调用DoDataExchange(), true 就是获取对话框中输入的数据并赋予变量m_num1
m_num3 = m_num1 + m_num2;
UpdateData(false); //用变量m_mum3的值去初始化对话框IDC_EDIT3
*/

//------ 方式五 将三个EDIT编辑框关联到当前CTestDlg类的控件类型的成员变量,如CEdit类型的控件
/*
m_edit1.GetWindowText(ch1,10);
m_edit2.GetWindowText(ch2,10);
num1 = atoi(ch1);
num2 = atoi(ch2);
num3 = num1 + num2;
itoa(num3,ch3,10);
m_edit3.SetWindowText(ch3);
*/

//------ 方式六 向控件发送SendMessage()的方式来获取或者设置控件窗口的文本
// 注意: 要在MSDN中查看WM_GETTEXT和WM_SETTEXT,才能看到SendMessage()的这种参数用法,单纯的查看SendMessage()是看不到这种参数传入wParam和lParam的用法的.
// 是谁给谁发送啊? 是点击的那个ADD按钮给控件窗口发送?
// 要获得窗口文本用WM_GETTEXT,设置则用WM_SETTEXT
//点击按钮是向编辑框EDIT1发送消息要求获得其文本,保存到ch1
/*
WM_GETTEXT
An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller.

To send this message, call the SendMessage function with the following parameters.

SendMessage(
(HWND) hWnd,              // handle to destination window
WM_GETTEXT,               // message to send
(WPARAM) wParam,          // number of characters to copy
(LPARAM) lParam           // text buffer这时候这里居然是buffer了,这个参数真实多变....
);

WM_SETTEXT
An application sends a WM_SETTEXT message to set the text of a window.

To send this message, call the SendMessage function with the following parameters.

SendMessage(
(HWND) hWnd,              // handle to destination window
WM_SETTEXT,               // message to send
(WPARAM) wParam,          // not used; must be zero
(LPARAM) lParam           // window-text string (LPCTSTR)
);
Parameters

*/
//调用SDK函数,要加双冒号::,最后要强制转换,有点疑惑,ch1是char,lparam是LPARAM要转换
::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch2);
num1 = atoi(ch1);
num2 = atoi(ch2);
num3 = num1 + num2;
itoa(num3,ch3,10);
m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3);
/*
(1)或者用IDC_EDIT1关联的成员控件变量m_edit1去获得IDC_EDIT1的窗口句柄
::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
(2) 或者直接调用IDC_EDIT1的SendMessage(),那是自己发给自己?看MSDN解释:"An application sends a WM_GETTEXT message,.....call SendMessage() " 没有说是谁call
GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
(3) 直接用关联的控件m_edit1,控件也是窗口,调用SendMessage
m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1)
*/

//------ 方式七 SendDlgItemMessage()
SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
num1 = atoi(ch1);
num2 = atoi(ch2);
num3 = num1 + num2;
itoa(num3,ch3,10);
SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)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:");
}

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