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

The Ultimate Toolbox Static Text Control

By , 25 Aug 2007
 

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Source code and project files for this sample can be found in the samples\gui\StaticText directory of the sample projects download.

Overview

The COXStaticText class is a CStatic derived class. The normal look of this control is similar to a static control: it just sets a text string. However, unlike the MFC's CStatic, this class allows a user to give that text a special appearance.

Features

  • Change the text color and background color
  • Set and get the text font and attributes
  • Draw 3D text (embossed text)
  • Draw 3D text (offset in pixels)
  • Scroll text in any direction
  • Set and get scrolling speed (in pixels per second)
  • Draw text at an angle
  • Draw ellipses at the beginning, end or middle of a text string
  • Draw special borders: sunken, raised, flat line, dotted line
  • Multi-threaded, consumes very little processor time when scrolling
  • The appearance of the COXStaticText control can be changed on the fly; a user can set any control property at any moment of time, and the result of this modification will be visible immediately
  • Text scrolling is carried out by a separate worker thread
  • The user can tune the text scrolling smoothness in order to minimize processor consumption

Usage

Create a static control on your CDialog based resource and assign it an ID. Using the Class Wizard or Add Member Variable wizard, add a CStatic member to the dialog class. Next, include the header OXStaticText.h, and change the CStatic member in the AXF_DATA section to a COXStaticText object. You may want to set up some initial properties in your OnInitDialog:

// Set the Static Text window.

LPTSTR    psText = _T("Static Text");
int        /*nTextLeft = 10, nTextTop = 10,*/ nTextHeight = 30;
m_StaticText.SetWindowText(psText);
m_StaticText.SetTextColor(RGB(0, 0, 0));
m_StaticText.SetBkColor(RGB(192, 192, 192));

LOGFONT lf;
if(m_StaticText.GetLogFont(&lf))
{
    lf.lfHeight=nTextHeight;
    lf.lfWidth=0;
    lf.lfItalic=TRUE;
    lf.lfWeight=900;
    m_StaticText.SetLogFont(&lf);
}
m_StaticText.SetFontName(_T("Arial"));

Many other properties can be set as well, as outlined above. A complete class reference for the COXStaticText class can be found in the Graphical User Interface | Static Controls section of the compiled HTML help documentation.

History

Initial CodeProject release August 2007.

License

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

About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
Member
Organisation
263 members

In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.
 
Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.

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   
Questionsource and doc download not availablememberkyle.wu9 Mar '12 - 0:55 
except first exe file, all of other download zip file now are 0 byte.
GeneralDoesnt work in formviewmembermhorowit1 Oct '07 - 19:21 
This example doesnt work in a formview dialog.
 
if I add
CWnd *wnd;
wnd=this->GetDlgItem(IDC_OLDSTATIC);
wnd->GetClientRect(&rect);
m_StaticText.Create(psText,WS_CHILD|WS_VISIBLE|SS_SUNKEN | WS_BORDER| WS_EX_CLIENTEDGE,rect,this,IDC_OLDSTATIC);
 
It always displays the text at the top left corner of the dialog.
What am I missing to get the control to appear where the old one was?
GeneralRe: Doesnt work in formviewstaffTim Deveaux2 Oct '07 - 4:22 
Odd - I wonder what that rect contains.
 
But another approach would be to have the class wizard / member variable wizard set up a CStatic member, then change that to a COXStaticText. Create will be called for you - then in OnInitialUpdate of form view you can set things up:
 
	m_stat.SetTextColor(RGB(200,100,29));
	m_stat.SetWindowText(_T("Hello"),TRUE);
        // ... etc

GeneralRe: Doesnt work in formviewmembermhorowit2 Oct '07 - 9:26 
I actually tried what you suggested first.
 
If you use the class wizard to add a static member it is unitiailized in a formview,
it crash when you try and set attributes because the window handle is null.
 
The rect in my code has top=0,left=0, bottom = the static text controls height,right has static controls length.
 
I havent found now way to get the postion.
 

GeneralRe: Doesnt work in formviewstaffTim Deveaux2 Oct '07 - 11:18 
Where are you setting the attributes - I'm calling at this point in OnInitialUpdate, and things are ok:
 
void CTestFormStatView::OnInitialUpdate()
{
        CFormView::OnInitialUpdate();
        GetParentFrame()->RecalcLayout();
        ResizeParentToFit();
 
        // m_stat is a COXStaticText
        m_stat.SetTextColor(RGB(200,100,29));
        m_stat.SetWindowText(_T("Hello"),TRUE);
 
}

GeneralRe: Doesnt work in formview [modified]membermhorowit4 Oct '07 - 9:46 
same spot you are, it dies within a call to setDLgControlId
hWnd=0
 
which routine do you have the attachment to the control in for your test?
 
this code works fine at the same point in the code
wnd=this->GetDlgItem(IDC_MYSTATIC);
wnd->GetClientRect(&rect);
font=wnd->GetFont();
font->GetLogFont(&lf);
this->GetClientRect(&clrect);
 
lf.lfHeight*=(nVertRes/768.0)*0.7;
lf.lfWidth*=(nHorzRes/1024.0)*0.7;
fonts = new CFont();
VERIFY(fonts->CreateFontIndirect(&lf));
wnd->SetFont(fonts,false);
So it is possible to find the controls window.
 

 
-- modified at 15:55 Thursday 4th October, 2007
GeneralRe: Doesnt work in formview; fixed!!!!membermhorowit4 Oct '07 - 10:40 
I got it to work,
Formviews arent created with dynamic data exchange automatically
 
I needed to add the following to the form header
 
call myformview
{
.....
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
.....
public:
COXStaticText m_mystatic;
.....
};
 
in my code forms .cpp
 

void myformview::DoDataExchange(CDataExchange* pDX)
{
CWnd::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MYSTATIC, m_mystatic);
}
 
void myformview::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
m_mystatic.SetTextColor(RGB(0, 255, 0));
m_mystatic.SetBkColor(RGB(0, 192, 192));
m_mystatic.SetWindowText("Test Text",true);
 
}

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid