Thursday, June 20, 2013

visual studio 2012 c++ mfc ctoolbar develop log

prepare:

CToolBar m_ToolBar;

 

1. create toolbar:

if (!m_ToolBar.CreateEx( this,TBSTYLE_FLAT|TBSTYLE_TOOLTIPS,  TTS_ALWAYSTIP|WS_CHILD | WS_VISIBLE |CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY |CBRS_SIZE_DYNAMIC |CBRS_ORIENT_HORZ,
CRect(4,4,0,0)) || !m_ToolBar.LoadToolBar(IDR_TOOLBAR1) )
{
MessageBox(_T("初始化错误!"));
return FALSE;
}



2.reposition bar,and tool bar can show:

RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);



3.show some toolbar buttons sample:


void CSSPViewsPropPage::switchWindowControls(BOOL show){
int ids[] = {ID_OPEN,ID_SAVE,ID_SAVEAS};
int idx;
UINT style;
CRect rect1;

for(int i=0,c=_ARRAYSIZE(ids);i<c;++i){
idx = m_ToolBar.CommandToIndex(ids[i]);
if(idx < 0)
continue;
style = m_ToolBar.GetButtonStyle(idx);
if(show){
style &= ~TBBS_HIDDEN;
}else{
style |= TBBS_HIDDEN;
}
m_ToolBar.SetButtonStyle(idx,style);
}

// if have some dynamic create control,need move window like follow:

// if(m_btColor.m_hWnd){
    //    rect1 = disableToolbarControls(ID_CURPAGE,2);
    //    m_btColor.MoveWindow(&rect1);
    // }
}


4.disable some toolbar continues buttons sample,and it return they rect for some dynamic create control.

CRect CSSPViewsPropPage::disableToolbarControls(UINT nID,int count){
CRect rect1,rect2;
int index = m_ToolBar.CommandToIndex(nID);
if(index<0)
return CRect();
//m_ToolBar.GetItemRect(index, &rect1);
m_ToolBar.GetToolBarCtrl().GetItemRect(index, &rect1);
m_ToolBar.GetToolBarCtrl().GetItemRect(index+count-1, &rect2);
rect1.right = rect2.right;
rect1.bottom -= 2; rect1.top += 2;

UINT style;
for(int i=0;i<count;++i){
style = m_ToolBar.GetButtonStyle(index+i);
style |= TBBS_DISABLED;
m_ToolBar.SetButtonStyle(index+i,style);
}
return rect1;
}
5.dynamic create some window control in the toolbar buttons:

rect1 = disableToolbarControls(ID_COUNTPAGE,2);

// CStatic m_txCount;
m_txCount.Create( _T("/0"), WS_CHILD|WS_VISIBLE, rect1, &m_ToolBar, 0);





6.enable or disable some toolbar button sample:

void CSSPViewsPropPage::UpdateStateToolBar(bool b)
{
m_ToolBar.GetToolBarCtrl().EnableButton(IDC_UNDO,b);
m_ToolBar.GetToolBarCtrl().EnableButton(IDC_STAMP,b);
}



7.resize toolbar with parent window and silbing control:

void CSSPViewsPropPage::OnSize(UINT nType, int cx, int cy)
{
COlePropertyPage::OnSize(nType, cx, cy);
//UPDATE_EASYSIZE;
if(m_pStatic1.m_hWnd){
CRect rect,rect2;
int height;
GetClientRect(&rect);

m_ToolBar.GetWindowRect(&rect2);
height = rect2.Height();
rect2 = rect;
rect2.bottom = rect2.top + height;
m_ToolBar.MoveWindow(&rect2);

rect.top += height;
m_pStatic1.MoveWindow(&rect);
}
}



8.toolbar click event:

BEGIN_MESSAGE_MAP(CSSPViewsPropPage, COlePropertyPage)
ON_COMMAND_RANGE(IDC_LEFT, ID_SAVEAS, &CSSPViewsPropPage::OnToolBarButton)
END_MESSAGE_MAP()

void CSSPViewsPropPage::OnToolBarButton(UINT nID)
{
if (nID < IDC_LEFT || nID > ID_SAVEAS)
{
return;
}
switch(nID)
{
case ID_OPEN:
{
// code
}
}
}






9.toolbar tooltip:

if (!m_ToolBar.CreateEx( this,TBSTYLE_FLAT|TBSTYLE_TOOLTIPS,  TTS_ALWAYSTIP|WS_CHILD | WS_VISIBLE |CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY |CBRS_SIZE_DYNAMIC |CBRS_ORIENT_HORZ,
CRect(4,4,0,0)) || !m_ToolBar.LoadToolBar(IDR_TOOLBAR1) )
{
MessageBox(_T("初始化错误!"));
return FALSE;
}

BEGIN_MESSAGE_MAP(CSSPViewsPropPage, COlePropertyPage)
ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify)
END_MESSAGE_MAP()

BOOL CSSPViewsPropPage::OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
TCHAR szFullText[256];
CString strTipText;
UINT nID = pNMHDR->idFrom;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
// idFrom is actually the HWND of the tool
nID = ::GetDlgCtrlID((HWND)nID);
}

if (nID != 0) // will be zero on a separator
{
// don't handle the message if no string resource found
if (AfxLoadString(nID, szFullText) == 0)
return FALSE;

// this is the command id, not the button index
AfxExtractSubString(strTipText, szFullText, 1, '\n');
}

#ifndef _UNICODE
if (pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn(pTTTA->szText, strTipText, _countof(pTTTA->szText));
else
_mbstowcsz(pTTTW->szText, strTipText, _countof(pTTTW->szText));
#else
if (pNMHDR->code == TTN_NEEDTEXTA)
_wcstombsz(pTTTA->szText, strTipText, _countof(pTTTA->szText));
else
lstrcpyn(pTTTW->szText, strTipText, _countof(pTTTW->szText));
#endif
*pResult = 0;

return TRUE; // message was handled
}

No comments: