MFC 布尔控件

头文件

#pragma once


// CJBoolControl

class CJBoolControl : public CButton
{
	DECLARE_DYNAMIC(CJBoolControl)

public:
	CJBoolControl();
	virtual ~CJBoolControl();
	void SetFasle();
	void SetTrue();
	bool Attach(const UINT nID,CWnd *nParant);
protected:
	DECLARE_MESSAGE_MAP()
	//virtual void DrawItem(LPDRAWITEMSTRUCT );
public:
	virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
private:
	COLORREF m_RedColor;
	COLORREF m_GreenColor;
	BOOL bstate;
	CBrush brush;
public:
	afx_msg void OnBnClicked();
};



源文件

// JBoolControl.cpp : 实现文件
//


#include "stdafx.h"
#include "BoolControl.h"
#include "JBoolControl.h"




// CJBoolControl


IMPLEMENT_DYNAMIC(CJBoolControl, CButton)


CJBoolControl::CJBoolControl()
{
	m_RedColor = RGB(255,0,0);
	m_GreenColor = RGB(0,255,0);
	bstate = FALSE;
	brush.CreateSolidBrush(m_GreenColor);
}


CJBoolControl::~CJBoolControl()
{
}




BEGIN_MESSAGE_MAP(CJBoolControl, CButton)
	ON_CONTROL_REFLECT(BN_CLICKED, &CJBoolControl::OnBnClicked)
END_MESSAGE_MAP()




bool CJBoolControl::Attach(const UINT nID,CWnd *pParant)
{
	if (SubclassDlgItem(nID,pParant))
	{
		return false;
	}
	else
		return true;
}




void CJBoolControl::DrawItem(LPDRAWITEMSTRUCT  lpDrawItemStruct)
{


	// TODO:  添加您的代码以绘制指定项
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
	VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);
	const int bufSize = 512;
	TCHAR buffer[215];
	GetWindowText(buffer, bufSize);
	USES_CONVERSION;
	int size=strlen(W2A(buffer));//得到长度
	SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);//透明
	dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//
	DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);
}




void CJBoolControl::OnBnClicked()
{
	// TODO: 在此添加控件通知处理程序代码
	bstate = !bstate;
	if (bstate)
	{
		brush.DeleteObject();
		brush.CreateSolidBrush(m_RedColor);
	}
	else	
	{
		brush.DeleteObject();
		brush.CreateSolidBrush(m_GreenColor);
	}
	this->Invalidate();
}
void CJBoolControl::SetFasle()
{
	if (!bstate)
	{
		brush.DeleteObject();
		brush.CreateSolidBrush(m_RedColor);
		this->Invalidate();
		bstate = !bstate;
	}


}
void CJBoolControl::SetTrue()
{
	if (bstate)
	{
		brush.DeleteObject();
		brush.CreateSolidBrush(m_GreenColor);
		this->Invalidate();
		bstate = !bstate;
	}
}

使用方法:


在 BOOL CBoolControlDlg::OnInitDialog()初始化

	GetDlgItem(IDC_BUTTON1)->ModifyStyle(0,BS_OWNERDRAW,0);
	m_button.Attach(IDC_BUTTON1,this);

 

实例代码:

https://download.csdn.net/download/u010261063/10294882


参考资料:

https://baike.so.com/doc/1494929-1580811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值