Advanced Control Programming

advertisement
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 control
classes that respond to their own notification
message
Modify:
MFC Control Classes + Control Events
 New Style of Controls




Numeric Edit Controls
Owner-Draw List Boxes
Graphical Push Buttons
Customizing a Control’s Colors


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.

Example : Create an edit control which allows
user input Student ID and the length must be
7 characters.

Step 1 : New a class name : CNumEdit that is
derived from MFC Classes CEdit

When an edit control has the input focus and
a character key is pressed, the control
receives a WM_CHAR message.

Step 2: Code in OnChar function to make the
constrains

Step 3 : Create variable for a object from
class CNumEdit in class Dialog


Transfer of data between a control in a dialog
box and a data member.
Result in running:

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.

Example: Create a Graphical ListBox and show
the Graphical Image when choose a Item in
ListBox


Step 1: Design the Dialog with a ListBox and
Picture Control
Remember :
in the ListBox
◦ Owner Draw :
Variable
◦ Has strings :
Check

Step 2 : In Resource Tab. Insert Bitmap and
remember these bitmap ID for later using

Choose a series of bmp Image in your Drives

Step 3: Create a new class COwnerDrawListBox
to customize your ListBox

Add needed functions into the class

Make the code to customize the ListBox

Step 4: Add a object variable from
COwnerDrawListBox into Class Dialog and
make the transfer of data.

OnInitDialog : I do AddItem into Custom ListBox

Step 5: When Choose a Item In ListBox. Image will
be showed. Remember Event LBN_SELCHANGE for
ListBox

Create control variable m_samplePic for
Picture control and :

Result :



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.


Example : change the button OK and Cancel
into Graphical ones
Step 1: Insert new Icons into resource

Step 2: Change the properties of two button
and add control valuable for them

Step 3: Make the code change apperance of
OK and Cancel button in OnInitDialog


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).


Example : Change the background and text of
input Name value for Edit Box
Step 1: Create Class CEditColor Derived from
CEdit
and Add variable about color and function
ton change them into class

Remember : Event WM_CTLCOLOR

Step 2: Make the Transfer Data and change
color in OnInitDialog

Result :
Download