VC Multifunction Label Clabelex

Source: Internet
Author: User
Tags new features set background

Introduction

Do user interface often use some static text control, display some text information, but MFC provides the CStatic class function is too simple, unable to meet advanced requirements. For this reason I derived a class Clabelex from CStatic and extended the CStatic. First submission, the level is not enough please forgive me. I learned a lot from vckbase.com and it's time for me to pay back.

I. Brief introduction of function

The new features include:

1. Set the background picture Setbgbitmap (), set the background picture when the mouse passes Setmouseoverbgbitmap (), set the background picture after the mouse clicks Setclickedbgbitmap ();

2. Set the label picture, Setlabelbitmap (), set the label picture when the mouse passes Setmouseoverlabelbitmap (), set the label picture when the mouse clicks

3. Text function

(1) Set the font color, underline and so on do not say.

(2) Automatically add an underscore when the mouse is passed, and automatically turn the text blue (like a hyperlink)

4, Borders and backgrounds

You can set/remove borders, specify border colors, set background colors, and fill the entire label

Ii. Principle of realization

1, in fact, is in OnPaint () to draw a variety of effects:

void Clabelex::onpaint ()
{
CPAINTDC DC (this); Device context for painting
dc. SetTextColor (M_crtext);
dc. SetBkMode (Transparent);
dc. SelectObject (This->getfont ());
Preparatory work
CRect rect;
CDC MEMDC;
CPen Borderpen,*poldpen,underlinepen;
CBrush Bgbrush,*poldbrush;
BITMAP BM;
int ntextleft=0,ntexttop=0; Position of text output
  
This->getclientrect (&rect);
  
Memdc.createcompatibledc (&DC);
Memdc.setmapmode (DC. Getmapmode ());
  
Draw a border
if (M_bborder)
{
Borderpen.createpen (Ps_solid,1,m_crborder);
Bgbrush.createsolidbrush (M_CRBG);
    
Poldpen=dc. SelectObject (&borderpen);
Poldbrush=dc. SelectObject (&bgbrush);
    
dc. Rectangle (&rect);
    
dc. SelectObject (Poldpen);
dc. SelectObject (Poldbrush);
    
Rect. Deflaterect (1,1);
}
Paste background picture
if (m_bclicked && m_clickedbgbm.getsafehandle ()!=null)
{
Memdc.selectobject (M_CLICKEDBGBM);
dc. BitBlt (Rect.left,rect.top,rect. Width (), Rect. Height (),
&memdc,0,0,srccopy);
}
else if (M_bover && m_mouseoverbgbm.getsafehandle ()!=null)//When the mouse passes by
{
Memdc.selectobject (M_MOUSEOVERBGBM);
dc. BitBlt (Rect.left,rect.top,rect. Width (), Rect. Height (),
&memdc,0,0,srccopy);
}
else if (M_bgbm.getsafehandle ()!=null)
{
Memdc.selectobject (M_BGBM);
dc. BitBlt (Rect.left,rect.top,rect. Width (), Rect. Height (),
&memdc,0,0,srccopy);
}
Sticker picture
if (m_bclicked && m_clickedlabelbm.getsafehandle ()!=null)
{
M_clickedlabelbm.getbitmap (&BM);
Double fscal=bm.bmwidth*1.0/bm.bmheight;
Ntextleft=int (rect. Height () *fscal) +4;
Memdc.selectobject (M_CLICKEDLABELBM);
dc. StretchBlt (Rect.left,rect.top,int) (rect. Height () *fscal), Rect. Height (),
&memdc,0,0,bm.bmwidth,bm.bmheight,srccopy);
}
else if (M_bover && m_mouseoverlabelbm.getsafehandle ()!=null)
{
M_mouseoverlabelbm.getbitmap (&BM);
Double fscal=bm.bmwidth*1.0/bm.bmheight;
Ntextleft=int (rect. Height () *fscal) +4;
Memdc.selectobject (M_MOUSEOVERLABELBM);
dc. StretchBlt (Rect.left,rect.top,int) (rect. Height () *fscal), Rect. Height (),
&memdc,0,0,bm.bmwidth,bm.bmheight,srccopy);
}
else if (M_labelbm.getsafehandle ()!=null)
{
M_labelbm.getbitmap (&BM);
Double fscal=bm.bmwidth*1.0/bm.bmheight;
Ntextleft=int (rect. Height () *fscal) +4;
Memdc.selectobject (M_LABELBM);
dc. StretchBlt (Rect.left,rect.top,int) (rect. Height () *fscal), Rect. Height (),
&memdc,0,0,bm.bmwidth,bm.bmheight,srccopy);
}
Else
{
ntextleft=4;
}
Output text
Textmetric TM;
dc. GetTextMetrics (&TM);
CString StrText;
This->getwindowtext (StrText);
ntexttop=rect.top+ (rect. Height ()-tm.tmheight)/2;
if (Strtext.getlength () >0)
{
dc. TextOut (Ntextleft,ntexttop,strtext);
}
  
Draw Underline
if (m_bunderline)
{
ntextleft-=2;
ntexttop=ntexttop+tm.tmheight+1;
Underlinepen.createpen (Ps_solid,1,m_crunderline);
Poldpen=dc. SelectObject (&underlinepen);
dc. MoveTo (Ntextleft,ntexttop);
dc. LineTo (Ntextleft+tm.tmavecharwidth*strtext.getlength (), ntexttop);
}
}

Note: underline the font I didn't use the direct font underline method, because I think that is not good-looking, hehe

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.