Click here to Skip to main content
Click here to Skip to main content

Extended Use of CStatic Class - CLabel 1.6

By , 2 Aug 2002
 

Introduction

Ever wanted a static control that behaved like a Visual Basic label control? Well this could be just the thing you might be looking for. This version adds all the functionality of the label control, plus many other features. The control uses double buffering which gives fast painting. The real work in done in the OnPaint function.

I've called the class CLabel and its simple to use in dialog boxes, just follow this simple instructions.

  1. Design the dialog box in the normal way.
  2. Create an OnInitDialog function using the class wizard.
  3. Add Label.cpp and Label.h to your project.
  4. #Include <label.cpp> in your dialog .cpp file.
  5. Delete the project '.clw' file and regenerate by opening the project and pressing Ctrl+W
  6. Any static controls that need enhancing, give each control a unique ID within the dialog editor.
  7. Assign each member controls (CLabel) to each ID using the class wizard.
  8. In OnInitDialog uses the CLabelAPI to change the appearance of the control.

API of CLabel

 CLabel& SetBkColor(COLORREF crBkgnd, <br>
                            
          COLORREF crBkgndHigh = 0, <br>
                            
          BackFillMode mode = Normal);<br>
          
Set the background colour of the control and allows an optional gradient fill.
CLabel& SetText(const CString& strText) Sets the text of the controls
CLabel& SetTextColor(COLORREF crText) Sets the text colour of the control
CLabel& SetFontBold(BOOL bBold) Toggles the state of the bold attribute of the text in the control
CLabel& SetFontName(const CString& strFont) The the fonts face name in the control
CLabel& SetFontUnderline(BOOL bSet) Toggles the state of the underline font attribute of the control
CLabel& SetFontItalic(BOOL bSet) Toggles the state of the italic font attribute of the control
CLabel& SetFontSize(int nSize) Sets the fonts size in points.
CLabel& SetSunken(BOOL bSet) Toggles the state of the sunken attribue of the control
CLabel& SetBorder(BOOL bSet) Toggles the state of the borders attribute
CLabel& FlashText(BOOL bSet) Toggles the state of the text flashing attribute
CLabel& FlashBackground(BOOL bSet) Toggles the state of the text flashing attribute
CLabel& SetLink(BOOL bLink) Toggles the state of the link attribute (allows label to become internet link)
CLabel& SetLinkCursor(HCURSOR hCursor) Sets the cursor for the link.
CLabel& SetTransparency(BOOL bTransparency) Sets the transparency for the background.
CLabel& SetFont3D(BOOL bSet,Type3D type) Sets 3D fonts
CLabel& SetRotationAngle(UINT nAngle,BOOL bRotation) Sets rotation angle for font
CLabel& SetText3DHiliteColor(COLORREF cr3DHiliteColor) Sets the hi-light colour for the text
CLabel& SetMailLink(BOOL bEnable, BOOL bNotifyParent) Sets the hi-light colour for the text

Example of use

 m_fname.SetFontName("System");
 m_fsize.SetFontSize(14);
 m_uline.SetFontUnderline(TRUE);
 m_tcolor.SetTextColor(RGB(255,0,0));
 m_bcolor.SetBkColor(RGB(0,255,255));
 m_italics.SetFontItalic(TRUE);
 m_bold.SetFontBold(TRUE);
 m_border.SetBorder(TRUE);
 m_sunken.SetSunken(TRUE);
 m_lblGradient.SetBkColor(RGB(255,255,255),RGB(0,0,255), CLabel::Gradient);

 m_monty.SetFontName("Arial");
 m_monty.SetFontSize(12);
 m_monty.SetTextColor(RGB(255,255,0));
 m_monty.SetFontUnderline(TRUE);
 m_monty.SetBkColor(RGB(0,0,0));
 m_monty.SetFontItalic(TRUE);
 m_monty.SetFontBold(TRUE);
 m_monty.SetBorder(TRUE);
 m_monty.SetSunken(TRUE);
 

History

CLabel Version 1.6

  • Jeroen Roosendaal - SetFont suggestion
  • Laurent - Spotting SelectObject bugs
  • Bernie - Fix PreCreateWindow bug
  • Jignesh I. Patel - Added expanded tabs feature
  • Jim Farmelant - Fix SetText crash

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Norm .net
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom
Member



Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.

We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net
 
Visit Software Kinetics

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to Auto Size text in CLabel [modified]membermasteryoda2127 Oct '11 - 10:33 
I am trying to auto size the text in CLabel so the text always fills the label.   I tried the code below, but dc.DrawText() always returns the same size rectangle.
 
void CDebateTimerDlg::AutoSizeFont(void)
{
     CPaintDC dc(&m_Label); // device context for painting
     CString text = _T("Sample Text");
     CRect rect, rc;
     m_Label.GetClientRect(&rect);
     rc = rect;
     bool bLeft     = false;
     bool bRight = false;
     do
     {
          if (rc.right < rect.right)
               m_font.nSize = int(m_font.nSize * 1.1);
          else
               if (rc.right > rect.right)
                    m_font.nSize = int(m_font.nSize * 0.9);
          rc = rect;
          m_Label.SetFontSize(m_font.nSize);
          dc.DrawText(text, text.GetLength(), &rc, DT_SINGLELINE | DT_CALCRECT); // rc never changes.
          bLeft     = int(0.9 * rect.right) <= rc.right;
          bRight = rc.right <= int(1.1 * rc.right);
     } while (!(bLeft && bRight));
}
 
