Praveen Katiyar' Blog

Learning along the way . . . .

Creating a status Bar in a Dialog Box ( VC++, MFC)

Normally in MFC Applications one can not create a status bar by default, this article shows you how status bar can be created in a dialog box.

Create a Dialog based application, using MFC AppWizard,

compile and make sure that every thing is working fine.

Open “resource.h”

define two identifiers in the file ”resource.h”, for two panes, these identifier will be used to identify two panes in the status bar you are going to create.

#define ID_INDICATOR_PANE             106
#define ID_INDICATOR_TIME             107

define an array of indicators in  DialogBox Source file, let say if you need to create two panes, you need  to define two values as shown below.

static UINT BASED_CODE indicators[] =
{
    ID_INDICATOR_PANE,
    ID_INDICATOR_TIME
};

Now make the necessary modifications in the Dialog Box’s InitDialog function.

BOOL CMyStatusBarDialogDlg::OnInitDialog()
{
//    rudimentary stuff
    CDialog::OnInitDialog();

// Set the icon for this dialog. 
    SetIcon(m_hIcon, TRUE);        // Set big icon
    SetIcon(m_hIcon, FALSE);       // Set small icon

// Here is what you need, to create a status bar
m_StatusBar.Create(this);     //Create status bar
m_StatusBar.SetIndicators(indicators,2);

//    Find the Size of Dialog box

CRect rect;                                       
GetClientRect(&rect);        

//    Size the two panes
m_StatusBar.SetPaneInfo(0,ID_INDICATOR_PANE, SBPS_NORMAL, rect.Width()-100);     
m_StatusBar.SetPaneInfo(1,ID_INDICATOR_TIME, SBPS_STRETCH ,0);

//    This is where we actually draw it RepositionBars( AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, ID_INDICATOR_TIME ) ;

    // Timer is Set to Update the Time on the status Bar.   
    SetTimer(100,1000,NULL);

    return TRUE;  // return TRUE  unless . . . .
}

Implement the Message WM_TIMER to update time

Implement the WM_TIMER message handler to update the current time on the pane of the status bar.

void CStatusBarDialogDlg::OnTimer(UINT nIDEvent)
{
    // TODO: Add your message handler code here 

    if ( nIDEvent==STATUS_TIMEER )
    {
        CTime t1 ;
        t1 = CTime::GetCurrentTime();
        m_StatusBar.SetPaneText(1,t1.Format("%H:%M:%S"));
    }
    CDialog::OnTimer(nIDEvent);
}

build and execute, and voila you are done.

Advertisements

June 29, 2013 - Posted by | CodeProject, GUI, MFC | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: