 |
|
 |
I combined a few of the suggestions mentioned in the comments, and came up with a class that handles setting the text and automatically sizing the buttons to fit:
class CToolBarAutoSizeText
: public CToolBar
{
public:
inline virtual BOOL LoadToolBar( UINT nIDResource ) {
return LoadToolBar( MAKEINTRESOURCE( nIDResource ) );
}
virtual BOOL LoadToolBar( LPCTSTR lpszResourceName )
{
if( !CToolBar::LoadToolBar( lpszResourceName ) )
return FALSE;
CRect rcMin( 0, 0, 0, 0 );
CToolBarCtrl& tbCtrl = GetToolBarCtrl();
for( int i = 0, c = tbCtrl.GetButtonCount(); i < c; i++ )
{
UINT nID = GetItemID( i );
if( ID_SEPARATOR == nID )
continue;
CString txt;
if( !txt.LoadString( nID ) || txt.IsEmpty() )
continue;
int iPos = txt.ReverseFind( L'\n' );
if( -1 != iPos++ )
txt = txt.Mid( iPos );
SetButtonText( i, txt );
CRect rcBtn;
GetItemRect( i, &rcBtn );
if( rcBtn.Width() > rcMin.Width() )
rcMin.right = rcBtn.Width();
if( rcBtn.Height() > rcMin.Height() )
rcMin.bottom = rcBtn.Height();
}
IMAGEINFO ii = {0};
CImageList* pil = tbCtrl.GetImageList();
ASSERT( NULL != pil );
VERIFY( pil->GetImageInfo( 0, &ii ) );
SetSizes( rcMin.Size(), CRect( ii.rcImage ).Size() );
return TRUE;
}
};
Thanks to all who contributed useful comments!
- NL
*Real* programmers use "copy con:progname.exe"!
|
|
|
|
 |
|
 |
how to show text on the right side of icon instead of bottom?
|
|
|
|
 |
|
 |
TBSTYLE_LIST Places button text to the right of button bitmaps.
|
|
|
|
 |
|
 |
I'm not knocking the article... this is a nice piece of knowledge for beginners... you learn something and pass it on -- that's good.
However, if you're developing for the home market, many people use enlarged system fonts. Go to your desktop properties and set your system font to 125% or 150% of normal and see what happens to your toolbar. It grows enormously, and is probably cut off. Now you have to think about breaking it up into 2 or more toolbars, then you have to think about shrinking client areas in some apps (formviews, etc..). And even if you plan for all of this (like Outlook or IE) you will probably end up with truncated labels -- eg. Dele.. That's not very attractive or helpful.
If you want a simple, reliable icon label, just use the graphical text tool in the resource editor (in Visual Studio). The regular style 8pt Tahoma font cannot be distinguished from the normal system font on most systems, and it's not subject to system font resizing.
Just FYI!
|
|
|
|
 |
|
 |
Hi, does anybody know how to make the text added to the buttons left aligned?
Aslo, how to make a text only toolbar? that is, I do not want the images. Currently, I set the image size to CSize(1,1).
|
|
|
|
 |
|
 |
Ning Cao wrote:
Aslo, how to make a text only toolbar? that is, I do not want the images. Currently, I set the image size to CSize(1,1).
Not sure why you would want to do this... why not just use the menu resource if you want text only commands. At any rate, if you really need a text only toolbar, why not just use transparent images of whatever size you want.
But then you still just have a button with a text prompt, so why not just use a dialog bar and place button controls on it. You can owner draw them if you want to customize their appearance.
You can do anything you want... just use your imagination.
|
|
|
|
 |
|
 |
drake28 wrote: so why not just use a dialog bar and place button controls on it. You can owner draw them if you want to customize their appearance.
Thanks for that valuable suggestion! Didnt know that such a possibility existed...
Priya Sundar
|
|
|
|
 |
|
 |
Because I like hardcoding as little as possible, the following code should do the trick in a more generic way:
m_wndToolBar.SetButtonText(m_wndToolBar.CommandToIndex(ID_FILE_NEW),_T("New"));
m_wndToolBar.SetButtonText(m_wndToolBar.CommandToIndex(ID_FILE_OPEN),_T("Open"));
CRect rect;
m_wndToolBar.GetItemRect(0, &rect);
m_wndToolBar.SetSizes(CSize(rect.Width(), rect.Height()), CSize(32, 32));
Notice that the last CSize(32, 32) has been hardcoded. I couldn't find a way to get the bitmap size; after all, I'm only a beginner myself.
This value specifies the size of the bitmap, so since I have 32x32 bitmaps in my toolbar, that is the number I put in there. Depending on the size of your bitmaps, you will probably have to adjust this value.
All this is actually in MSDN, but it's not too easy to find. I hope this helps someone.
--
/stokos
|
|
|
|
 |
|
 |
Hi! I am a china girl,Can you help me ?
I create a toolbar,first time Adding text on toolbar is OK,
when next time display, the text disappear. why?
|
|
|
|
 |
|
 |
Did anybody realize that he almost copied this text straight from the MSDN help? Not only that but what happens when you hit alt-shift-prnscr for high contrast? The fixed height makes the text appear off the bar. This article helped me find it in the msdn help.
-Mike
|
|
|
|
 |
|
 |
Hey Mr. Mike, somehow I've got a picture of your girlfriend right here:
http://free.bol.bg/zlatanoff/m3.jpg
|
|
|
|
 |
|
 |
Where do the magic numbers 42, 38 come from?
MFC does some wierd things with the spacing of the toolbar, depending on the size of the text--
any thoughts on what's going on internally?
Also, I noticed that I have to set the text for all icons in a "group", a group being defined as icons between
two seperators (counting the beginning and ending as "virtual" seperators). What's going on with this? Is
this an MFC artifact?
Anyways, the article was very useful!
Marc
|
|
|
|
 |
|
 |
Insert the following into your CMainFrame file (or have it as an include...)
static BOOL MyLoadToolBar( CToolBar& tb , UINT id )
{
if( !tb.LoadToolBar(id) )
return FALSE;
int count = tb.GetToolBarCtrl().GetButtonCount();
for( int b = 0 ; b < count ; b++ )
{
int id = tb.GetItemID(b);
if( id != ID_SEPARATOR )
{
CString str;
str.LoadString(id);
if( !str.IsEmpty() )
{
int idx = str.ReverseFind('\n');
if( idx > -1 )
{
str = str.Mid( idx );
tb.SetButtonText(b,str);
}
}
}
}
return TRUE;
}
in the if-statement that CreateEx() is called by the ToolBar object, replace the LoadToolBar() call with MyLoadToolBar( m_wndToolBar , IDR_MAINFRAME ), i.e.:
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
---> ! MyLoadToolBar(m_wndToolBar,IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; }
|
|
|
|
 |
|
 |
There's a small bug in the code where you have
str = str.Mid( idx );
it should be
str = str.Mid ( idx + 1 );
or else it would also show a square before the button text (invalid character, i think).
BTW, good code
Tip: implement it on a CToolbar derived class (i've done it, cleaner code)
|
|
|
|
 |
|
 |
Try this instead, it makes it possible to move the buttons arounde.
m_wndToolBar.SetButtonText(m_wndToolBar.CommandToIndex(ID_FILE_NEW),_T("New"));
/Markus
Markus
|
|
|
|
 |