Click here to Skip to main content
Licence CPOL
First Posted 16 Oct 2003
Views 100,962
Bookmarked 63 times

XColorStatic - a colorizing static control

By | 16 Oct 2003 | Article
XColorStatic is a simple CStatic-based control that provides font changes, text and background colors, and icon display.

Introduction

XColorStatic is a general-purpose control to allow nice text display on a dialog. The demo shows you the kinds of text and icon display that are possible:

screenshot

XColorStatic API

Here is the complete list of CXColorStatic methods:

    void SetBackgroundColor(COLORREF rgb, BOOL bRedraw = TRUE);
    void SetTextColor(COLORREF rgb, BOOL bRedraw = TRUE);
    void SetBold(BOOL bFlag, BOOL bRedraw = TRUE);
    void SetFont(LPCTSTR lpszFaceName, int nPointSize, BOOL bRedraw = TRUE);
    void SetFont(LOGFONT *pLogFont, BOOL bRedraw = TRUE);
    void SetFont(CFont *pFont, BOOL bRedraw = TRUE);
    void SetIcon(HICON hIcon, BOOL bRedraw = TRUE);
    void SetMargins(int x, int y) { m_nXMargin = x; m_nYMargin = y; }

How To Use

To integrate XColorStatic into your app, you first need to add the following files to your project:

  • XColorStatic.cpp
  • XColorStatic.h
  • FontSize.cpp
  • FontSize.h

Then use the resource editor to add a static control to your dialog, and use Class Wizard to attach a member variable to that control. Note that when adding the static control, you must name it something other than IDC_STATIC.

Next, include the header file XColorStatic.h in the dialog's header file. Then replace the CStatic definition with CXColorStatic. Now you are ready to start using XColorStatic.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

Revision History

Version 1.0 - 2003 October 17

  • Initial public release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hans Dietrich

Software Developer (Senior)
Hans Dietrich Software
United States United States

Member

I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.







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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralScrollbar Improvement Pinmemberdarktrooper15:41 7 Jun '06  
kbomb987's scrollbar addition is a fine improvement to this control, but I wanted to be able to properly calculate the RECT of the text, based on font size, etc., existing in the control so I could accurately size the scrollbar. The net effect is the scrollbar will now scroll to the very end of the text, without any extra space.
 
Undoubtedly there could be further improvements or refinements to what I have done.
 
In XColorStatic.h, change the function declaration of ResetScrollBar to:
 
void ResetScrollBar(CRect* a_pMainRect, CRect* a_pWithTextRect);
 
Change the method definition in XColorStatic.cpp to be:
 
void CXColorStatic::ResetScrollBar(CRect* a_pMainRect, CRect* a_pWithTextRect)
{
// Need for scrollbars?
if(a_pMainRect->Height() > a_pWithTextRect->Height())
{
ShowScrollBar( SB_VERT, FALSE ); // Hide it
SetScrollPos( SB_VERT, 0 );
}
else
{
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nPage = a_pMainRect->Height();
si.nMax = a_pWithTextRect->Height();
si.nMin = 0 ;
 
SetScrollInfo(SB_VERT, &si);
 
EnableScrollBarCtrl( SB_VERT, TRUE );
}
}
 
In XColorStatic.cpp, replace OnPaint() with the following (I have included the previous conributions of David Fleming's tab handling and kbomb987's scrollbar functionality):
 
void CXColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
 
dc.SaveDC();
 
dc.SetTextColor(m_rgbText);
dc.SetBkColor(m_rgbBackground);
dc.SetBkMode(OPAQUE);
dc.SelectObject(m_pBrush);
 
CRect rect;
GetClientRect(rect);
 
// cannot have both an icon and text
 
