
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:
- Copy files CxStatic.h, CxStatic.cpp and MemDC.h to your project directory, and add those files to your project.
- Put
#include "CxStatic.h" in files where you plan to use this class.
- Change
CStatic definitions to CxStatic.
- 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 );
void Setfont(CFont *pFont, BOOL bRedraw );
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);
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);
m_staGradient.Format("Counter = %d", nCounter);
m_staGradient.SetBkColor( RGB(255,240,150), RGB(124,0,0),
CxStatic::HGradient);
m_staBitmap.SetBitmap(IDB_BITMAP2, CxStatic::OriginalSize);
I hope someone will find this control useful!
History
CxStatic v0.1:
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
I am currently working as software engineer in a french transport company.
I am interested in C++, MFC, wxWindows and .NET environnement.