Auto-painted button control with VC ++

zhaozj2021-02-11  233

Auto-painted button control with VC

Microsoft Windows is consistent with its rich

GUI,

Simple fucking

Work, was accepted by the majority of users. The dialog is one of the very important interface.

And Windows provides considerable interface resources for developers of their applications, many

Control can easily join the dialog box. But when we need to write a business application, you need

When you have a more friendly graphical user interface, you will find the poor of resources.

Visual C provides a CBitmap button class to alleviate on a certain program.

Add new resources. But when we ask for dynamically change graphics in the program, I

They have to consider giving up the CBITMAPBUTTON class, and sitting down to do something.

A possible way is to overrunate the CBUTTON class (ie, button control). This area is in this area

The size, position and ID number can be easily produced by appstadio. Of course, this is the most

The big benefit is to make full use of the C characteristics to encapsulate the code controlled code.

It avoids repetitive development, but also makes the program more concise, enhances the readability of the code.

.

Now we learn this method by creating a class called CColorButton.

. Although this is a very simple class, by calling member functions ChangeColor

Changing its color, the high-bright border is selected in the mouse point. But use this idea to

Simply add a few data members and overload a function, you can easily achieve what you need.

Features.

We must first understand the WM_DRAWITEM message before creating this class. Button

When a combination box, a list box, or a visual status of the menu changes, the system will send

A WM_DRAWITEM message gives these controlled owner windows. WPARAM for this message

Point out this control ID number, and iParam is a DrawItemStruct structure.

The pointer, the structure stores information about the items to be drawn, and the types needed to draw. DR

The AwitemStruct structure has the following format.