if (m_hIcon)
{
int nIconX = ::GetSystemMetrics(SM_CXICON);
int nIconY = ::GetSystemMetrics(SM_CYICON);
 
rect.left = rect.left + (rect.Width() - nIconX) / 2;
rect.top = rect.top + (rect.Height() - nIconY) / 2;
 
dc.DrawIcon(rect.left, rect.top, m_hIcon);
}
else
{
dc.SelectObject(&m_font);
 
// get static's text
CString strText = _T("");
GetWindowText(strText);
 
UINT nFormat = 0;
DWORD dwStyle = GetStyle();
 
// set DrawText format from static style settings
if (dwStyle & SS_CENTER)
nFormat |= DT_CENTER;
else if (dwStyle & SS_LEFT)
nFormat |= DT_LEFT;
else if (dwStyle & SS_RIGHT)
nFormat |= DT_RIGHT;
 
if (dwStyle & SS_CENTERIMAGE) // vertical centering ==> single line only
nFormat |= DT_VCENTER | DT_SINGLELINE;
else
nFormat |= DT_WORDBREAK;
 
// Added to expand tabs...
if(strText.Find(_T('\t')) != -1)
nFormat |= DT_EXPANDTABS;
 
//
// BEGIN SCROLL CODE
//
// Create a rect above our target rect that will not allow drawing to.
// We will make 200 pixels above our target non-drawing. That should give us enough
// room to scroll text upwards.
RECT excluderect;
excluderect.top = rect.top - 200;
excluderect.bottom = rect.top;
excluderect.left = rect.left;
excluderect.right = rect.right;
dc.ExcludeClipRect(&excluderect);
 
// Draw the text accounting for any scrolling of the scroll bar.
rect.top -= GetScrollPos( SB_VERT );
 

//
// END SCROLL CODE
//
 
rect.left += m_nXMargin;
rect.top += m_nYMargin;
dc.DrawText(strText, rect, nFormat);
 

CRect rWithTextRect, rMainRect;
GetClientRect(&rMainRect);
rWithTextRect = rMainRect;

dc.DrawText(strText, &rWithTextRect, DT_CALCRECT | DT_WORDBREAK);
 
ResetScrollBar(&rMainRect, &rWithTextRect);
 
}

dc.RestoreDC(-1);
 
}
 

I hope this is a useful addition to this control.
 
Best regards to the Code Project Community,
 
-Lee
GeneralFull Transparent Background Pinmemberwlburgess11:54 19 Oct '05  
GeneralRe: Full Transparent Background PinmemberLeeJ.C.21:36 23 Oct '05  
GeneralRe: Full Transparent Background Pinmembercaliff_usa4:53 18 Jan '07  
GeneralNot displaying tabs correctly. PinmemberDavid Fleming23:50 1 Sep '05  
JokeRe: Not displaying tabs correctly. PinmemberDavid Fleming12:19 2 Sep '05  
GeneralCode to add Vertical Scroll Bars to this Control Pinmemberkbomb9878:01 28 May '05  
QuestionIs It A BUG ?? Pinmemberrenjith_sree19:31 1 Jul '04  
AnswerRe: Is It A BUG ?? PinmemberSean Moss-Pultz1:03 16 Jul '04  
GeneralRe: Is It A BUG ?? Pinmemberrenjith_sree1:29 16 Jul '04  
GeneralThat's wrong! PinmemberSB20039:42 13 Nov '10  
GeneralDebug assertion at non-modal dialogs Pinmemberfraenky21:59 6 Apr '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberHans Dietrich0:29 7 Apr '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberHans Dietrich17:16 7 Apr '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberGray Dragon11:01 6 May '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberHans Dietrich11:38 6 May '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberGray Dragon18:35 6 May '04  
GeneralRe: Debug assertion at non-modal dialogs PinmemberGray Dragon18:58 6 May '04  
GeneralRe: Debug assertion at non-modal dialogs Pinmemberwikiguyjd10:32 26 Jan '07  
QuestionBug? PinmemberHockey12:38 27 Nov '03  
AnswerRe: Bug? PinmemberHans Dietrich0:37 28 Nov '03  
GeneralDebug Assertion Failed Pinmemberandrewgs734:12 27 Nov '03  
GeneralRe: Debug Assertion Failed PinmemberHans Dietrich9:53 27 Nov '03  
GeneralRe: Debug Assertion Failed Pinmemberandrewgs7314:09 28 Nov '03  
GeneralNice But........ PinmemberAtif Mushtaq20:41 17 Oct '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 17 Oct 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid