Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

CxStatic: a CStatic control with text wrapping

Rate me:
Please Sign up or sign in to vote.
4.68/5 (15 votes)
17 Feb 20052 min read 127.5K   7.3K   59   27
A CStatic control with text wrapping, color, and transparency.

Sample Image

Introduction

Recently, I was looking for a good CStatic control that supports text wrapping (even with SS_CENTERIMAGE), font and color change, transparency... I had a look at CLabel by Norm.Net but was a bit disappointed because it was lacking text wrapping and was buggy (control is not refreshed after text change when transparent). First, I tried to find what was wrong, but finally I decided to write my own control.
This control is updated frequently so feel free to to drop in any time.

Using the code

How to add this control to your project:

  1. Copy files CxStatic.h, CxStatic.cpp and MemDC.h to your project directory, and add those files to your project.
  2. Put #include "CxStatic.h" in files where you plan to use this class.
  3. Change CStatic definitions to CxStatic.
  4. Use the API described below:
    void    SetBkColor(COLORREF rgb, 
            COLORREF crBkgndHigh = 0, BackFillMode mode = Normal);
    void    SetTransparent(BOOL bTranspMode);
    void    SetTextColor(COLORREF col);
    void    SetFont(const CString& strFont, int nPointSize, 
            int nWeight, BOOL bRedraw = TRUE);
    void    SetFont(LOGFONT *pLogFont, BOOL bRedraw /*= TRUE*/);
    void    Setfont(CFont *pFont, BOOL bRedraw /*= TRUE*/);
    void    SetFont3D(BOOL bFont3D, Type3D type);
    void    SetAutoAdjustFont(BOOL bAutoAdjustFont);
    
    BOOL    SetBitmap(HBITMAP hBitmap, ImageSize Emode = FitControl, <br>   COLORREF rgbTransparent =  0xFF000000);<br>BOOL    SetBitmap(UINT nIDResource, ImageSize Emode = FitControl,<br>   COLORREF rgbTransparent =  0xFF000000);// Loads bitmap from resource ID<br>BOOL    SetBitmap(LPCTSTR lpszResName, ImageSize Emode = FitControl,<br>   COLORREF rgbTransparent = 0xFF000000);// Loads bitmap from file
    
    void    SetWindowText(LPCTSTR strText);
    void    Format(LPCTSTR szFmt, ...);
    void     AppendText(LPCTSTR szFmt, ...);

Example of use

m_staGradient.SetTextColor( WHITE );
m_staGradient.SetFont("Arial", 16, FW_NORMAL);
// Choose font

m_staGradient.Format("Counter = %d", nCounter);
//like CString format method

m_staGradient.SetBkColor( RGB(255,240,150), RGB(124,0,0), 
  CxStatic::HGradient); // Create horizontal gradient


m_staBitmap.SetBitmap(IDB_BITMAP2, CxStatic::OriginalSize);

I hope someone will find this control useful!

History

CxStatic v0.1:

  • Initial release.

CxStatic v0.2:

  • Fix UpdateData bug (added WM_SETTEXT handling)
  • Now you can modify font by doing : cxstatic.SendMessage(WM_SETFONT,(WPARAM)fArial20, TRUE);
  • Renamed method SetFormat(...) to Format(...)
  • Changed some SetFont declarations

CxStatic v0.3:

  • Now control is owner-drawn
  • Fix SetBitmap bug (see above)
  • Add very simple multiline support
  • Add Setmoveable properties that enables to move and resize control at run-time

CxStatic v0.4:

  • Fix memory leak
  • Add Drag'nDrop Support for BMP files
  • Fix text alignment bug since 0.3
  • Modify methods signature(CString to LPCTSTR) and more Unicode compliant
  • Fixed bug when loading image from a file

TODO

  • Remove flickering when transparent
  • Add SetRounded to create a rounded rect around control
  • Add text scrolling
  • Add tooltip
  • Add contextual menu
  • Use CxImage to provide all image formats support (available in the next release 0.5)
  • Write a better article that explains how it works

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


Written By
Researcher
France France
I am currently working as software engineer in a french transport company.
I am interested in C++, MFC, wxWindows and .NET environnement.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Toseekin21-Feb-21 21:38
Toseekin21-Feb-21 21:38 
QuestionLicense terms of CStaticGradient Pin
Member 147604693-Mar-20 10:47
Member 147604693-Mar-20 10:47 
QuestionLicensing terms Pin
AnupTR25-Apr-13 10:21
AnupTR25-Apr-13 10:21 
Generalgood job Pin
batsword20-Aug-10 18:02
batsword20-Aug-10 18:02 
GeneralThanks a lot! Pin
_Marina_9-Feb-10 5:00
_Marina_9-Feb-10 5:00 
GeneralSetDlgItemText Error Pin
Hongjun Ge8-Aug-06 18:08
Hongjun Ge8-Aug-06 18:08 
GeneralCxStatic in wxWdigets Pin
seti6.8m29-Jan-06 23:15
seti6.8m29-Jan-06 23:15 
GeneralBig pictures - bug Pin
denis-cubic11-Jan-06 2:53
denis-cubic11-Jan-06 2:53 
GeneralPreSubclassWindow Pin
OddBod6-Nov-05 1:29
OddBod6-Nov-05 1:29 
GeneralRe: PreSubclassWindow Pin
Vince Ricci9-Feb-06 21:39
Vince Ricci9-Feb-06 21:39 
GeneralText position not good Pin
JPBOULEY6-Feb-05 0:37
JPBOULEY6-Feb-05 0:37 
GeneralRe: Text position not good Pin
Vincent Richomme8-Feb-05 3:30
Vincent Richomme8-Feb-05 3:30 
GeneralRe: Text position not good Pin
JPBOULEY9-Feb-05 4:01
JPBOULEY9-Feb-05 4:01 
Hi,
I have work a little to fix that and the main problem is on the GetStyle() function. According Microsoft documentation style retuned is not from the control but on the dialog. Because have use the CxStatic class for presonnal application I have find a quick fix by passing an additionnal psoition parameter. It is working but it's not "beautiful" and text position is not completely controlled.

Best regards


GeneralRe: Text position not good Pin
Vincent Richomme9-Feb-05 8:35
Vincent Richomme9-Feb-05 8:35 
GeneralUNICODE &amp; Remove Limits Pin
JimmyO26-Jan-05 22:32
JimmyO26-Jan-05 22:32 
GeneralGood job, but (another) Pin
PixiGreg4-Jan-05 5:46
PixiGreg4-Jan-05 5:46 
GeneralRe: Good job, but (another) Pin
Vincent Richomme7-Jan-05 2:15
Vincent Richomme7-Jan-05 2:15 
GeneralRe: Good job, but (another) Pin
PixiGreg7-Jan-05 8:43
PixiGreg7-Jan-05 8:43 
GeneralRe: Good job, but (another) Pin
Vincent Richomme7-Jan-05 11:39
Vincent Richomme7-Jan-05 11:39 
GeneralRe: Good job, but (another) Pin
Vincent Richomme8-Jan-05 0:42
Vincent Richomme8-Jan-05 0:42 
GeneralRe: Good job, but (another) Pin
PixiGreg8-Jan-05 1:32
PixiGreg8-Jan-05 1:32 
GeneralNice work but Pin
emadns21-Dec-04 22:18
emadns21-Dec-04 22:18 
GeneralRe: Nice work but Pin
Vincent Richomme21-Dec-04 23:37
Vincent Richomme21-Dec-04 23:37 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.