Generally, the toolbar button is a small bitmap that does not display text. Note that each button ID has a corresponding string resource in the format of xxxxxx/nxxxx. The second string is short and generally used for tips display. That is, when the mouse stays on the button, a small Prompt window is displayed. This text can be displayed together with the text of the toolbar button and the bitmap. The setbuttontext function is used to add text to a button. The definition is as follows:
Bool setbuttontext (INT nindex, lpctstr lpsztext );
Nindex indicates the serial number of the button and lpsztext indicates the text of the button. If the size of the toolbar button is large enough, text is usually displayed below the icon after the icon is displayed, such as "open" and "save. After the toolbar is generated in the oncreate function of the main framework, use the setbuttontext function to add text to all buttons:
Int cmainframe: oncreate (maid)
{
......
M_wndtoolbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndtoolbar );
//------------------------------------------------------------------
// First record the button size without text
// Csize notextbuttonsize; defined in the header file
Crect rect;
M_wndtoolbar.getitemrect (0, & rect );
Notextbuttonsize. Cx = rect. Width ();
Notextbuttonsize. Cy = rect. Height ();
// Set toolbar button text
For (INT I = 0; I <m_wndtoolbar.getcount (); I ++)
{
Cstring strtext;
Strtext. loadstring (m_wndtoolbar.getitemid (I ));
Int J = strtext. Find ("/N ");
If (j> = 0)
{
Cstring strbutton = strtext. Right (strtext. getlength ()-J-1 );
M_wndtoolbar.setbuttontext (I, strbutton );
}
Else
M_wndtoolbar.setbuttontext (I ,"");
}
// Calculate the size of the button with text. The size of the button with the longest text:
// Csize textbuttonsize; defined in the header file
Textbuttonsize. Cx = textbuttonsize. Cy = 0;
For (I = m_wndtoolbar.gettoolbarctrl (). getbuttoncount (); I> = 0; I --)
{
M_wndtoolbar.getitemrect (I, & rect );
Rect. normalizerect ();
Textbuttonsize. Cx = _ max (rect. Size (). CX, textbuttonsize. CX );
Textbuttonsize. Cy = _ max (rect. Size (). Cy, textbuttonsize. Cy );
}
// M_beshowtext is a bool variable that specifies whether the toolbar button displays text and can be saved in the INI file.
// For example, read in the constructor:
// M_beshowtext = afxgetapp ()-> getprofileint ("setting", "toolbartext", 1 );
// Save it in the Destructor:
// Afxgetapp ()-> writeprofileint ("setting", "toolbartext", m_beshowtext );
Modifytoolbar (m_beshowtext); // adjust the button size
Return 0;
}
// Modifytoolbar is a custom function used to control whether the toolbar button displays text.
Void cmainframe: modifytoolbar (bool beshowtext)
{
Csize sizebutton, sizeimage;
// Set the button size. The value can be adjusted based on the actual situation.
Sizebutton. Cx = beshowtext? Textbuttonsize. CX: notextbuttonsize. CX;
Sizebutton. Cy = beshowtext? Textbuttonsize. Cy: notextbuttonsize. Cy;
Sizeimage. Cx = 16; // Icon size
Sizeimage. Cy = 15;
M_wndtoolbar.setsizes (sizebutton, sizeimage );
Recalclayout ();
}
What if I want to set multi-line text display or display text on the right of the image? Remember text can also be made into a part of the image, so you only need to combine the text to be displayed with the image, and then set it to a button image. If you want to implement it using code, you can dynamically generate images by yourself. For example, the following code implements multiple lines of text:
// Display the text of multiple Toolbar
Bool cmttoolbar: showtext (uint nidresource)
{
//--------------------------------------------------------
// Step 1: Get the image size of a single button
Hinstance hinst = afxfindresourcehandle (makeintresource (nidresource), rt_toolbar );
Hrsrc =: findresource (hinst, makeintresource (nidresource), rt_toolbar );
If (hrsrc = NULL)
Return false;
Hglobal = loadresource (hinst, hrsrc );
If (hglobal = NULL)
Return false;
Ctoolbardata * pdata = (ctoolbardata *) lockresource (hglobal );
If (pdata = NULL)
Return false;
Assert (pdata-> wversion = 1 );
Csize sizeimage (pdata-> wwidth, pdata-> wheight );
Unlockresource (hglobal );
Freeresource (hglobal );
//--------------------------------------------------------
// Step 2: calculate the maximum number of lines of text based on the toolbar window DC, and then calculate the height of the toolbar button to be adjusted
Ctoolbarctrl & bar = gettoolbarctrl ();
CDC * pdcctrl = bar. getdc ();
CDC dcdst; // Target DC, used to generate a new bitmap
Dcdst. createcompatibledc (pdcctrl );
// Create a font
Logfont;
Zeromemory (& logfont, sizeof (logfont ));
Logfont. lfwidth = 6;
Logfont. lfheight = 12;
Logfont. lfcharset = gb2312_charset;
Strcpy (logfont. lffacename, "");
Cfont fntnew;
Fntnew. createfontindirect (& logfont );
Cfont * pfntold = dcdst. SelectObject (& fntnew );
// The image size of the new button
Csize sizenewimage (sizeimage. CX, 0 );
// Create a String Array
Const int ncount = bar. getbuttoncount ();
Cstringarray * pstrarray = new cstringarray [ncount];
Int nlines = 0; // number of lines in the text
Int nindex = 0;
Int ncharheight = 0; // the height of a single character
Tbbutton TB;
Int nbtncount = 0; // Number of buttons (excluding separators)
For (INT I = 0; I <ncount; ++ I)
{
Zeromemory (& TB, sizeof (tbbutton ));
Bar. getbutton (I, & TB );
// If it is a separator
If (Tb. fsstyle & tbstyle_sep) = tbstyle_sep)
{
Continue;
}
Cstring strt;
Strt. loadstring (Tb. idcommand );
Int NPOs = strt. Find (_ T ('/N') + 1;
While (NPOs> 0)
{
Int npos2 = strt. Find (_ T ('/N'), NPOs );
Int nindex;
If (npos2> NPOs)
{
Nindex = pstrarray [nbtncount]. Add (strt. mid (NPOs, nPos2-nPos ));
NPOs = npos2 + 1;
}
Else if (strt. getlength ()> NPOs)
{
Nindex = pstrarray [nbtncount]. Add (strt. mid (NPOs ));
NPOs =-1;
}
Nlines = max (nlines, nindex + 1 );
Csize size = dcdst. gettextextent (pstrarray [nbtncount] [nindex]);
Ncharheight = max (size. Cy, ncharheight );
Sizenewimage. Cx = max (size. CX, sizenewimage. CX );
}
Nbtncount ++;
}
// Convert to actual pixels
Sizenewimage. Cy = nlines * ncharheight;
//--------------------------------------------------------
// Step 3: dynamically create a toolbar button bitmap and replace the original imagelist
// Read the toolbar bitmap Resource
Cbitmap BMP toolbar;
Bitmap bmbitmap;
If (! BMP toolbar. Attach (LoadImage (AfxGetInstanceHandle (),
Makeintresource (nidresource), image_bitmap, 0, 0,
Lr_defaultsize | lr_createdibsection | lr_loadmap3dcolors) |
! BMP toolbar. getbitmap (& bmbitmap ))
Return false;
// Obtain the total width and height of the bitmap.
Int nwidth = bmbitmap. bmwidth;
Int nheight = bmbitmap. bmheight;
// The total width and height of the new bitmap
Int nwidthnew = sizenewimage. CX * nbtncount;
Sizenewimage. Cy + = nheight;
Int nheightnew = sizenewimage. Cy;
CDC dcsrc; // source DC
Dcsrc. createcompatibledc (pdcctrl );
Cbitmap * pbmpoldsrc = dcsrc. SelectObject (& BMP toolbar );
Cbitmap bmp dst; // new bitmap
Bmp dst. createcompatiblebitmap (& dcsrc, nwidthnew, nheightnew );
Cbitmap * pbmpolddst = dcdst. SelectObject (& bmp dst );
// First fill in the background color
Dcdst. fillsolidrect (crect (0, 0, nwidthnew, nheightnew),: getsyscolor (color_btnface ));
Dcdst. setbkmode (transparent); // sets the transparent background mode.
Int nstartx = (sizenewimage. cx-sizeImage.cx)/2; // calculates the start abscissa for center Processing
// Start to create a toolbar bitmap.
For (I = 0; I <nbtncount; ++ I)
{
Dcdst. bitblt (I * sizenewimage. cx + nstartx, 0, sizeimage. CX, sizeimage. Cy,
& Dcsrc, I * sizeimage. CX, 0, srccopy );
Int J;
For (j = 0; j <pstrarray [I]. getsize (); j ++)
{
Csize size = dcdst. gettextextent (pstrarray [I] [J]);
Int nstratx = (sizenewimage. cx-size.cx)/2;
Dcdst. textout (I * sizenewimage. cx + nstratx, sizeimage. Cy + J * ncharheight, pstrarray [I] [J]);
}
}
// Restore DC and release resources
Dcsrc. SelectObject (pbmpoldsrc );
Dcdst. SelectObject (pbmpolddst );
Dcdst. SelectObject (pfntold );
Bar. releasedc (pdcctrl );
Delete [] pstrarray;
//--------------------------------------------------------
// Step 4: Set the toolbar
// Reset the size.
Setsizes (sizenewimage + csize (7, 7), sizenewimage );
// Replace the toolbar bitmap
Addreplacebitmap (hbitmap) bmp dst. Detach ());
Return true;
}
In the above Code, calculate the new button Image Height sizenewimage. change Cy to calculate the width of the new button image sizenewimage. cx. When creating a toolbar bitmap, paste the original image on the left and write the text on the right to implement the toolbar button on the right.
As the toolbar button images do not change, the above Code is of little significance and can be used as a learning reference.