chapter 7 controls. 2 introduction (1/4) control –special kind of window –designed to convey...
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
Chapter 7Controls
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
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.
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
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
6
MFC Class Hierarchy
A control is also a window!
7
Button Control (1/8)• Various kinds of the Button Control
8
Button Control• Two ways of creating a control
① Create one by hand (coding)
② Create one using the dialog edit window
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
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
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
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
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
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."));}
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
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”));
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); }
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);
Coding Practice• Creating the following program using
CButtons
20
Button Control• Two ways of creating a control
① Create one by hand (coding)
② Create one using the dialog edit window
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
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
25
Creating control variables• mouse right button click on a control
Add variables
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
}
Coding Practice• Creating the following program using Dialog
based application template• When pushing the button, show the
information