About the color of the button in VC font and background image changes

Source: Internet
Author: User

finally began to feel at ease to study VC, the first has been puzzling me is about the evils of VC CButton class, a little beautification of the function is not, want to change the color of the font of what also want to write their own code, envy himself in the time to get VB Ah, hehe. But my own these two days of exploration, also calculate learned a lot of AH. Below is the button class that you have encapsulated, the foreground color of the text changed, the background color, the font, and the context and background image of the button itself.

bb.h:

#if!defined (Afx_bb_h__e2d72529_dda6_4cb2_b212_ab7319736d8e__included_)
#define AFX_BB_H__E2D72529_DDA6_4CB2_B212_AB7319736D8E__INCLUDED_

#if _msc_ver > 1000
#pragma once
#endif//_msc_ver > 1000
Bb.h:header file
//

/////////////////////////////////////////////////////////////////////////////
bb window

Class Bb:public CButton
{
Construction
Public
BB ();

Attributes
Public
void SetTextColor (COLORREF);
void Settextbkcolor (COLORREF);
void SetBkColor (COLORREF);
void SetImage (char *i);
void SetBkMode (int m);
void SetFont (int f1,char *f2);
Operations
Public:

Overrides
ClassWizard generated virtual function overrides
{afx_virtual (BB)
Public
virtual void DrawItem (lpDrawItemStruct lpdrawitemstruct);
}}afx_virtual

Implementation
Public
Virtual ~bb ();

Generated message map functions
Protected
{afx_msg (BB)
afx_msg void OnPaint ();
}}afx_msg
COLORREF TextColor;
COLORREF Textbkcolor;
COLORREF Bkcolor;
Char *image;
int Bkmode;
Hbitmap M_hbitmap;
CFont *f;
Declare_message_map ()
};

/////////////////////////////////////////////////////////////////////////////

{{afx_insert_location}}}
Microsoft Visual C + + would insert additional declarations immediately before the previous line.

#endif//!defined (AFX_BB_H__E2D72529_DDA6_4CB2_B212_AB7319736D8E__INCLUDED_)

Bb.cpp:

Bb.cpp:implementation file
//

#include "stdafx.h"
#include "1.h"
#include "Bb.h"

#ifdef _DEBUG
#define NEW Debug_new
#undef This_file
static char this_file[] = __file__;
#endif

/////////////////////////////////////////////////////////////////////////////
BB

BB::BB ()
{
Textbkcolor=::getsyscolor (Color_3dface);
Textcolor=rgb (220,0,0);
Bkcolor=::getsyscolor (Color_3dface);
bkmode=1;
image=0;
F=new CFont;
}

BB::~BB ()
{
delete []image;
F->deleteobject ();
}


Begin_message_map (BB, CButton)
{Afx_msg_map (BB)
}}afx_msg_map
End_message_map ()

/////////////////////////////////////////////////////////////////////////////
BB message handlers

void BB::D rawitem (lpdrawitemstruct lpdrawitemstruct)
{

CDC DC;
CRect rect;
dc.   Attach (lpDrawItemStruct->hdc); Get the Button DC to CDC
rect=lpdrawitemstruct->rcitem;

Rect = lpdrawitemstruct->rcitem; Store the Button rect to our local rect.
Rect = lpdrawitemstruct-> rcitem;


CBitmap M_bitmap;
CDC Dcmem;
BITMAP s_bmp;
if (image) {
M_bitmap.attach (M_HBITMAP);
Dcmem.createcompatibledc (&DC); Created in a device environment compatible with the memory device environment
Dcmem.selectobject (&M_BITMAP);
To select a bitmap object into a memory device environment
M_bitmap.getbitmap (&s_bmp); Get bitmap Information
dc. StretchBlt (Rect.left,rect.top,rect. Width (), Rect. Height (),
&dcmem,0,0,s_bmp.bmwidth,s_bmp.bmheight,srccopy);
}else{
dc. Draw3drect (&rect,rgb (2,25,25), RGB (10,0,10));

dc. Fillsolidrect (&rect,bkcolor);//here can define the required color to appear on the Button.
}
UINT state=lpdrawitemstruct->itemstate; This defines the "state of the" Push button either pressed or not.

if (State & ods_selected)
{
dc. Drawedge (&rect,edge_sunken,bf_rect);

}
Else
{
dc. Drawedge (&rect,edge_raised,bf_rect);
}
dc. SetBkMode (Transparent);
dc.   SetBkColor (Textbkcolor); Setting the Text Background color
dc.     SetTextColor (TextColor); Setting the Text Color

TCHAR Buffer[max_path]; To store the Caption of the button.
ZeroMemory (Buffer,max_path); Intializing the buffer to zero
:: GetWindowText (Lpdrawitemstruct->hwnditem,buffer,max_path); Get the Caption of Button Window

dc. DrawText (buffer,&rect,dt_center| dt_vcenter| Dt_singleline);//redraw the Caption of Button Window

dc. Detach (); Detach the Button DC

}

void Bb::settextcolor (Colorref a) {
this->textcolor=a;
This->invalidate ();

}
void Bb::settextbkcolor (Colorref a) {
this->textbkcolor=a;
This->invalidate ();

}
void Bb::setbkcolor (Colorref a) {
this->bkcolor=a;
delete[] Image;
image=0;
This->invalidate ();

}
void Bb::setbkmode (int a) {
this->bkmode=a;
This->invalidate ();

}
void Bb::setimage (char *i) {
delete[] Image;
Image=new Char[strlen (i) +1];
:: strcpy (Image,i);
M_hbitmap= (HBITMAP):: LoadImage (null,_t (image), image_bitmap,0,0,lr_loadfromfile| Lr_createdibsection);
This->invalidate ();
}
void Bb::setfont (int f2,char *f1) {
if (f) {
Delete F;
}
F=new CFont;
F->createpointfont (F2,F1);
This->setfont (f);
This->invalidate ();

}

The key is for the redraw of the button, to overload the DrawItem function, and to set the button as owner drawing. The other is the use of DC, usually more contact can be mastered. Give an example:

void Cmy1dlg::onbutton1 ()
{
M.setfont (900, "blackbody");
M.setbkcolor (RGB (166,166,66));
}

void Cmy1dlg::onbutton3 ()
{
M.setimage ("c://inetpub//wwwroot//three to the countryside //else//logo.bmp");
M.setfont (400, "official script");
}

m is a BB object, setting the picture can only be BMP format.

        Well, the article is written here, there are some deficiencies also hope that everyone to correct me.

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.