现在的位置: 首页 > 综合 > 正文

关于vc中Button的颜色字体与背景图片的修改

2013年08月16日 ⁄ 综合 ⁄ 共 4471字 ⁄ 字号 评论关闭

     终于开始安心研究vc了,首先一直困扰我是关于vc里万恶的CButton类,一点美化的功能都没,想改个颜色字体什么的还要自己写代码,羡慕自己在弄vb的时候啊,呵呵。不过自己这两天的探索,也算学会了不少啊。下面是自己封装的button类,有改按钮文字的前景色,背景色,字体以及按钮本身的背景色和背景图片。

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++ will 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::DrawItem(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);     // 创建于内存设备环境相兼容的设备环境
     dcMem.SelectObject(&m_Bitmap);
     // 将位图对象选入到内存设备环境中
     m_Bitmap.GetBitmap(&s_Bmp);        // 获取位图信息
     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 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 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();
 
}

关键是对于按钮的重绘,要重载drawitem函数,还要将button设为所有者绘制。其他的都是对于DC的用法,平时多联系就能掌握了。给出个例子:

void CMy1Dlg::OnButton1()
{
  m.setfont(900,"黑体");
  m.setbkcolor(RGB(166,166,66));
}

void CMy1Dlg::OnButton3()
{
 m.setimage("C://Inetpub//wwwroot//三下乡//else//logo.bmp");
 m.setfont(400,"隶书");
}

m为bb对象,设置图片只能为bmp格式的。

 

        好了,文章写到这里,有什么不足之处还望大家多多指正。

【上篇】
【下篇】

抱歉!评论已关闭.