chapter 7 controls. 2 introduction mfc classic controls control typewndclassmfc class...

30
Chapter 7 Controls

Upload: clyde-randall

Post on 04-Jan-2016

254 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

Chapter 7Controls

Page 2: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 3: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 4: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

4

Button Control

• Two ways of creating a control

① Create one by hand (coding)

② Create one using the dialog edit window

Page 5: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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 6: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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 7: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 8: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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 MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 10: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

Coding Practice

• Make a new project of Dialog Based Application

• Design the dialog using Dialog Edit Window and tool box

Page 11: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

Adding Event Message Handler

• Use Properties window to add message handlers

Page 12: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

click on a control Add Event Handler

Page 13: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

13

Creating control variables

• mouse right button click on a control Add variables

Page 14: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

}

Page 15: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

Coding Practice

• Creating the following program using Dialog based application template

• When pushing the button, show the information

Page 16: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

More Controls

Page 17: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

17

Edit Control (1/6)

• Edit Control

• Line option:

Page 18: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

18

Edit Control (1/6)

• Properties

Page 19: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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.

Page 20: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 21: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

Coding Practice

• Adding Edit Control to the dialog application

Page 22: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 23: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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)

Page 24: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 25: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

25

Edit Control (6/6)

• Select text for editing

Before ①

After ①

After ②

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

Page 26: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

26

Scroll bar control (1/3)

• Scroll bar codes (interfaces)

Page 27: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 28: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 29: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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

Page 30: Chapter 7 Controls. 2 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit

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