Dynamic StatusBar pane...

I have status bar and I need to display various information on a pane.
I need to set the pane's size according to the displayed string's length.

How can I know which size to give to the pane in the "SetPaneInfo" function, so it will fit exactly to the displayed string's length.
Who is Participating?
Vinayak KumbarConnect With a Mentor Sr Program ManagerCommented:

Do this

TEXTMETRIC txtmat;    
CClientDC dc(this);

CString str("df5656");
int Len = str.GetLength();
Len *= txtmat.tmMaxCharWidth;
CMainFrame *p_main = (CMainFrame *)AfxGetMainWnd();
p_main->m_wndStatusBar.SetPaneInfo(1, IDS_MYPANE, SBPS_NORMAL, Len);

Here IDS_MYPANE is indicator ID, inserted at first place. Its length varias on the lenght of string. Then u can set that string to that pane

Hope this helps.
Vinayak KumbarSr Program ManagerCommented:

Here is One More Method.

//Get the DC
CClientDC dc(this);

//Have the string
CString str("xwiz");

//Get the length of string
int Len = str.GetLength();
int PaneWidth = 0;

//Calculate pane width from each char width
for(int Index = 0; Index < Len; Index++)
      char ch = str[Index];
      int Temp = 0;
      PaneWidth += Temp;
CMainFrame *p_main = (CMainFrame *)AfxGetMainWnd();

//Set The Width
p_main->m_wndStatusBar.SetPaneInfo(1, IDS_MYPANE, SBPS_NORMAL, PaneWidth);

//Make it Upper To Fit the Pane

//Set The Text

To achive from both the methods from any class other than CMainFrame, u may have to include mainfrm.h(if not included) and go to mainframe.h and declare m_wndStatusBar as public.

Try it out.
All Courses

From novice to tech pro — start learning today.