mfc-self-painting control (Cradio article)

Source: Internet
Author: User
Tags addgroup drawtext set background textout

Sample diagram


I. Public Documents

Like this one, it's not written. (mfc-self-painting control (CButton article))

Second, Radio

Header file

#pragma once #include "Public.h" #include <vector>/////////////////////////////////////////////////////////// #define DEF_TEXT_FRAME_COLOR RGB (255,255,255)//default color #define DEF_TEXT_COLOR RGB (10,10,10)// Default color #define TOOLTIP_ID 100//Hint ID/////////////////////////////////////////////////////////////////////////
	/class Cpngradio:public CButton {Public:cpngradio ();

Virtual ~cpngradio ();
	Public://Initialize background picture, and select picture void Init (UINT nbkimg, UINT nckimg);
	Gets the currently selected state BOOL getchecked () {return m_bchecked;}
	Sets the selected state void setchecked (BOOL bchecked = TRUE);
	Set background color void Setbackcolor (Colorref crback) {m_crback = Crback;}
	Set font size and type void setfonttype (int fontsize, CString fonttype); Set Font color bool SetTextColor (COLORREF crtextcolor, colorref crtextframecolor = Def_text_frame_color, bool BShowFrame = Fals
	e);
Add all radio to a set of static void AddGroup (Std::vector<cpngradio*> vcradio); protected:virtual void DrawItem (lpdrawitemstRuct lpdrawitemstruct);
	virtual void Presubclasswindow ();
	Image *imagefromresource (hinstance hinstance, UINT uimgid, LPCTSTR lptype);
	void Drawtextstring (CDC * PDC, LPCTSTR pszstring, Colorref crtext, Colorref crframe, lprect lprect);
	void Drawtextstring (CDC * PDC, LPCTSTR pszstring, Colorref crtext, colorref crframe, int nxpos, int nypos);
	void Setgroup (std::vector<cpngradio*> vcradio);
void Paintparent ();
	Protected:bool m_bchecked;
	Image* M_imagecheck;
	Image* M_imageback;
	COLORREF M_crback;
	COLORREF M_crtextcolor;
	CFont M_font;
	BOOL M_bshowtextframe;
	COLORREF M_crtextframecolor;
	BOOL M_bsetback;

	Std::vector<cpngradio*> M_vcradio;
	afx_msg BOOL onclicked ();
	afx_msg hbrush Ctlcolor (cdc* PDC, UINT nCtlColor);

	afx_msg BOOL OnEraseBkgnd (cdc* PDC); Declare_message_map () declare_dynamic (Cpngradio)};

CPP file


#include "stdafx.h" #include "PngRadio.h" implement_dynamic (Cpngradio, CButton) begin_message_map (Cpngradio, CButton) On_wm_create () on_control_reflect_ex (bn_clicked, onclicked) on_wm_erasebkgnd () On_wm_ctlcolor_reflect () END_MESSAGE _map ()//////////////////////////////////////////////////////////////////////////CPngRadio::CPngRadio () {m_
	bchecked = FALSE;
	M_crtextcolor = RGB (0, 0, 0);
	M_crback = RGB (255, 255, 255);
	M_bsetback = FALSE;
	M_bshowtextframe = FALSE;
	M_crtextcolor = Def_text_color;
M_crtextframecolor = Def_text_frame_color;
	} cpngradio::~cpngradio () {} void Cpngradio::P Resubclasswindow () {modifystyle (0, Bs_ownerdraw);
CButton::P Resubclasswindow (); } void Cpngradio::init (UINT nbkimg, UINT nckimg) {M_imagecheck = Imagefromresource (AfxGetResourceHandle (), NCKIMG, L "PN
	G ");

	M_imageback = Imagefromresource (AfxGetResourceHandle (), nbkimg, L "PNG");
	if (M_imageback!= NULL) {m_bsetback = TRUE;
	} CRect Rcbutton; Rcbutton = CRect (0, 0, M_imagecheck->getwidth ()/4, m_imagecheck->getheight ()); SetWindowPos (NULL, 0, 0, rcbutton.width (), Rcbutton.height (), Swp_noactivate |
Swp_nomove);
	} hbrush Cpngradio::ctlcolor (cdc* PDC, UINT nctlcolor) {pdc->setbkmode (transparent);
Return (Hbrush) getstockobject (Null_brush);
	} void Cpngradio::setfonttype (int fontsize, CString fonttype) {logfont lf;
	memset (&AMP;LF, 0, sizeof (LOGFONT));
	Lf.lfheight = fontsize;
	_tcsncpy_s (Lf.lffacename, Lf_facesize, Fonttype, Fonttype.getlength ()); VERIFY (M_font.
CreateFontIndirect (&AMP;LF));
	} void Cpngradio::D rawitem (lpdrawitemstruct lpdis) {CDC dc; dc.
	Attach (LPDIS-&GT;HDC);

	CRect Rcitem (Lpdis->rcitem);
		if (m_bsetback) {Graphics graph (LPDIS-&GT;HDC); Graph. DrawImage (M_imageback, RECTF (0, 0, rcitem.width (), Rcitem.height ()), Rcitem.left, Rcitem.top, Rcitem.width (),
		Rcitem.height (), unitpixel); Graph.
	RELEASEHDC (LPDIS-&GT;HDC);
		} if (M_imagecheck!= NULL) {CRect Rcbutton;
		int imgwidth = M_imagecheck->getwidth ()/4; INT ImgHeight = M_imagecheck->getheight ();
		if (m_bchecked && iswindowenabled ()) Rcbutton = CRect (0, 0, ImgWidth, imgheight); else if (m_bchecked &&!
		IsWindowEnabled ()) Rcbutton = CRect (2 * imgwidth, 0, 3 * imgwidth, imgheight);
		else if (!m_bchecked && iswindowenabled ()) Rcbutton = CRect (imgwidth, 0, 2 * imgwidth, imgheight);

		else Rcbutton = CRect (3 * imgwidth, 0, 4 * imgwidth, imgheight); Graphics Graph (DC.
		GETSAFEHDC ()); Graph.
		DrawImage (m_imagecheck, 0, 0, Rcbutton.left, Rcbutton.top, Rcbutton.width (), Rcbutton.height (), unitpixel); Graph. RELEASEHDC (DC.
		GETSAFEHDC ());
		painting Fonts CString sztext;
		GetWindowText (Sztext); dc.
		SetBkMode (Transparent); cfont* Oldfont = DC. SelectObject (M_font. Getsafehandle ()?

		&m_font:getfont ());
		Drawtextstring (&AMP;DC, Sztext, M_crtextcolor, M_crtextframecolor, ImgWidth + 4, 0); dc.
	SelectObject (Oldfont); } DC.
Detach (); //Set color bool Cpngradio::settextcolor (COLORREF crtextcolor, COLORREF CrteXtframecolor, bool bshowframe) {m_crtextcolor = Crtextcolor;
	M_bshowtextframe = Bshowframe;

	M_crtextframecolor = Crtextframecolor;
	if (GetSafeHwnd ()) Invalidate (FALSE);
return true; Image * Cpngradio::imagefromresource (hinstance hinstance, UINT uimgid, LPCTSTR lptype) {hrsrc Hresinfo =:: FindResourc
	E (hinstance, Makeintresource (Uimgid), lptype); if (Hresinfo = null) return null;
	Fail DWORD dwsize; dwsize = Sizeofresource (hinstance, hresinfo);
	Get resource size (bytes) Hglobal hresdata;
	Hresdata =:: LoadResource (HINSTANCE, hresinfo); if (Hresdata = null) return null;
	Fail Hglobal Hmem;
	Hmem =:: GlobalAlloc (Gmem_moveable, dwsize);
		if (Hmem = = NULL) {:: FreeResource (Hresdata);
	return NULL;
	} lpvoid Lpresdata, Lpmem;
	Lpresdata =:: Lockresource (Hresdata);
	Lpmem =:: GlobalLock (HMEM); :: CopyMemory (Lpmem, Lpresdata, dwsize);
	Copy Memory:: GlobalUnlock (HMEM); :: FreeResource (Hresdata);
	Free memory IStream *pstream;
	HRESULT hr; hr =:: CrEatestreamonhglobal (Hmem, TRUE, &pstream);//create Stream object Image *pimage = NULL;
	if (SUCCEEDED (HR)) {pimage = Image::fromstream (pstream);//get GDI + pointer Pstream->release ();
	}:: GlobalFree (HMEM);
return pimage;
	BOOL cpngradio::onclicked () {setchecked ();
return FALSE; } void Cpngradio::setchecked (BOOL bchecked) {if (bchecked) {if (!m_vcradio.empty ()) {for (cpngradio* E:m_vcrad
			IO) {e->setchecked (FALSE);
	}} m_bchecked = bchecked;
	Paintparent ();
Invalidate (FALSE);
	BOOL cpngradio::onerasebkgnd (cdc* pDC) {return TRUE;} void Cpngradio::P aintparent () {CRect rect;
	GetWindowRect (&rect);
	GetParent ()->screentoclient (&rect);
GetParent ()->invalidaterect (&rect);
	}//Art font void Cpngradio::D rawtextstring (CDC * PDC, LPCTSTR pszstring, Colorref crtext, Colorref crframe, lprect lprect) {
	Variable definition int nstringlength = lstrlen (pszstring);
	int nxexcursion[8] = {1,1,1,0,-1,-1,-1,0}; int nyexcursion[8] = {-1,0,1,1,1,0,-1,-1};
	CRect Rcdraw;
		if (m_bshowtextframe) {//Painting Border Pdc->settextcolor (Crframe);
			for (int i = 0; i < sizeof (nxexcursion)/sizeof (nxexcursion[0)); ++i) {rcdraw.copyrect (lprect);
			Rcdraw.offsetrect (Nxexcursion[i], nyexcursion[i]); Pdc->drawtext (pszstring, Nstringlength, &rcdraw, Dt_vcenter | Dt_center | Dt_singleline |
		Dt_end_ellipsis);
	///Painting Font rcdraw.copyrect (lprect);
	Pdc->settextcolor (Crtext); Pdc->drawtext (pszstring, Nstringlength, &rcdraw, Dt_vcenter | Dt_center | Dt_singleline |
	Dt_end_ellipsis);
Return }//Art font void Cpngradio::D rawtextstring (CDC * PDC, LPCTSTR pszstring, Colorref crtext, colorref crframe, int nxpos, int n
		YPos) {if (Nypos = = 0) {LogFont lf; if (M_font. Getsafehandle ()) {M_font.
		Getlogfont (&AMP;LF);
		else {getfont ()->getlogfont (&AMP;LF);
	} Nypos = (M_imagecheck->getheight ()-ABS (Lf.lfheight))/2;
	}//variable definition int nstringlength = lstrlen (pszstring); int nxexcursion[8] = {1,1,1,0,-1,-1,-1,0};

	int nyexcursion[8] = { -1,0,1,1,1,0,-1,-1};
		if (m_bshowtextframe) {//Painting Border Pdc->settextcolor (Crframe); for (int i = 0; i < sizeof (nxexcursion)/sizeof (nxexcursion[0)); i++) {pdc->textout (Nxpos + nxexcursion[i], n
		YPos + nyexcursion[i], pszstring, nstringlength);
	///Painting Font pdc->settextcolor (Crtext);
	Pdc->textout (Nxpos, Nypos, pszstring, nstringlength);
Return } void Cpngradio::addgroup (Std::vector<cpngradio*> vcradio) {for (cpngradio* E:vcradio) {E->setgroup (vcRa
	DIO); } void Cpngradio::setgroup (Std::vector<cpngradio*> vcradio) {m_vcradio = Vcradio;}

Call method

	Cpngradio M_radio1;
	Cpngradio M_radio2;
	Cpngradio M_radio3;
	--------------------
	DDX_Control (PDX, Idc_radio1, m_radio1);
	DDX_Control (PDX, Idc_radio2, M_radio2);
	DDX_Control (PDX, Idc_radio3, M_radio3);
	--------------------
	M_radio1. Init (Idb_png_crback, idb_png_check);
	M_radio2. Init (Idb_png_crback, idb_png_check);
	M_radio3. Init (Idb_png_crback, idb_png_check);
	std::vector<cpngradio*> VC;
	Vc.push_back (&m_radio1);
	Vc.push_back (&m_radio2);
	Vc.push_back (&m_radio3);
	Cpngradio::addgroup (VC);
	M_radio1. Setchecked ();






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.