Typedef struct tagdrawitemstruct {

UINT CTLTYPE; // Control Type

UINT CTLID; // Control ID Number

Unit itemid; // Menu item index

Uint itemaction; // Describe the required drawing operation

Uint itemState; // Note the visible state after the drawing

HWND HWNDITEM; // Control window handle

HDC HDC; // Related equipment environment

Rect rcitem; // Draw control border

DWORD ITEMDATA; / / Specify the application defined with the menu item

32-bit value

DrawItemstruct;

Where ItemAction and ItemState determine the needs of the plot operation. Itemact

ION illustrates the desired drawing operation, one or more of the following values;

Value meaning

ODA_DRAWENTIRE needs to be re-control

ODA_FOCUS gets or lost input focus

ODA_SELECT Select Status Change

ItemState indicates the visible state of the item after the current drawing action occurs. The following is the shape

State logo:

Value meaning

ODS_CHECKD is only used in the menu ODS_DISABLE this item is blocked

ODS_FOCUS This item has input focus

ODS_GRAYED is only used in menus

ODS_SELECT This item is selected

(On) □ Chengdu Zeng Zhi

Auto-painted button control with VC

Using VC programming, it will be found that when the button controls receives the WM-DRMAWITEM message.

The DrawItem function of the CBOTTON class will call. Therefore, what we have to do is to use C

Polymorphism responds to MW-DrawItem messages by overloading the DrawItem function of the CButton class.

Below we actually construct a CCOL -ORBUTTON class.

Class CcolorButton: Public CButton

{Private:

ColorRef M-Color:

PUBLIC:

CColorButton (): cbutton (), m-color (0) {}; //

Function

Void ChangeColor (ColorRef color); // change color

Virtual Void DrawItem (LPDrawItemstruct LPDRAWITEMSTRUCT

) // overloaded function

}

// Overloaded virtual function

Void CcolorButton :: DrawItem (LPDrawItemstruct LPDRAWIT

Emtruct)

{

CDC DC;

DC. Attach (LPDrawItemstruct-> HDC); // Get drawing settings

Preparation environment CDC

Verify (LPDrawItemstruct-> CTLTYPE == ODT-Button);

IF (LPDrawItemstruct-> Itemaction & ODA-DRAWENTIRE)

{

// Redraw over the entire control

Cbrush Brush (m-color);

DC. FillRect (& (LPDrawItemstruct-> ReItem), & brush

;

} IF ((LPDrawItemStruct-> itemState & ODS-SELECTED

) &&

(LPDrawItemstruct-> Itemaction &

(ODA-SELECT | ODA-DRAWENTIRE))) {// Select this control ===> Highlight Border

ColorRef fc = RGB (255-GETRVALUE (M-color), 255-GETG

Value (m-color), 255-getBValue (m-color);

CBRUSH Brush (FC);

DC. Framerect (& (LPDrawItemstruct-> rcitem), & brush

);

} If (! (Lpdrawitemstruct-> itemState & ODS-SELECT

ED) &&&

(LPDRAWITEMSTRUCT-> Itemaction & ODA-SELECT) {

// The selected status of the control ends ===> Remove the border

Cbrush Brush (m-color);

DC. FrameRect (& lpdrawItemstruct-> rcitem, & brush);

} DC. Detach ();

} // Used to change the color of the color

Void CcolorButton :: ChangeColor (ColorRef color) {Cr

Ect RECT;

m-color = color;

GetClientRect (& RECT);

}

M-ColorChangecolor (ColorRef Color) Color, WM-DrawItem

The data member M-Color and to save the color of the button in the code. Changecolo

The R (ColorRef color) function is responsible for changing the color value of the button is color, then by making

The controlled client area is invalid and excited WM-DRAWITEM messages. Now this button control class

Calculate it. Let's add it to the dialog box to test (medium)

Auto-painted button control with VC

1. First create a single document apparatus through AppWizard.

2. Follow the startup AppStadio to create a dialog. Add a button control,

And set it to IDC-ColorButton. Finally, I have to remember to put the Push Button.

The OWNER DRAW check box in the Properties dialog box places the check mark. 3. Run CTestDialog class in AppStadio. Course

After adding data members in the CTestDialog class, add as follows in CTestDialog class

PRIVATE data member:

Private:

CColorButton M-ColorButton;

4. What is the remaining problem is how to make M-ColorButton's Drawi?

The TEM function can respond to the button controlled by the ID value of IDC-ColorButton control WM-D

Rawitem message. At this time, you will use the CWND class member function BOOL CWND :: Subcla

SSDLGITEM (Uint Nid, CWND * PParent). By calling this function, we

It can be dynamically take over the control generated from the dialog template and subordinate it to the CWND object.

That is, the current CWND object takes over the control of the ID number belonging to PParent to NID.

Cut the message. For button control, it controls the position and size of the current button.

The same as the button control corresponding to the NID. So we progress to the source file of CTestDialog

The following editing:

Bool ctestdialog :: oninitdialog ()

{CDialog :: oninitdialog ();

// Todo: Add Extra Initialization Here

M-ColorButton. Subclassdlgtem (idc-colorbutton, this)

; // Take over the message

M-ColorButton. ChangeColor (RGB (255, 0, 0); // Set to

Red (can be set to any color)

Return (TRUE);

}

Then add a response mouse to the CTestDialog by ClassWizzard.

The function of the DC-ColorButton button message:

Void ctestdialog :: oncolerbutton ()

{// Todo: Add Your Control NOTIFICATION HANDLER CODE

Here

INT r = int ((Float) Rand () / rand-max) * 255

INT g = int ((Float) Rand () / rand-max) * 255

INT b = int ((Float) Rand () / rand-max) * 255

M-ColorButton. ChangeColor (RGB (R, G, B));}

5. Finally, add a response Wm-lButtondo with ClassWizzard for View

WN function to activate the dialog. Please edit it as follows.

Void ctestview :: ONLBUTTON DOWN (Uint Nflags, Point Po

int)

{CTestDialog DLG;

DLG. Domodal ();

}

6. Compile and test the program. When the mouse is in the paid window, press the left mouse button.

A dialog is popped up. Press the left mouse button in the red rectangular area in the dialog box.

Its border becomes highlighted, if the left button is released in this area, this city area will change

Color and color is random.

Simply change the redraw code in the DrawItem function, you can get your own needs.

Site button. (Below) □ Sichuan Zeng Zhi

转载请注明原文地址:https://www.9cbs.com/read-3853.html

New Post(0)