Implementation of the Custom button

Source: Internet
Author: User
Code
General VC controls are gray.ProgramWhen the interface is beautified, the use of general controls does not match the beautifying program interface. In VB, C #, you can easily change the background color of the control, but it is not possible in VC. We need to improve this function by ourselves. Here is a brief introduction. If you are interested, you can go to http: // Search and view related content in www.vbkbase.com (VC knowledge base.

Create a dialog box project in vc60 and add a new class. Select the corresponding base class based on the control you want to beautify. to beautify the static control, select cstatic as the base class, the new class is cstaticex.

Add two class member variables to the new class. One is the control background color and the other is the text color:

Private:

Colorref m_backcolor;

Colorref m_textcolor;

Initialize the two variables in the constructor. I initialize them to black and white characters.

M_backcolor=RGB (0,0,0);

M_textcolor=RGB (255,255,255);

Add the wm_paint message to cstaticex, and add the beautification control to the onpaint message processing function of wm_paint.Code

{

Cpaintdc DC (This);//Device context for painting

CDC memdc;

Cbrush m_brush,*M_oldbrush;

Cbitmap bitmap;

Crect rect;

Cstring strwindowtext;

IntX, Y;

Csize size;

//Obtain Region

Getclientrect (&Rect );

//Get Text

This->Getwindowtext (strwindowtext );

//Create compatible memory bitmap handle

Memdc. createcompatibledc (&DC );

Bitmap. createcompatiblebitmap (&DC, rect. Width (), rect. Height ());

Memdc. SelectObject (&Bitmap );

//Get Text Size

Size=Memdc. gettextextent (strwindowtext );

//Create a paint brush and draw a background color

M_brush.createsolidbrush (m_backcolor );

M_oldbrush=Memdc. SelectObject (&M_brush );

Memdc. fillrect (&Rect,&M_brush );

Memdc. SelectObject (m_oldbrush );

M_brush.deleteobject ();

//Text

Y=Rect. Top;

X=Rect. Right/2-Size. CX/2;

Memdc. setbkcolor (m_backcolor );

Memdc. settextcolor (m_textcolor );

Memdc. textout (X, Y, strwindowtext );

//Drawing

DC. bitblt (rect. Left, rect. Top, rect. Width (), rect. Height (),&Memdc, rect. Left, rect. Top, srccopy );

}

Add a static control named idc_statictest in the dialog box and add a member variable cstatic m_statictest to it;

Add the header file of the cstaticex class to the header file of the dialog box class;

Set cstatic m_statictest

Change to cstaticex m_statictest;

Compile and run the command to see the effect!

ArticleSource: http://Www.diybl.com/course/3_program/vc/vc_js/200878/132216.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.