扩展Static控件

本文介绍了如何在VC++环境下扩展CStatic控件,以实现类似VB或.NET中静态控件的易设属性,如颜色、字体等。通过继承CStatic类并响应WM_CTLCOLOR消息,创建自定义的静态控件类,从而避免了手动为每个控件编写代码的繁琐,并能提供更加丰富的显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在VB或者.NET环境下,静态控件的颜色、字体大小等等属性都可以通过属性对话框很轻易的设定,但是在vc++环境下要实现同样的目的,必须自己手动编写代码实现,一种常用的做法是响应对话框的WM_CTRCOLOR消息。

HBRUSH CSettingDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{if (pWnd->GetDlgCtrlID()==IDC_LINE_STYLE)    
{
   pDC->SetTextColor(RGB(0,255,0));  

   //pDC->SetBkMode(TRANSPARENT);  

   pDC->SetBkColor(RGB(255,0,0));  
   return m_brush;               
}
return hbr;          
}


这样做也有其不方便的地方,需要对每一个控件设定,而且缺省的静态控件比较单调,所以自己视图扩展一下静态控件,于是通过继承CStatic类写了自己的一个扩展的静态类。

类定义文件:

#if !defined(AFX_STATICEX_H__436E57E9_F7E9_4728_8910_F22CDE1CE3D0__INCLUDED_)
#define AFX_STATICEX_H__436E57E9_F7E9_4728_8910_F22CDE1CE3D0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StaticEx.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CStaticEx window

class CStaticEx : public CStatic
{
// Construction
public:
	CStaticEx();

// Attributes
public:
	COLORREF m_BackColor;
	COLORREF m_TextColor;
	COLORREF m_FillColor;
	COLORREF m_BodeColor;
	COLORREF m_BodeLineColor;
	
	UINT FORMAT;
	CFont* pFont;
	CString szText;
	
	bool bTransparent;
// Operations
public:


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CStaticEx)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CStaticEx();

	// Generated message map functions
protected:
	//{{AFX_MSG(CStaticEx)
	afx_msg void OnPaint();
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

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

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STATICEX_H__436E57E9_F7E9_4728_8910_F22CDE1CE3D0__INCLUDED_)


类实现文件:

// StaticEx.cpp : implementation file
//

#include "stdafx.h"
#include "ExStatic.h"
#include "StaticEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStaticEx

CStaticEx::CStaticEx()
{
	m_BackColor=RGB(0,0,0);//缺省的文本背景色
	m_TextColor=RGB(255,255,255);//缺省的文本颜色
	m_FillColor=RGB(0,160,0);//缺省的子窗口填充颜色
	m_BodeColor=RGB(255,255,255);//缺省的边框颜色
	m_BodeLineColor=RGB(0,0,0);//缺省的边框线条颜色
	
	szText="static test";//缺省时显示的字符
	FORMAT=DT_RIGHT|DT_VCENTER|DT_SINGLELINE;//缺省的文本格式
	pFont=NULL;//缺省时为父窗口字体
    
	bTransparent=true;//缺省时文本背景透明  
}

CStaticEx::~CStaticEx()
{
}


BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
	//{{AFX_MSG_MAP(CStaticEx)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStaticEx message handlers

void CStaticEx::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	CRect rt;//控件矩形
	CRect datart;//文本显示矩形
	CPen* oldpen;
	CBrush* oldbrush;
	

	
	CPen* pen_bodeline=new CPen(PS_SOLID,1,m_BodeLineColor);
	CBrush* brush_bode=new CBrush(m_BodeColor);
	CBrush* brush_fill=new CBrush(m_FillColor);

	GetClientRect(&rt); 
	datart=CRect(rt.left+5,rt.top+5,rt.right-5,rt.bottom-5);

	CDC MemDC;//内存DC
	CBitmap MemBitmap;
	
	//绘制边框
	MemDC.CreateCompatibleDC(NULL); 
	MemBitmap.CreateCompatibleBitmap(&dc,rt.Width(),rt.Height()); 
	CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
	oldpen=MemDC.SelectObject(pen_bodeline);
 	oldbrush=MemDC.SelectObject(brush_bode); 
 	MemDC.Rectangle(rt);
 	MemDC.SelectObject(brush_fill);
 	MemDC.Rectangle(datart);
 	MemDC.MoveTo(rt.left,rt.top);
 	MemDC.LineTo(rt.left+5,rt.top+5);
 	MemDC.MoveTo(rt.right-1,rt.top);
 	MemDC.LineTo(rt.right-6,rt.top+5);
	MemDC.MoveTo(rt.left,rt.bottom-1);
 	MemDC.LineTo(rt.left+5,rt.bottom-6);
	MemDC.MoveTo(rt.right-1,rt.bottom-1);
 	MemDC.LineTo(rt.right-6,rt.bottom-6);

	//绘制文本
	MemDC.SetBkColor(m_BackColor);  //设置文本背景颜色 
	if(bTransparent)
		MemDC.SetBkMode(TRANSPARENT); 
	CFont *pOldFont; 
	if(pFont==NULL)
	{
		//pFont=new CFont();
		pFont=GetParent()->GetFont();
	}
	pOldFont=MemDC.SelectObject(pFont);//设置文本字体 
	MemDC.SetTextColor(m_TextColor);//设置文字颜色 
	datart.right-=5;
	MemDC.DrawText(szText,&datart,FORMAT); 

	dc.BitBlt(rt.left,rt.top,rt.Width(),rt.Height(),&MemDC,0,0,SRCCOPY); 
	
	//恢复缺省的dc
	MemDC.SelectObject(oldpen); 
	MemDC.SelectObject(oldbrush);
	MemDC.SelectObject(pOldFont);
	MemDC.SelectObject(pOldBit);
	MemBitmap.DeleteObject();
	MemDC.DeleteDC();

	//删除对象
	pen_bodeline->DeleteObject();
	delete pen_bodeline;
	brush_fill->DeleteObject();
	delete brush_fill;
	brush_bode->DeleteObject();
	delete brush_bode;
}


测试效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值