Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey...

27
Chapter 7 Controls

description

Introduction Example: push button control –Don’t need to draw a rectangle on the screen –Don’t need to process the mouse message –Everything is almost done. Just add it!!! –Notification will be given when clicked.

Transcript of Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey...

Page 1: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Chapter 7Controls

Page 2: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

2

Introduction (1/4)• Control– Special kind of window – Designed to convey information to the user or to

acquire input– Reduce the tedium of windows programming and

promote a consistent user interface

Page 3: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Introduction• Example: push button control– Don’t need to draw a rectangle on the screen– Don’t need to process the mouse message

– Everything is almost done. Just add it!!!– Notification will be given when clicked.

Page 4: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

4

Introduction• MFC Classic Controls

Control Type WNDCLASS MFC ClassButtons "BUTTON" CButtonList boxes "LISTBOX" CListBoxEdit controls "EDIT" CEditCombo boxes "COMBOBOX" CComboBoxScroll bars "SCROLLBAR" CScrollBarStatic controls "STATIC" CStatic

Page 5: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

5

How it works• Controls and its parent window

– Notification Message• When its states change• Example) A button is being clicked

– Member Functions of the control• Change or get the state of the control. • Example) Change a button to be inactive

Controls(Child Window) Parent Window

① Notification

② function call

Page 6: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

6

MFC Class Hierarchy

A control is also a window!

Page 7: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

7

Button Control (1/8)• Various kinds of the Button Control

Page 8: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

8

Button Control• Two ways of creating a control

① Create one by hand (coding)

② Create one using the dialog edit window

Page 9: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

9

Button control by hand• Procedure:– 1. Adding CButton object as a member variable

– 2. Call CButton‘s Create(..) member function to create• Ex) Call it inside CChildView::OnCreate() function

m_button.Create( _T(“Push Here!“), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(100, 100, 200, 130), this, 101);

In CChildView class : ChildView.h

CButton m_button; // adding CButton object

Page 10: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

10

Button Control• CButton::Create() member function

– Caption : title string– Style : Style as a button and as a window

Ex) WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON

– Rect : position and size– pParentWnd : pointer to parent window– UINT nID : ID of the control (ex, 101, 102…)

BOOL CButton::Create (Caption, Style, rect, pParentWindow,

ID) ;

Windows style CButton style

Page 11: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

11

Styles of CButtonStyle DescriptionBS_PUSHBUTTON Creates a standard push button controlBS_DEFPUSHBUTTON Creates a default push button; used in dialog boxes

to identify the push button that's clicked if Enter is pressed

BS_CHECKBOX Creates a check box controlBS_AUTOCHECKBOX Creates a check box control that checks and

unchecks itself when clicked

BS_3STATE Creates a three-state check box controlBS_AUTO3STATE Creates a three-state check box control that cycles

through three states—checked, unchecked, and indeterminate—when clicked

BS_RADIOBUTTON Creates a radio button controlBS_AUTORADIOBUTTON

Creates a radio button control that, when clicked, checks itself and unchecks other radio buttons in the group

BS_GROUPBOX Creates a group box control

Page 12: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Additional Styles of CButtonStyle DescriptionBS_LEFTTEXT Moves the text accompanying a radio button or

check box control from the button's right (the default) to its left

BS_RIGHTBUTTON Same as BS_LEFTTEXTBS_LEFT Left justifies the button text in the control rectangleBS_CENTER Centers the button text in the control rectangleBS_RIGHT Right justifies the button text in the control rectangleBS_TOP Positions the button text at the top of the control

rectangleBS_VCENTER Positions the button text in the center of the control

rectangle verticallyBS_BOTTOM Positions the button text at the bottom of the control

rectangleBS_MULTILINE Allows text too long to fit on one line to be broken

into two or more lines

Page 13: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

When clicking a button• Windows sends Notification Message

• Message Handler for the notification

1. Add message handler MACRO in massage map

2. Implement the message handler function

Controls(Child Window) Parent Window

Notification

Page 14: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

14

Adding Message Handler• 1. Add message handler MACRO in massage map

• 2. Implement the message handler function

ON_BN_CLICKED( ID, function name)

Ex) ON_BN_CLICKED( 101, OnButtonClicked) // in massage map

void CChildView::OnButtonClicked( ) // message handler{ AfxMessageBox(_T(“Button is clicked."));}

Page 15: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

When changing state of CButton• Call member functions of CButton

– Change(Set) the current state– Acquire(Get) the current state

Controls(Child Window) Parent Window

function call

Page 16: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

16

Member functions of CButton1. Change the state of the check box

2. Check the state of the check box

m_button.SetCheck(BST_CHECKED);m_button.SetCheck(BST_UNCHECKED);

if ( m_button.GetCheck() == BST_CHECKED ) AfxMessageBox(_T(“Button is checked”));

Page 17: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Radio button • Only one at a time is checked.• They are connected.

• You can make it manually// Radio Button Manual Creation Examplevoid CChildView::OnRadioButton1Clicked () {

m_wndRadioButton1.SetCheck (BST_CHECKED); m_wndRadioButton2.SetCheck

(BST_UNCHECKED); m_wndRadioButton3.SetCheck

(BST_UNCHECKED); }

Page 18: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Auto Radio Button• WS_GROUP connects Radio buttonsm_RadioButton1.Create (_T ("COM1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect1, this, IDC_COM1); m_RadioButton2.Create (_T ("COM2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect2, this, IDC_COM2); m_RadioButton3.Create (_T ("COM3"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect3, this, IDC_COM3);

m_RadioButton4.Create (_T (“Student1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect4, this, IDC_COM1); m_RadioButton5.Create (_T (" Student2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect5, this, IDC_COM2);

Page 19: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Coding Practice• Creating the following program using

CButtons

Page 20: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

20

Button Control• Two ways of creating a control

① Create one by hand (coding)

② Create one using the dialog edit window

Page 21: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

21

Dialog Edit Window• Resource View -> Edit “Dialog”– Creating and designing controls using toolbox

• Control variables– Each control can be connected with a control

variable. – Call the member functions of the control variable

to change/get the state of the control

Page 22: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Coding Practice• Make a new project of Dialog Based

Application• Design the dialog using Dialog Edit Window

and tool box

Page 23: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Adding Event Message Handler • Use Properties window to add message

handlers

Page 24: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Adding Event Message Handler Alternative way• In dialog edit window, mouse right button

click on a control Add Event Handler

Page 25: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

25

Creating control variables• mouse right button click on a control

Add variables

Page 26: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

26

Creating control variables• Added things:

// In Header fileclass CTestDlg::public CDiglog{ ...

CButton m_button;}

// In Source filevoid CTestDlg::DoDataExchange(CDataExchange* pDX){

CFormView::DoDataExchange(pDX);//{{AFX_DATA_MAP(CExButtonView)DDX_Control(pDX, IDC_BUTTON1, m_button);//}}AFX_DATA_MAP

}

Page 27: Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.

Coding Practice• Creating the following program using Dialog

based application template• When pushing the button, show the

information