tran anh tuan a. help to modify a control’s behavior by deriving classes of your own from the mfc...

36
Tran Anh Tuan A

Upload: austin-johnson

Post on 23-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Tran Anh Tuan A

Page 2: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Help to modify a control’s behavior by deriving classes of your own from the MFC control classes

Help to build reusable, self-contained control classes that respond to their own notification message

Modify: MFC Control Classes + Control Events

New Style of Controls

Page 3: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Numeric Edit Controls Owner-Draw List Boxes Graphical Push Buttons Customizing a Control’s Colors

Page 4: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

A normal edit control accepts a wide range of characters, including numbers, letters of the alphabet, and punctuation symbols.

A numeric edit control accepts only numbers. It's perfect for entering phone numbers, serial numbers, IP addresses, and other numeric data.

Page 5: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Example : Create an edit control which allows user input Student ID and the length must be 7 characters.

Page 6: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 1 : New a class name : CNumEdit that is derived from MFC Classes CEdit

Page 7: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

When an edit control has the input focus and a character key is pressed, the control receives a WM_CHAR message.

Page 8: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 2: Code in OnChar function to make the constrains

Page 9: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 3 : Create variable for a object from class CNumEdit in class Dialog

Page 10: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Transfer of data between a control in a dialog box and a data member.

Result in running:

Page 11: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

By default, items in a list box consist of strings of text. Should you need a list box that displays graphical images instead of text, you can create an owner-draw list box—one whose contents are drawn by your application, not by Windows—by following two simple steps.◦ Derive a new list box class from CListBox, and override

CListBox::MeasureItem and CListBox::DrawItem. Also override PreCreateWindow, and make sure that either LBS_OWNERDRAWFIXED or LBS_OWNERDRAWVARIABLE is included in the list box style.

◦ Instantiate the derived class, and use Create or CreateEx to create the list box.

Page 12: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Example: Create a Graphical ListBox and show the Graphical Image when choose a Item in ListBox

Page 13: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 1: Design the Dialog with a ListBox and Picture Control

Remember :in the ListBox◦ Owner Draw :

Variable◦ Has strings :

Check

Page 14: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 2 : In Resource Tab. Insert Bitmap and remember these bitmap ID for later using

Page 15: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Choose a series of bmp Image in your Drives

Page 16: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 3: Create a new class COwnerDrawListBox to customize your ListBox

Page 17: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Add needed functions into the class

Page 18: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Make the code to customize the ListBox

Page 19: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained
Page 20: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 4: Add a object variable from COwnerDrawListBox into Class Dialog and make the transfer of data.

Page 21: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

OnInitDialog : I do AddItem into Custom ListBox

Page 22: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 5: When Choose a Item In ListBox. Image will be showed. Remember Event LBN_SELCHANGE for ListBox

Page 23: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Create control variable m_samplePic for Picture control and :

Page 24: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Result :

Page 25: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Two button styles that were first introduced in Windows 95—BS_BITMAP and BS_ICON—make graphical push buttons a breeze by taking a single image and creating a push button from it.

A BS_BITMAP-style push button (henceforth, a bitmap push button) displays a bitmap on the face of a push button.

A BS_ICON-style push button (an icon push button) displays an icon.

Page 26: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Example : change the button OK and Cancel into Graphical ones

Step 1: Insert new Icons into resource

Page 27: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 2: Change the properties of two button and add control valuable for them

Page 28: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 3: Make the code change apperance of OK and Cancel button in OnInitDialog

Page 29: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

MFC supports two mechanisms for changing a control's colors. Both rely on the fact that before a control paints itself, it sends its parent a message containing the handle of the device context used to do the painting.

The parent can call CDC::SetTextColor and CDC::SetBkColor on that device context to alter the attributes of any text drawn by the control. It can also alter the control's background color by returning a brush handle (HBRUSH).

Page 30: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Example : Change the background and text of input Name value for Edit Box

Step 1: Create Class CEditColor Derived from CEditand Add variable about color and function ton change them into class

Page 31: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained
Page 32: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Remember : Event WM_CTLCOLOR

Page 33: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained
Page 34: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Step 2: Make the Transfer Data and change color in OnInitDialog

Page 35: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained

Result :

Page 36: Tran Anh Tuan A.  Help to modify a control’s behavior by deriving classes of your own from the MFC control classes  Help to build reusable, self-contained