Finally, I began to study VC with peace of mind. First of all, I had been troubled by the cbutton class of VC, which had no beautification function at all. I had to write my own code if I wanted to change the color font or something, envy yourself when you are working on VB. However, I have learned a lot about my exploration over the past two days. The following is a self-encapsulated button class with the foreground color, background color, Font, and background color and background image of the modified button text.
BB. h:
# If! Defined (afx_bb_h1_e2d72529_dda6_4cb2_b212_ab7319736d8e1_included _)
# Define afx_bb_h1_e2d72529_dda6_4cb2_b212_ab7319736d8e1_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 );
//} 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 ++ will insert additional declarations immediately before the previous line.
# Endif //! Defined (afx_bb_h1_e2d72529_dda6_4cb2_b212_ab7319736d8e1_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: drawitem (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 );
// Select the bitmap object to the memory device environment.
M_bitmap.getbitmap (& s_bmp); // retrieves 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), RGB (10, 0, 10 ));
DC. fillsolidrect (& rect, bkcolor); // here you 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 ){
This-> textcolor =;
This-> invalidate ();
}
Void BB: settextbkcolor (colorref ){
This-> textbkcolor =;
This-> invalidate ();
}
Void BB: setbkcolor (colorref ){
This-> bkcolor =;
Delete [] image;
Image = 0;
This-> invalidate ();
}
Void BB: setbkmode (int ){
This-> bkmode =;
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 to reload the drawitem function for button re-painting, and set the button as the owner for painting. The others are about the usage of DC, and you can get started with multiple connections at ordinary times. An example is provided:
Void cmy1dlg: onbutton1 ()
{
M. setfont (900, "");
M. setbkcolor (RGB (166,166, 66 ));
}
Void cmy1dlg: onbutton3 ()
{
M. setimage ("C: // inetpub // wwwroot // tri-rural areas // else // logo.bmp ");
M. setfont (400, "");
}
M is a BB object, and the image can only be in BMP format.
Okay. I hope you can correct the shortcomings in this article.