Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

Dynamic embossed text Progress control

Rate me:
Please Sign up or sign in to vote.
4.63/5 (8 votes)
5 Oct 20024 min read 74.2K   2.1K   35   6
A dynamic embossed text progress control that uses no resource file

Introduction

This is a gradient based, embossed progress control that uses an image dynamically generated from a string parameter.

How it was created

About a year ago I created a utility for work. Because of the lengthy operations involved while parsing through one or more files, I needed a progress control. Spending time on Code Project, I came across CSuperProgressCtrl created by Jean-Edouard Lachand-Robert. I liked the eye-catching embossing (taken directly from an article by Zafir Anjum entitled "Emboss text and other shape on your bitmap") of the progress control. Using a black and white bitmap resource in the project, it made for a great display.

While putting together a dialog for the progress control to reside in, I came across an article on Code Project by Chris Maunder. His article about CProgressWnd described how to have a progress dialog without having to have a resource. Replacing the normal progress control with the embossed version and modifying the CProgressWnd::SetWindowSize() function gave a nice result that allowed for a progress control with some text lines below it.

As much as I liked the control, it had the limitation of requiring a black and white bitmap resource so it could not be changed on the fly depending on the text. About the the time I was pondering this problem, one of my workmates suggested that I generate the bitmap using a text string rather than generating a bitmap resource file of the text to be compiled into the project. This solution would allow me to change the embossed text to whatever I wanted or needed. Working with that thought in mind, I removed the text display from the original CProgressWnd class and began looking for a place to hook in the bitmap creation. The only two functions that needed to be modified were

CProgressWnd::Create()
and CProgressWnd::SetWindowSize(). I also modified CSuperProgressCtrl to add the functions GetHeight() and GetWidth() to make the changes to CProgressWnd::SetWindowSize() possible.

I wanted to be able to control the font and size of the bitmap text. To accomplish this Create() was changed so that in addition to the parent window and the title, it takes a string to display, a font name, and a font height.

Code Details

All of the real code changes are in CProgressWnd::Create() and CProgressWnd::SetWindowSize(). I'll skip over the original code and concentrate only on the code changes that I've made to the class. Inside of Create(), I use a utility class to create a font of the desired type and size. The class CAutoFont class by Jamie Nordmeyer greatly simplifies the task. I chose to use a bold version of the font because I like the resulting display better. I then select the newly created font into a device context that I create for drawing.

// If no font was passed in, use the default.
csFontName = lpcFontName; 

if( lpcFontName && _tcslen(lpcFontName) )
    pFont = new CAutoFont( lpcFontName );
else
    pFont = new CAutoFont();

hMemDC = CreateCompatibleDC( NULL );
if( hMemDC )
{
    CDC* pDC = CDC::FromHandle( hMemDC );
    if( pDC )
    {
        pFont->SetBold( TRUE );

        // The font size.
        pFont->SetHeight( nFontHeight ); 
        
        // Select the font so that a size can be determined.
        pOldFont = pDC->SelectObject( pFont );   

        string_size = pDC->GetTextExtent( (LPCTSTR)csBitmapString, 
            csBitmapString.GetLength() );
        pOldFont = pDC->SelectObject( pOldFont );

        //...

Now that I have a font and a device context, it's time to place the text in a bitmap for the progress control to use. Using the CBitmapDC class created by Anneke Sicherer-Roetman, I create a bitmap in memory of the size calculated earlier with the call to GetTextExtent().

// Create a bitmap in memory using the string passed in.
CBitmapDC bitmapDC( string_size.cx, string_size.cy, pDC );
pOldFont = bitmapDC.SelectObject( pFont );
string_size = bitmapDC.GetTextExtent( (LPCTSTR)csBitmapString, 
                        csBitmapString.GetLength() );
bitmapDC.TextOut( 0, 0, (LPCTSTR)csBitmapString, 
                        csBitmapString.GetLength() );
m_pBitmap = bitmapDC.Close();

Now all that's left to do is supply the CSuperProgressCtrl::Create() call with a handle to the bitmap that was just created.

//...
		
bSuccess = m_wndProgress.Create(
    this,           // Parent window.
    TempRect.left,  // X Position.
    TempRect.right, // Y Position.
    (HBITMAP)(*m_pBitmap),
    IDC_PROGRESS    // ID
    );

pOldFont = bitmapDC.SelectObject( pOldFont );

ReleaseDC( pDC );
    }

    DeleteDC( hMemDC );
}		

Once the new embossed bitmap progress control is created, it must be sized and positioned. This is done in CProgressWnd::SetWindowSize(). Calling

m_wndProgress.GetWidth()
and m_wndProgress.GetHeight() makes the calculations easy. The code is commented and relatively clean so I won't comment on it.

Quick Sample Code

One thing that took me awhile to initially discover is that the CSuperProgressCtrl class must be registered before being used. To accomplish this, place the function call below somewhere in the programs initialization. In the sample, I place the call in OnInitialUpdate().

CSuperProgressCtrl::RegisterClass();

In the OnBtnRun() function, I create the progress bar passing in the text string csTemp for the bitmap and a 36 point font. The NULL causes the default font to be used.

pProgressWnd = new CProgressWnd( this, _T("Progress"), 
    (LPCTSTR)csTemp, NULL, 36 );		

A call to SetRange() sets the range. Inside of the loop a call to

SetPos()
updates the progress control. The PeekAndPump() call gives the progress control a chance to pick up any button clicks on the cancel button. By calling Cancelled(), the application can tell if the cancel button was pressed.

Conclusion

And that's really it. Whew! It was a quick and dirty thing using several classes I found here on Code Project, but it turned out pretty well. And during the course of writing this little article, I've gained an even greater appreciation for the effort that others put into publishing their articles.

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to capture the progress control? Pin
FlyingDancer20-Jul-05 3:32
FlyingDancer20-Jul-05 3:32 
GeneralInteresting but... Pin
Darren Schroeder6-Oct-02 7:13
Darren Schroeder6-Oct-02 7:13 
GeneralRe: Interesting but... Pin
John White6-Oct-02 7:30
John White6-Oct-02 7:30 
GeneralRe: Interesting but... Pin
John White6-Oct-02 7:51
John White6-Oct-02 7:51 
GeneralRe: Interesting but... Pin
Damonpeng15-Oct-07 4:36
Damonpeng15-Oct-07 4:36 
GeneralThis is truly wonderful Pin
Nish Nishant6-Oct-02 0:44
sitebuilderNish Nishant6-Oct-02 0:44 
Great control John, and thanks Rose | [Rose]

Regards,
Nish


Author of the romantic comedy

Summer Love and Some more Cricket [New Win]

Review by Shog9
Click here for review[NW]

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.