第 10 章 控件 (control)

28
第 10 第 第第 (Control) 10.1 第第第第第第第第 10.2 第第第第 10.3 第第第第第第第 ( 第第 )

Upload: yin

Post on 14-Jan-2016

170 views

Category:

Documents


0 download

DESCRIPTION

第 10 章 控件 (Control). 10.1 标准控件及其使用 10.2 通用控件 10.3 控件的背景颜色 ( 自学 ). 控件 (Control) :为方便用户与应用程序的交互,程序界面上放置的能够用于处理用户事件并作出响应的图形部件,称为 ~ 。如, 命令按钮 (Command Button) 文本框 (Text Box) 滚动条 (Progress Bar) 列表框 (List Box) 静态文本 (Static) ……. 控件的特点: 图形化 ( 派生自 CWnd 类 ) ,具有窗口的一切特性 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第 10 章 控件 (Control)

第 10 章 控件 (Control)

10.1 标准控件及其使用10.2 通用控件10.3 控件的背景颜色 ( 自学 )

Page 2: 第 10 章 控件 (Control)

控件 (Control) :为方便用户与应用程序的交互,程序界面上放置的能够用于处理用户事件并作出响应的图形部件,称为 ~ 。如,

命令按钮 (Command Button)文本框 (Text Box)滚动条 (Progress Bar)列表框 (List Box)静态文本 (Static)……

控件的特点:1) 图形化 ( 派生自 CWnd 类 ) ,具有窗口的一切特性2) 分为可见 (Visible) 控件和非可见 (Invisible) 控

件3) 控件通常作为子窗口出现在应用程序的界面上4) MFC 中控件以类的形式提供

Page 3: 第 10 章 控件 (Control)

10.1 标准控件及其使用标准控件 (Standard control) :

A.交互对象B.应用于对话框,或主窗口,或工具栏C.接收用户信息,反馈 ( 显示 ) 信息

Page 4: 第 10 章 控件 (Control)

10.1 标准控件及其使用控件的使用规则:

1) 创 建 控 件 类 对 象 , 一 般 在 窗 口 的WM_CREATE 消息响应函数 OnCreate() 中编写逻辑代码。2) 建立消息响应,并编写相应的响应代码

Example :CListBox listbox;listbox.Create(…);

Page 5: 第 10 章 控件 (Control)

1. 静态文本控件 (CStatic)

用途:显示文本或图片。一般不接收用户事件消息。初始化成员函数: Create( ) 原型BOOL Create( LPCTSTR lpszText, // 字符串 DWORD dwStyle, // 样式 const RECT& rect, // 大小及位置 CWnd* pParentWnd, // 父窗口 UINT nID = 0xffff ); // 控件的资源标识

CObject

CCmdTarget

CWnd

CStatic

Page 6: 第 10 章 控件 (Control)

10.1 标准控件及其使用

Page 7: 第 10 章 控件 (Control)

10.1 标准控件及其使用

为了使应用程序的子窗口可见,有两个样式是必须选定的,

WS_CHILD WS_VISIBLE

如, CStatic m_static;

m_static.Create(“Static”, WS_CHILD | WS_VISIBLE | SS_CENTER,

CRect(20,20,100,40), this, 0)

分析例 10-1 不需要响应用户交互,因此,资源标识设为0 。

Page 8: 第 10 章 控件 (Control)

2. 按钮控件 (CButton)

CObject

CCmdTarget

CWnd

CButton

BOOL Create( LPCTSTR lpszCaption, // 显示文本 DWORD dwStyle, // 样式 const RECT& rect, // 大小及位置 CWnd* pParentWnd, // 父窗口 UINT nID ); // 资源标识

Page 9: 第 10 章 控件 (Control)

Example : #define IDB_BUTTON1 101 (Resource.h)

CButton button;

button.Create(“Button”, WS_CHILD | WS_VISIBLE |WS_BORDER, CRect(20,20,100,60), this, IDB_BUTTON1);

button.Create(“CHECK”, WS_CHILD | WS_VISIBLE | BS_CHECKBOX, CRect(20,20,100,60), this, IDB_BUTTON1);

button.Create(“RADIO”, WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, CRect(20,20,100,60), this, IDB_BUTTON1);

2. 按钮控件 (CButton)

Page 10: 第 10 章 控件 (Control)

CButton 的消息响应:

2. 按钮控件 (CButton)

Page 11: 第 10 章 控件 (Control)

Example:1) 消息映射

ON_BN_CLICKED(IDB_BUTTON1, OnButton1Clicked)2) 消息映射函数声明和定义

afx_msg void OnButton1Cliked();void CButtonDemoView::OnButton1Clicked() { … }

演示 CButton 消息映射过程

分析例 10-2

Page 12: 第 10 章 控件 (Control)

2. 按钮控件 (CButton)

Page 13: 第 10 章 控件 (Control)

3. 编辑控件 (CEdit)

CObject

CCmdTarget

CWnd

CEdit

CEdit :输入文本,或编辑文本, 是应用程序从用户处得到文本的主要对象 单行文本,或多行文本

