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.
BubbaiAsked:
Who is Participating?
 
Vinayak KumbarConnect With a Mentor Sr Program ManagerCommented:
Hi,

Do this

TEXTMETRIC txtmat;    
CClientDC dc(this);
dc.GetTextMetrics(&txtmat);

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.
VinExpert
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

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;
      dc.GetCharWidth(ch,ch,&Temp);
      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
str.MakeUpper();

//Set The Text
p_main->m_wndStatusBar.SetPaneText(1,str);

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.
VinExpert
0
All Courses

From novice to tech pro — start learning today.