cmpt 212 more mfc controls, messages and handlers mfc controls.pdf · cmpt 212 more mfc controls,...

31
CMPT 212 More MFC Controls, Messages and Handlers Spring 2007

Upload: trantuyen

Post on 20-Sep-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

CMPT 212More MFC Controls,

Messages and Handlers

Spring 2007

Page 2: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Overview (1 / 2) Look at some Edit box styles More info about dialogs Using MessageBox() Dealing with mouse messages Dealing with keyboard messages Text output

Page 3: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Overview (2 / 2) More controls:

Multiline edit text controls Group boxes Check boxes Radio buttons List boxes Combo boxes

Page 4: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Some EditBox Styles (1 / 3) Some controls can have a number of styles,

determined by the flags supplied in the resourcedefinition file

Edit box is one of these controls Already seen in how to force edit boxes to accept

only numbers (example from text): ES_NUMBER

Page 5: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Some EditBox Styles (2 / 3) Some other styles:

ES_PASSWORD - forces the edit box to display “*” instead ofreal characters

ES_READONLY - also already seen, edit box cannot be edited ES_LOWERCASE / ES_UPPERCASE - all characters are

automatically converted to lower/upper case ES_LEFT / ES_RIGHT / ES_CENTER - specifies text alignment ES_MULTILINE - multiline textbox, will come back to it later

Page 6: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Some EditBox Styles (3 / 3) These styles are ORed together As you would expect, some styles cannot be ORed

together (e.g. ES_LEFT and ES_RIGHT) Sample definition:

EDITTEXT IDC_AGE, 10, 10, 30, 16,ES_NUMBER | ES_CENTER

Creates a center-aligned textbox that only accepts digitsas input

Page 7: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Modal Dialogs Block execution of the application until they are

dismissed Return a value (indirectly, by calling

EndDialog(value)), depending on how it wasdismissed IDCANCEL is returned if the user closes the dialog (via the

“X” button) Can check this value and determine what to do next

Page 8: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Sample DialogResource DefinitionLogin DIALOG 50, 50, 130, 130CAPTION "User Authorization"{ LTEXT "Enter userid:", IDC_STATIC, 30, 20, 50, 8 EDITTEXT IDC_USERID, 30, 30, 70, 16

LTEXT "Password:", IDC_STATIC, 30, 50, 40, 8 EDITTEXT IDC_PASSWORD, 30, 60, 70, 16, ES_PASSWORD DEFPUSHBUTTON "Log in", IDC_LOGIN, 50, 100, 30, 15}

Page 9: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

MessageBox()(1 / 2) The quick-and-dirty way to exchange small bits of information

with the user Pops up a message box that can have a number of different

styles Default style is MB_OK, but can also be yes / no, yes / no /

cancel, ok / cancel, etc. May also specify an icon to display

When to use it? User makes a mistake, to notify the user Your program needs an answer to a simple query (e.g. “Are you

sure you want to continue?”)

Page 10: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

MessageBox()(2 / 2) Syntax:

MessageBox(“Some useful message/query”, “Title”,STYLE_FLAGS)

E.g.:MessageBox(“You may not schedule an appointment

here”, “Overlaps with Existing Appointment”,MB_OK | MB_ICONSTOP);

Message boxes are application modal

Page 11: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Mouse Messages (1 / 4) Similarly to Java, can “listen” to many different

kinds of messages (button down, button up, doubleclick)

We will only look at left / right button down, but theothers are similar:

Messages to listen for (these go into your messagemap): ON_WM_LBUTTONDOWN() ON_WM_RBUTTONDOWN()

Page 12: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Mouse Messages (2 / 4) Need to override the behaviour provided by the

CWndFrame baseclass of OnLButtonDown() /OnRButtonDown(): Firstly, signature is:

afx_msg void OnLButtonDown(UINT flags,CPoint point)

Likewise for OnRButtonDown() Now add this method to declaration in the .h and provide a

definition in the .cpp

Page 13: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Mouse Messages (3 / 4) The two arguments are:

Flags: Which mouse button was pressed Whether user was holding shift and / or control down

Point: The coordinates of where the mouse was pressed (with

respect to the client window)

Page 14: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Mouse Messages (4 / 4)

Now that you have these two bits ofinformation you can do this like: Check if there is something at this location

(needed for the GUI part of the project, moredetails to follow)

Do something at this location (e.g. printsomething)

Page 15: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Basic Drawing / PrintingTechniques

Require a handle on the current client area: Achieved with the CClientDC (Client device

context) object: CClientDC dc(this);

Use it to draw or print text: dc.TextOut(x, y, text, length);

We will come back to drawing techniques later

Page 16: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Keyboard Messages(1 / 2) Similar to mouse messages The window needs to listen for “ON_WM_CHAR”

messages Needs to also override the base-class definition of

OnChar. Signature: afx_msg void OnChar(UINT c, UINT repCount, UINT

flags)

c = character code for the key pressed repCount = repeat count flags = flags such as whether ALT or CTRL keys were also

down

Page 17: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Keyboard Messages(2 / 2)

Note that the first argument of OnChar is notnecessarily the character entered - it canalso represent a keystroke like backspace Examine the example from Chapter 3.4 of the

textbook

Page 18: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

More on Text Output

If you wish (or have to) to write text out usingCPaintDC or the likes (e.g. CClientDC), youneed to handle things like centering textyourself This is the only problem we will consider for now,

