Click to See Complete Forum and Search --> : Text on toolbar buttons.


Cooker
August 21st, 2004, 10:37 PM
Hello,
I create a toolbar and three buttons,and I would like to add text on buttons.
But i find the text is truncated.
I want to make the whole text on buttons.
How shall I do it???
Thanks a lot! :)

#define NUM_BUTTONS 3
#define MAX_LEN 128

HWND CreateAToolBar(HWND hwndParent)
{
HWND hwndTB;
TBBUTTON tbb[NUM_BUTTONS];
INITCOMMONCONTROLSEX icex;
char szBuf[MAX_LEN] ;
int cch;
int iNew, iOpen, iSaveas;

// Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&icex);
tbab.hInst = GetModuleHandle(NULL);
tbab.nID = IDR_TOOLBAR1;

// Create a toolbar.
hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR) NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwndParent,
(HMENU) IDR_TOOLBAR1, GetModuleHandle(NULL), NULL);

// Send the TB_BUTTONSTRUCTSIZE message, which is required for
// backward compatibility.
SendMessage(hwndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);



LoadString(GetModuleHandle(NULL), IDS_NEW, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iNew = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);

LoadString(GetModuleHandle(NULL), IDS_OPEN, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iOpen = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);


LoadString(GetModuleHandle(NULL), IDS_SAVEAS, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iSaveas = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);


ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = -1;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = ID_FILE_NEW;
tbb[0].iString = (int)iNew;

tbb[1].iBitmap = -1;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = ID_FILE_OPEN;
tbb[1].iString = (int)iOpen;

tbb[2].iBitmap = -1;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = ID_FILE_SAVEAS;
tbb[2].iString = (int)iSaveas;

SendMessage(hwndTB, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
return hwndTB;

}

Cooker
August 22nd, 2004, 06:17 AM
I would like to do it!

Bond
August 23rd, 2004, 07:25 AM
Looks like you've already got one base covered -- setting the iBitmap member of TBBUTTON to I_IMAGENONE (or -1). Now, specify the TBSTYLE_LIST style when creating the toolbar. Finally, use the BTNS_AUTOSIZE style when creating your buttons.

Cooker
August 23rd, 2004, 08:05 AM
Looks like you've already got one base covered -- setting the iBitmap member of TBBUTTON to I_IMAGENONE (or -1). Now, specify the TBSTYLE_LIST style when creating the toolbar. Finally, use the BTNS_AUTOSIZE style when creating your buttons.
Aftering correcting

#define NUM_BUTTONS 3
#define MAX_LEN 128

HWND CreateAToolBar(HWND hwndParent)
{
HWND hwndTB;
TBBUTTON tbb[NUM_BUTTONS];
char szBuf[MAX_LEN] ;
int cch;
int iNew, iOpen, iSaveas;


// Create a toolbar.
hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR) NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | TBSTYLE_LIST , 0, 0, 0, 0, hwndParent,
(HMENU) IDR_TOOLBAR1, GetModuleHandle(NULL), NULL);

// Send the TB_BUTTONSTRUCTSIZE message, which is required for
// backward compatibility.
SendMessage(hwndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);

LoadString(GetModuleHandle(NULL), IDS_NEW, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iNew = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);

LoadString(GetModuleHandle(NULL), IDS_OPEN, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iOpen = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);


LoadString(GetModuleHandle(NULL), IDS_SAVEAS, szBuf, MAX_LEN-1);
cch = strlen(szBuf);
szBuf[cch+1] = '\0';
iSaveas = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);


ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = -1;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_AUTOSIZE;
tbb[0].idCommand = IDM_NEW;
tbb[0].iString = (int)iNew;

tbb[1].iBitmap = -1;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_AUTOSIZE;
tbb[1].idCommand = IDM_OPEN;
tbb[1].iString = (int)iOpen;

tbb[2].iBitmap = -1;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_AUTOSIZE;
tbb[2].idCommand = ID_FILE_SAVEAS;
tbb[2].iString = (int)iSaveas;

SendMessage(hwndTB, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
return hwndTB;

}

But there are still some spaces at the left side of text.
How to delect to space???
Thanks!

Bond
August 23rd, 2004, 10:52 AM
I was looking at one of your other posts where you said you replaced I_IMAGENONE with -1 because the compiler didn't recognize I_IMAGENONE. However, they're defined as follows:

#define I_IMAGECALLBACK (-1)
#if (_WIN32_IE >= 0x0501)
#define I_IMAGENONE (-2)
#endif // 0x0501


You should use -2 instead. That will remove the extra space where it's expecting the image to be.

BTW, did you download the SDK?