Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 7 Controls

2 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

3 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 4 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit Combo boxes"COMBOBOX"CComboBox Scroll bars"SCROLLBAR"CScrollBar Static controls"STATIC"CStatic

5 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) Controls (Child Window) Parent Window ① Notification ② function call

6 6 MFC Class Hierarchy A control is also a window!

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

8 8 Button Control Two ways of creating a control ① Create one by hand (coding) ② Create one using the dialog edit window

9 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 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 styleCButton style

11 11 Styles of CButton StyleDescription BS_PUSHBUTTONCreates a standard push button control BS_DEFPUSHBUTTONCreates a default push button; used in dialog boxes to identify the push button that's clicked if Enter is pressed BS_CHECKBOXCreates a check box control BS_AUTOCHECKBOXCreates a check box control that checks and unchecks itself when clicked BS_3STATECreates a three-state check box control BS_AUTO3STATECreates a three-state check box control that cycles through three states—checked, unchecked, and indetermin ate—when clicked BS_RADIOBUTTONCreates a radio button control BS_AUTORADIOBUTTO N Creates a radio button control that, when clicked, checks itself and unchecks other radio buttons in the group BS_GROUPBOXCreates a group box control

12 Additional Styles of CButton StyleDescription BS_LEFTTEXTMoves the text accompanying a radio button or check box c ontrol from the button's right (the default) to its left BS_RIGHTBUTTONSame as BS_LEFTTEXT BS_LEFTLeft justifies the button text in the control rectangle BS_CENTERCenters the button text in the control rectangle BS_RIGHTRight justifies the button text in the control rectangle BS_TOPPositions the button text at the top of the control rectangle BS_VCENTERPositions the button text in the center of the control rectangl e vertically BS_BOTTOMPositions the button text at the bottom of the control rectangl e BS_MULTILINEAllows text too long to fit on one line to be broken into two or more lines

13 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) Controls (Child Window) Parent Window Notification

14 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.")); }

15 When changing state of CButton Call member functions of CButton –Change(Set) the current state –Acquire(Get) the current state Controls (Child Window) Controls (Child Window) Parent Window function call

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

17 Radio button Only one at a time is checked. They are connected. You can make it manually // Radio Button Manual Creation Example void CChildView::OnRadioButton1Clicked () { m_wndRadioButton1.SetCheck (BST_CHECKED); m_wndRadioButton2.SetCheck (BST_UNCHECKED); m_wndRadioButton3.SetCheck (BST_UNCHECKED); } // Radio Button Manual Creation Example void CChildView::OnRadioButton1Clicked () { m_wndRadioButton1.SetCheck (BST_CHECKED); m_wndRadioButton2.SetCheck (BST_UNCHECKED); m_wndRadioButton3.SetCheck (BST_UNCHECKED); }

18 Auto Radio Button WS_GROUP connects Radio buttons m_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); m_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);

19 Coding Practice Creating the following program using CButtons

20 20 Button Control Two ways of creating a control ① Create one by hand (coding) ② Create one using the dialog edit window

21 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

22 Coding Practice Make a new project of Dialog Based Application Design the dialog using Dialog Edit Window and tool box

23 Adding Event Message Handler Use Properties window to add message handlers

24 Adding Event Message Handler Alternative way In dialog edit window, mouse right button click on a control  Add Event Handler

25 25 Creating control variables mouse right button click on a control  Add variables

26 26 Creating control variables Added things: // In Header file class CTestDlg::public CDiglog {... CButton m_button; } // In Source file void CTestDlg::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CExButtonView) DDX_Control(pDX, IDC_BUTTON1, m_button); //}}AFX_DATA_MAP }

27 Coding Practice Creating the following program using Dialog based application template When pushing the button, show the information


Download ppt "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."

Similar presentations


Ads by Google