====================
 
Any suggestions?   This code is suppose to maximize the font to between 90 - 110% of the label width.

-- modified 9 Dec '11 - 17:03.
GeneralFlick Problem On Redraw - Solution [modified]membertonim9 Jun '10 - 11:48 
This CLabel class is quite old but still has a great use!
I had use CLabel extensively from some time in a bingo game for BedOS and other MFC applications. I however have several remarks about it, I hope this to reach the author.
 
1. WM_SETTEXT not overrided
Sometimes there is a thread or DLL or process whick communicate with WM_SETTEXT message, not always text can be set with SetText() member function. But I think that OnSetText() is not overrided and UpdateSurface() is not call in this version (1.6)....
 
2. I had placed a call with WM_SETTEXT to a transparent label in a timer. When it has to be redrawn frequently I saw that there is a flick problem because in OnSetText() there is a call to Default() function which redraws client rectangle in the nasty default control color first. This can be solved with commenting Default() in OnSetText(). But this will lead to another problem - text is not going to be changed with SetWindowText() function because this is also in Default()....
This can be solved as follow:
- in Label.h add m_Text var:
CString m_Text ;
- in PreSubclassWindow() add:
GetWindowText(m_Text);
- at the top of OnPaint() replace
GetWindowText(strText);
with
strText = m_Text ;
- add OnSetText() override:
ON_MESSAGE(WM_SETTEXT, OnSetText)
- replace content of OnSetText() with:
void CLabel::OnSetText(WPARAM wParam, LPARAM lParam)
{
// Default();
TCHAR* strText = (TCHAR*)lParam ;
if(IsWindow(this->GetSafeHwnd()))
{
m_Text = strText ;
UpdateSurface();
}
}
-replace content of SetText() with:
CLabel::SetText(const CString& strText)
{
if(IsWindow(this->GetSafeHwnd()))
{;
if(m_Text != strText)
{
m_Text = strText ;
SetWindowText(strText);
//UpdateSurface();
}
}
 
return *this;
}
(otherwise UpdateSurface() is going to be called twice!
 
Hope that this helps!
tonim
 

modified on Wednesday, June 9, 2010 5:56 PM

GeneralRe: Flick Problem On Redraw - SolutiongroupNorm .net9 Jun '10 - 21:21 
Glad there's still somebody coding in MFC!
 
I've moved to .net 10 years ago, and won't be doing any further work in this article. Please modify the code.,
Two heads are better than one.

GeneralQuestionmemberguyuewuhua31 Aug '09 - 16:29 
add mouseover and mouseleave method,some suggestion?
Generallicense restrictions for CLabel classmemberclintM22 Apr '09 - 7:24 
Thanks for the CLabel class. There are no license restrictions listed on Code Project, regarding this class. Are there license restrictions?
 
Thanks.
 
Clint
GeneralRe: license restrictions for CLabel classmemberNorm .net22 Apr '09 - 20:22 
No restrictions, use anywhere it's completely free without ties.
 


Software Kinetics
- Moving software


GeneralRe: license restrictions for CLabel classmemberclintM23 Apr '09 - 2:32 
Thanks again.
GeneralChanging font size [modified]membersabapathy_8021 Jan '09 - 3:59 
Hi! A Very useful article!!
 
I am doing DDX_Control with RichEdit control in my form view to the member variable of your CLabel.
Everything is working fine except SetFontSize() for me.
I am displaying link using SetText() and SetHyperLink() functions.
How to change the caption's font size(which is set using SetText()) displayed in control?
Any suggestion will be very helpful..
 
Thanks..
 
modified on Wednesday, January 21, 2009 10:41 AM

GeneralFlashing problems ( transparent label over bitmap )memberalexuh13 Aug '08 - 4:21 
I had some flashing problems on transparent label redraw when they are positioned over a bitmap drawned dynamically. I fixed the problem with the following changes.
 
CLabel& CLabel::SetText(const CString& strText)
{
if(IsWindow(this->GetSafeHwnd()))
{
SetRedraw(FALSE);
SetWindowText(strText);
SetRedraw(TRUE);
UpdateSurface();
}
return *this;
}
 
void CLabel::UpdateSurface()
{
CRect (rc);
GetWindowRect(rc);
//RedrawWindow();
 
GetParent()->ScreenToClient(rc);
GetParent()->InvalidateRect(rc,TRUE);
GetParent()->UpdateWindow();
}
 
Thanks for your good class
QuestionMaybe it's better that set byCharSet=DEFAULT_CHARSET in SetFontName?membersodar22 May '08 - 17:57 
Thanks very much for the code!
 
By default, set byCharSet=ANSI_CHARSET, the font doesn't work in some languages such as Simplified Chinese.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 3 Aug 2002
Article Copyright 2000 by Norm .net
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid