Multifunctional label CLabelEx Author: North China University of Querong Wen (querw)
Download Source Codes Introduction To use some static text controls, some static text controls are often used, showing some text information, but the functionality of the CSTATIC class provided by the MFC is too simple to meet the advanced needs. To this end, I gave a class Clabelex from CSTATIC to extend CSTATIC. For the first time, please forgive me. I have learned a lot from vckbase.com, which is when I returned. First, the new features of the function are:
1, set background picture setBGbitmap (); set the mouse after passing background picture setMouseOverBGbitmap (); set the mouse after the background image setClickedBGbitmap (); 2, set the label picture, setLabelbitmap (); set the label picture of the mouse Timelabitmap (); Set the label picture 3, text function when the mouse click
(1) Set the font color, underscore, etc., don't say it. (2) Automatically add the underline when the mouse is passed, automatically block the text (just like a hyperlink) 4, border and background can set / cancel the border, Specify the border color; set the background color and populate the entire label
Second, the implementation principle
1, in fact, it is in onpaint ():
Void Clabelex :: onpaint ()
{
CPAINTDC DC (this); // Device Context for Painting
Dc.SetTextColor (M_CRText);
Dc.setbkmode (transparent);
Dc.selectObject (this-> getFont ());
///Ready to work
CRECT RECT;
CDC MEMDC;
CPEN Borderpen, * Poldpen, underlinepenPenpen;
CBRUSH BGBRUSH, * POLDBRUSH;
Bitmap BM;
INT NTEXTLEFT = 0, nTextTop = 0; // Location of the text output
This-> getClientRect (& Re);
Memdc.createCompatibleDC (& DC);
MEMDC.SETMAPMODE (DC.getmapmode ());
/// Section Box
IF (m_bborder)
{
Borderpen.createpen (PS_SOLID, 1, M_CRBORDER);
BGbrush.createsolidbrush (m_crbg);
Poldpen = dc.selectObject (& borderpen);
Poldbrush = dc.selectObject (& bgbrush);
DC.Rectangle (& Re);
Dc.selectObject (Poldpen);
Dc.selectObject (Poldbrush);
Rect.deflateRect (1, 1);
}
/// Sticker background
IF (m_bclicked && m_clickedbgbm.getsafehandle ()! = null)
{
MEMDC.SELECTOBJECT (M_ClickedBGBM);
Dc.bitblt (Rect.Lip, Rect.width (), Rect.Height (),
& MEMDC, 0, 0, SRCCOPY;
}
Else IF (M_BOVER && M_MOUSEOverBGBM.GetsafeHandle ()! = null) // When the mouse passes
{
MEMDC.SELECTOBJECT (M_MouseoverBGBM);
Dc.bitblt (Rect.Lip, Rect.width (), Rect.Height (),
& MEMDC, 0, 0, SRCCPY;
Else IF (m_bgbm.getsafehandle ()! = null)
{
MEMDC.SELECTOBJECT (M_BGBM);
Dc.bitblt (Rect.Lip, Rect.width (), Rect.Height (),
& MEMDC, 0, 0, SRCCOPY;
}
/// stickers label 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.Height () * fscal), Rect.Height (), 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.Height () * fscal), Rect.Height (), 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.Height () * fscal), Rect.Height (), 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);
}
/// Painted 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: I didn't use the way to set up the font underline, because I feel that I don't look good, huh, huh.
2. The method of inductive mouse is as follows:
Setcapture () and releasecapture () in mousemove;
Void Clabelex :: OnMouseMove (uint nflags, cpoint point)
{
// Todo: add your message handler code here and / or call default
IF (m_bover) // Cursor Is Currently over Control
{
CRECT RECT;
GetClientRect (Rect);
IF (! Rect.ptinRect (Point))
{
m_bover = false;
IF (m_bautounderline) /// automatic underscore
{
This-> setunderline (False, RGB (0, 0));
}
IF (m_bhighlight) // automatically highlights
{
/// Restore the original font color
This-> setTextColor (M_CrBackText);
}
Redrawwindow ();
Releasecapture ();
Return;
}
}
Else // Cursor Has Just Moved over Control
{
m_bover = true;
IF (m_bautounderline)
{
This-> setunderline (True, RGB (0, 0, 255));
}
IF (m_bhighlight)
{
m_crbacktext = m_crtext;
This-> setTextColor (RGB (0,0,255));
}
Redrawwindow ();
SetCapture ();
:: setCursor (M_HHHANDCUR);
}
CSTATIC :: OnMousemove (nflags, point);
}
Note: This method is simple and convenient, but there is a problem, look at the project, click Label1 to pop up a dialog, Label1 can't restore the original. I have never solved this problem. If you know, please let me know querw@sina.com