Page 14: 第 10 章 控件 (Control)

3. 编辑控件 (CEdit)

BOOL Create( DWORD dwStyle, // 样式 const RECT& rect, // 大小及位置 CWnd* pParentWnd, // 父窗口 UINT nID ); // 资源标识

Page 15: 第 10 章 控件 (Control)

EN_MAXTEXT

The EN_MAXTEXT notification message is sent when the current text

insertion has exceeded the specified number of characters for the edit

control. The text insertion has been truncated.

分析例 10-3

Page 16: 第 10 章 控件 (Control)

10.2 通用控件

进度条 滑动条 微调器 图像列表 列表视图 工具栏

Page 17: 第 10 章 控件 (Control)

10.2 通用控件1. 进度条控件 (CProgressCtrl)

BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

dwStyle :PBS_VERTICAL    垂直进度条PBS_SMOOTH    平滑进度条(否则,块状填充)

Page 18: 第 10 章 控件 (Control)

Example: CProgrssCtrl progressCtrl; progressCtrl.Create(WS_CHILD | WS_BORDER | WS_VISIBLE,

CRect(20, 20, 100, 20), this, IDC_PROGBAR); progrssCtrl.SetRange(0, 100); progrssCtrl.SetStep(10); progrssCtrl.SetPos(0); for(int i = 0; i < 10; i++) progrssCtrl.StepIt();

1. 进度条控件 (CProgressCtrl)

Page 19: 第 10 章 控件 (Control)

2. 微调器控件 (CSpinButtonCtrl)

CSpinButtonCtrl 经 常 与 编 辑 框 控 件(CEdit) 配对使用。

BOOL CSpinButtonCtrl::Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

Page 20: 第 10 章 控件 (Control)

与 CEdit 控件的绑定: CWnd * CSpinButtonCtrl::SetBuddy(CWnd * pWndBuddy)

2. 微调器控件 (CSpinButtonCtrl)

Page 21: 第 10 章 控件 (Control)

Example:

CEdit spinEdit;

spinEdit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,

CRect(20,20,100,40), this, IDC_SPINNER);

CSpinButtonCtrl spin;

spin.Create(…);

spin.SetBuddy(&spinEdit);\

spin.SetRange(0,100);

spin.SetPos(0);

….

2. 微调器控件 (CSpinButtonCtrl)

Page 22: 第 10 章 控件 (Control)

10.2 通用控件分析例 10-4

定时器Timer

Page 23: 第 10 章 控件 (Control)

定时器的使用

定 时 器 告 诉 WINDOWS 一 个 时 间 间 隔 (Interval) , 然 后WINDOWS 以此时间间隔周期性触发程序。通常有两种方法来实现:

发送 WM_TIMER 消息 调用应用程序定义的回调函数

1.1 用 WM_TIMER 来设置定时器 先请看 SetTimer 这个 API 函数的原型 UINT_PTR SetTimer(

HWND hWnd, // 窗口句柄 UINT_PTR nIDEvent, // 定时器 ID ,区分多个定时器 UINT uElapse, // 时间间隔 , 单位为毫秒 TIMERPROC lpTimerFunc // 回调函数

);

SetTimer(m_hWnd,1,1000,NULL); // 一个 1 秒触发一次的定时器

Page 24: 第 10 章 控件 (Control)

定时器的使用

在 MFC 程序中 SetTimer 被封装在 CWnd 类中,调用就不用指定窗口句柄了,例如 :

UINT SetTimer(1,100,NULL);函数返回值就是第一个参数值表示此定时器的 ID 号。第二个参数表示定时间隔 100 毫秒 , 三个参数在这种方法中一般用 NULL 。

响应代码

启动定时器

消息映射

Page 25: 第 10 章 控件 (Control)

定时器的使用

1.2 调用回调函数此方法首先写一个如下格式的回调函数

void CALLBACK TimerProc(HWND

hWnd,UINT nMsg,

UINT nTimerid,DWORD dwTime);

然后再用 SetTimer(1,100,TimerProc) 函数来建一个定时器,第三个参数就是回调函数地址。

Page 26: 第 10 章 控件 (Control)

多定时器的使用

使用 MFC ,增加 WM_TIMER 的消息处理函数OnTimer 即可,请看如下例子

void CTimerTestDlg::OnTimer(UINT nIDEvent){

switch (nIDEvent){case 24: /// 处理 ID 为 24 的定时器

Draw1(); break;

case 25: /// 处理 ID 为 25 的定时器 Draw2(); break;

}}

Page 27: 第 10 章 控件 (Control)

取消定时器不再使用定时器后,我们应该调用 KillTimer 来取消定时, KillTimer 的原型如下

BOOL KillTimer(

HWND hWnd, // 窗口句柄 UINT_PTR uIDEvent // ID

);

在 MFC 程序中我们可以直接调用 KillTimer(int

nIDEvent) 来取消定时器。

定时器的使用

Page 28: 第 10 章 控件 (Control)

10.3 控件的背景颜色 ( 自学 )