Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class...

Post on 04-Jan-2016

254 views 2 download

Tags:

Transcript of Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class...

Chapter 7Controls

2

Introduction

• MFC Classic Controls

Control Type WNDCLASS MFC Class

Buttons "BUTTON" CButton

List boxes "LISTBOX" CListBox

Edit controls "EDIT" CEdit

Combo boxes "COMBOBOX" CComboBox

Scroll bars "SCROLLBAR" CScrollBar

Static controls "STATIC" CStatic

3

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

4

Button Control

• Two ways of creating a control

① Create one by hand (coding)

② Create one using the dialog edit window

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

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

7

Member functions of CButton

1. 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”));

8

Button Control

• Two ways of creating a control

① Create one by hand (coding)

② Create one using the dialog edit window

9

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

Coding Practice

• Make a new project of Dialog Based Application

• Design the dialog using Dialog Edit Window and tool box

Adding Event Message Handler

• Use Properties window to add message handlers

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

click on a control Add Event Handler

13

Creating control variables

• mouse right button click on a control Add variables

14

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

}

Coding Practice

• Creating the following program using Dialog based application template

• When pushing the button, show the information

More Controls

17

Edit Control (1/6)

• Edit Control

• Line option:

18

Edit Control (1/6)

• Properties

19

Edit Control (2/6)

• Edit Control StyleStyle DescriptionES_LEFT Left-aligns text in the control.ES_CENTER Centers text in the control.ES_RIGHT Right-aligns text in the control.ES_AUTOHSCROLL Permits the edit control to scroll horizontally without a horizontal

scroll bar. To add a horizontal scroll bar, include the style WS_HSCROLL.

ES_AUTOVSCROLL Permits the edit control to scroll vertically without a vertical scroll bar. To add a vertical scroll bar, include the style WS_VSCROLL.

ES_MULTILINE Creates a multiline edit control.ES_LOWERCASE Displays all characters in lowercase.ES_UPPERCASE Displays all characters in uppercase.ES_PASSWORD Displays asterisks instead of typed characters.ES_READONLY Creates an edit control whose text can't be edited.ES_NOHIDESEL Prevents the edit control from hiding the selection when the control

loses the input focus.ES_OEMCONVERT Performs an ANSI-to-OEM-to-ANSI conversion on all characters typed

into the control so that the application won't get unexpected results if it performs an ANSI-to-OEM conversion of its own. Obsolete.

ES_WANTRETURN Programs the Enter key to insert line breaks instead of invoking the default push button for multiline edit controls used in dialog boxes.

20

Edit Control (4/6)

• Edit Control Notification Messages

Notification Sent When Message-Map Macro

EN_UPDATE The control's text is about to change. ON_EN_UPDATEEN_CHANGE The control's text has changed. ON_EN_CHANGEEN_KILLFOCUS The edit control loses the input focus. ON_EN_KILLFOCUSEN_SETFOCUS The edit control receives the input

focus.ON_EN_SETFOCUS

EN_HSCROLL The edit control is scrolled horizontally using a scroll bar.

ON_EN_HSCROLL

EN_VSCROLL The edit control is scrolled vertically using a scroll bar.

ON_EN_VSCROLL

EN_MAXTEXT A character can't be entered because the edit control already contains the number of characters specified with CEdit::LimitText or CEdit::SetLimitText.

ON_EN_MAXTEXT

EN_ERRSPACE An operation fails because of insufficient memory.

ON_EN_ERRSPACE

Coding Practice

• Adding Edit Control to the dialog application

How to change the state of CEdit

• Call member functions of CEdit

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

• To ways of connecting a control variable with the CEdit Control– By hand (implement in the DoDataExchange

function)– An automatic way (add variables control variable)

Controls(Child Window)

Parent Window

function call

How to change the state of CEdit

Two ways to connect a control variable with the CEdit Control

By hand (implement in the DoDataExchange function)

An automatic way (add variables control variable)

24

Edit Control (5/6)

• Inserting and Retrieving Text

• Set maximum number of character

• Clear, Cut, Copy, Paste and Undo

m_edit.SetWindowText(_T(" 초기값입니다 ."));CString str; m_edit.GetWindowText(str);

m_edit.SetLimitText(10);

m_edit.Clear();m_edit.Cut();m_edit.Copy();m_edit.Paste();m_edit.Undo();

25

Edit Control (6/6)

• Select text for editing

Before ①

After ①

After ②

① m_edit.SelSel(5, 7); ② m_edit.ReplaceSel("ABC");

26

Scroll bar control (1/3)

• Scroll bar codes (interfaces)

27

CScrollBar Class(2/3)

• Member functions

Member functions meaning

SetScrollRange() Set the min and max range of the scroll bar

SetScrollPos() Set the thumb position

GetScrollPos() Get the thumb position

Scroll bar notification(3/3)

• Messages:

• Add message MACRO in MessageMap– Scroll Bars have no ID.

• Event Message Handler

ON_WM_HSCROLL() or ON_WM_VSCROLL()ON_WM_HSCROLL() or ON_WM_VSCROLL()

void OnHScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

void OnVScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

void OnHScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

void OnVScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

WM_HSCROLL/ WM_VSCROLLWM_HSCROLL/ WM_VSCROLL

29

Scroll bar message handler (4/3)

• WM_HSCROLL/WM_VSCROLL 메시지 핸들러

– nSBCode• Scroll bar code (types of event)

– nPos• Position of the thumb• Only use when nSBCode is SB_THUMBPOSITION or

SB_THUMBTRACK

– pScrollBar• Pointer to the scroll bar

void OnHScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

void OnVScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

Coding Practice

• Add a edit control and scroll bar control • Set the range of the scroll bar to (0, 100)• Show the position value of the thumb in the

edit control