TB_ADDSTRING
在工具栏的数据结构中,有一个字符串列表,在添加或者插入工具栏按钮的时候,可以使用这个列表中的字符串作为按钮文字。TB_ADDSTRING 消息用来向工具栏的字符串列表添加一个或多个新的字符串。
wParam = (WPARAM) (HINSTANCE) hinst;
lParam = (LPARAM) MAKELONG(idString, 0);
Parameters
hinst
Handle to the module instance with an executable file that contains the string resource. If idString instead points to a character array with one or more strings, set this parameter to NULL.
idString
Resource identifier for the string resource. If hinst is set to NULL, idString points to a character array with one or more null-terminated strings. The last string in the array must be terminated with two null characters.
Return Values
Returns the index of the first new string if successful, or -1 otherwise.
Remarks
lParam = (MAKELONG)(idString, 0); // 一个字符串资源的资源 ID
{
ASSERT(::IsWindow(m_hWnd));
HINSTANCE hInst = AfxFindResourceHandle(MAKEINTRESOURCE((nStringID>>4)+1),
RT_STRING);
ASSERT(hInst != NULL);
return (int)::SendMessage(m_hWnd, TB_ADDSTRING, (WPARAM)hInst, nStringID);
}
lParam = (LPCTSTR)lpszStrings; // 一个字符串缓冲区
m_tbCtrl.AddStrings(_T("剪切(&T)/0"));
m_tbCtrl.AddStrings(_T("复制(&C)/0"));
m_tbCtrl.AddStrings(_T("粘贴(&P)/0"));
// Code2
m_tbCtrl.AddStrings(_T("剪切(&T)/0复制(&C)/0粘贴(&P)/0"));
Example Code
以下代码在对话框中创建了一个工具栏,并添加了三个带文字的工具栏按钮。方法一和方法二是两种常用指定工具栏文字的方法。使用方法一时要注意,你必须确认字符串缓冲区有足够的长度,以避免发生写内存越界的情况。
代码中所使用到的字符串资源定义如下:
IDS_CUT | 剪切(&T) |
IDS_COPY | 复制(&C) |
IDS_PASTE | 粘贴(&P) |
BOOL CTest_ToolbarCtrlDlg::CreateAToolBar()
{
if (!m_tbCtrl.Create(WS_CHILD, CRect(0, 0, 0, 0), this, IDR_TOOLBAR1))
{
ASSERT(FALSE);
return FALSE;
}
// 方法一
// CString strText(_T(""));
// TCHAR szBuf[16]; // 字符串缓冲区
//
// strText.LoadString(IDS_CUT);
// lstrcpy(szBuf, strText);
// szBuf[lstrlen(szBuf) + 1] = _T('/0'); // 以两个'/0'字符结尾
// int iCut = m_tbCtrl.AddStrings(szBuf);
//
// strText.LoadString(IDS_COPY);
// lstrcpy(szBuf, strText);
// szBuf[lstrlen(szBuf) + 1] = _T('/0'); // 以两个'/0'字符结尾
// int iCopy = m_tbCtrl.AddStrings(szBuf);
//
// strText.LoadString(IDS_PASTE);
// lstrcpy(szBuf, strText);
// szBuf[lstrlen(szBuf) + 1] = _T('/0'); // 以两个'/0'字符结尾
// int iPaste = m_tbCtrl.AddStrings(szBuf);
// 方法二
CString strText(_T(""));
strText.LoadString(IDS_CUT);
CString strCut(strText, strText.GetLength()+1);
int iCut = m_tbCtrl.AddStrings(strCut);
strText.LoadString(IDS_COPY);
CString strCopy(strText, strText.GetLength()+1);
int iCopy = m_tbCtrl.AddStrings(strCopy);
strText.LoadString(IDS_PASTE);
CString strPaste(strText, strText.GetLength()+1);
int iPaste = m_tbCtrl.AddStrings(strPaste);
// Fill the TBBUTTON array with button information, and add the
// buttons to the toolbar. The buttons on this toolbar have text
// but do not have bitmap images.
TBBUTTON tbb[3];
tbb[0].iBitmap = -1;
tbb[0].idCommand = IDS_CUT;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].dwData = 0;
tbb[0].iString = iCut;
tbb[1].iBitmap = -1;
tbb[1].idCommand = IDS_COPY;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].dwData = 0;
tbb[1].iString = iCopy;
tbb[2].iBitmap = -1;
tbb[2].idCommand = IDS_PASTE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].dwData = 0;
tbb[2].iString = iPaste;
m_tbCtrl.AddButtons(sizeof(tbb)/sizeof(tbb[0]), tbb);
m_tbCtrl.AutoSize();
m_tbCtrl.ShowWindow(SW_SHOW);
return TRUE;
}