we will come back to fonts later By default CPaintDC prints text left-aligned

starting from the point given to it (via theTextOut() call)

Page 19: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Centering Text (1 / 2)

How should we do this? Find the dimensions of the string as it will appear

when printed Use these dimensions to adjust the (x, y) location

passed to TextOut() To find the dimensions, call GetTextExtent():

CSize nCSizeText = dc.GetTextExtent(text,strlen(text));

CSize is a simple structure with width and heightmembers

Page 20: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Centering Text (2 / 2)

Now that we know the size, we can center it: Compute the X-offset:

int xCentered = xOrigin - width/2;

Possibly do the same for Y (depending whetheryou want to have it centered wrt to Y)

Print it with TextOut(): dc.TextOut(xCentered, y, text,

strlen(text));

Page 21: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Creating MultilineText Boxes Define a regular text box in the resource file Adjust dimensions to “taste” Add the following two flags to the resource file

definition of the textbox: ES_MULTILINE (multiline edit box) ES_WANTRETURN (edit box consumes “Enter”)

Lastly, add scroll bars as necessary: WS_VSCROLL - vertical scroll bar WS_HSCROLL - horizontal scroll bar

Now from the implementation perspective you cantreat it as you would a regular edit box

Page 22: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Checkboxes (1 / 2) Simple checkboxes in MFC can be in two states -

checked and unchecked Can also create checkboxes with an “intermediate”

state using BS_AUTO3STATE As usual, need to add the proper bit of code to the

resource file:AUTOCHECKBOX “Save Settings”, IDSAVE, 10, 10, 60, 10 Creates a checkbox with a label “Save Settings”

Now can access it from code with GetDlgItem andget / set the state of the checkbox

Page 23: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Checkboxes (2 / 2)

Example:CButton *checkBox = (CButton *)

GetDlgItem(IDSAVE);

checkBox->SetCheck(BST_CHECKED);

checkBox->GetCheck();

Note that the checkbox is treated as a CButton CButton is used to represent push buttons,

checkboxes and radio buttons

Page 24: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Grouping with Group Boxes Groups are represented by a thin frame

surrounding similar content E.g. - a group box around download

preferences of a web browser Creating a group box is simple - just add the

entry to the resource file:GROUPBOX “Downloads”, IDC_STATIC, 10, 10, 100, 200 It is common to use IDC_STATIC for group

boxes and labels as typically they do notgenerate any interesting events

Page 25: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Radio Buttons (1 / 2)

Very similar to checkboxes, except only oneradio button in a group can be turned on at atime

Used to represent a discrete choice E.g. Car Colors: Red / Yellow / Blue …

MFC provides a callGetCheckedRadioButton() that returns whichof the given buttons (from a group) iscurrently checked

Page 26: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Radio Buttons (2 / 2) Group boxes do not actually play a role in grouping controls, you

need to do this when defining the controls themselves by usingthe WS_GROUP flag GROUPBOX "Flavors", IDC_STATIC, 15, 10, 80, 50 AUTORADIOBUTTON "Chocolate", IDC_CHOCOLATE, 20, 20, 60,

10, WS_GROUP… GROUPBOX "Container", IDC_STATIC, 15, 60, 80, 40 AUTORADIOBUTTON "Cone", IDC_CONE, 20, 70, 60,

10, WS_GROUP…

The controls from the first to the second occurrence ofWS_GROUP are now grouped and from the second to the endare grouped as well

Page 27: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

List Boxes (1 / 3) Display a number of items that the user can select

Can be either single selection or multiple selection list We will only look at single selection list boxes

MFC CListBox provides methods to add/changecontent of the list box as well as retrieve theselected state of the list box

Resource file definition:LISTBOX IDC_MYLIST, 10, 10, 50, 200, WS_VSCROLL Last flag is optional and adds a vertical scroll bar

Page 28: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

List Boxes (2 / 3) Adding items to the end of a list box:

CListBox lb;lb.AddString(“Steak”); Can also insert into a specific index via InsertString(index,

“Item”); Delete a single item:

lb.DeleteString(“X”);

Delete all items:lb.ResetContent();

Find an index of an item:int indY = lb.FindString(“Y”);

Page 29: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

List Boxes (3 / 3)

Lastly, we want to determine what item theuser has selected: Grab the CListBox control with the usual

GetDlgItem() call Call CListBox::GetCurSel() to determine the

index of the current selection Use this index to retrieve the item from the list

box with CListBox::GetText(index, buffer);

Page 30: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Combo Boxes (1 / 2) Very similar to lists, but only one item is

displayed at a time The user can see the entire list by clicking

on the “down” arrow Resource file entry:

COMBOBOX IDC_MYCOMBO, 10, 10, 50, 200,CBS_SORT | CBS_DROPDOWNLIST

The last two flags tell the combo box to sort itscontents and to not show the list unless the userclicks on the “down” arrow

Page 31: CMPT 212 More MFC Controls, Messages and Handlers MFC Controls.pdf · CMPT 212 More MFC Controls, Messages and Handlers Spring 2007. ... y) location passed to ... COMBOBOX IDC_MYCOMBO,

Combo Boxes (2 / 2)

Using the combo box in your code is verysimilar to using a list box: AddString(), GetCurSel(), FindString(),

DeleteString() and ResetContent() do the exactsame thing as list box

GetLBText() is equivalent to GetText() of list box