您的位置:首页 > 其它

用BCG开发字体设定工具栏在按下粗体设置按钮式后,选中内容被设定,但是工具栏上的按钮没有处于按下状态

2010-12-24 10:31 666 查看
用BCG开发 字体设定工具栏,粗体、斜体、下划线按钮CBCGPToolbarButton 在按下后,CRichEditCtrl对应的选中内容被设定,但是工具栏上的按钮没有处于按下状态,即使在按钮按下消息里添加设置按钮状态的代码也无法在该消息里设定按钮状态

----------解决方法:按钮设为 TBBS_CHECKBOX 风格

void CFormatBar2::OnReset ()
{
UINT nvCmdID[3];
nvCmdID[0] = ID_CHAR_BOLD;
nvCmdID[1] = ID_CHAR_ITALIC;
nvCmdID[2] = ID_CHAR_UNDERLINE;

for (int i=0; i<3 ; i++)
{
UINT nCmdID = nvCmdID[i];

int index = CommandToIndex(nCmdID);
if (index >=0)
{
CBCGPToolbarButton *pBtn = GetButton(index);
pBtn->SetStyle(TBBS_CHECKBOX);!!!

----------解决方法:按钮设为 TBBS_CHECKBOX 风格

}
}

// Insert the color menu button:
CBCGPColorMenuButton* pColorButton = CreateColorButton ();
ReplaceButton (ID_CHAR_COLOR, *pColorButton);
delete pColorButton;
}

另外附上按钮按下时消息响应函数

typedef struct
{
UINT nCmdID;
DWORD dwMask;
DWORD dwEffects;
} FONT_CONTROL;

void CPropertyDlg::OnCharEffect(UINT nID)
{

FONT_CONTROL m_FontControlPara[3];

int i=0;
m_FontControlPara[i].nCmdID = ID_CHAR_BOLD;
m_FontControlPara[i].dwMask = CFM_BOLD;
m_FontControlPara[i].dwEffects = CFE_BOLD;
i++;

m_FontControlPara[i].nCmdID = ID_CHAR_ITALIC;
m_FontControlPara[i].dwMask = CFM_ITALIC;
m_FontControlPara[i].dwEffects = CFE_ITALIC;
i++;

m_FontControlPara[i].nCmdID = ID_CHAR_UNDERLINE;
m_FontControlPara[i].dwMask = CFM_UNDERLINE;
m_FontControlPara[i].dwEffects = CFE_UNDERLINE;
i++;

。。。。。

AFX_MANAGE_STATE(AfxGetStaticModuleState());

CLDCText40Control *pCtrl = (CLDCText40Control *)m_pCtrl;

int i;
for (i=0; i<3; i++)
{
if (nID==m_FontControlPara[i].nCmdID)
break;
}

if (i>=3)
{
return;
}

CWnd *pWnd = CWnd::FromHandle(pCtrl->m_hWnd);
CRichEditCtrlEx *pEdit = (CRichEditCtrlEx *)pWnd;

CCharFormat cf;
long nStart, nEnd;

pEdit->GetSel(nStart, nEnd);
pEdit->SetSel(nStart, (nEnd>nStart?nStart+1:nStart));//!!! 这样做是因为 字体不同 时 cf.dwEffects 为无效值
pEdit->GetSelectionCharFormat(cf);
pEdit->SetSel(nStart, nEnd);

if ( (cf.dwMask&m_FontControlPara[i].dwMask)==m_FontControlPara[i].dwMask)
{
//DWORD nRes = cf.dwEffects&CFE_BOLD;
//if (nRes==(DWORD)CFE_BOLD)
if ((cf.dwEffects&m_FontControlPara[i].dwEffects)==m_FontControlPara[i].dwEffects)
{
cf.dwEffects = 0;
}
else
{
cf.dwEffects = (m_FontControlPara[i].dwEffects);
}

cf.dwMask = m_FontControlPara[i].dwMask;
pEdit->SetSelectionCharFormat(cf);

pEdit->SetFocus ();
}

}

奇怪的是,在居中+对齐按钮的响应函数中对工具栏按钮状态进行设置却有效果一下列出代码----(???原因待查)

void CPropertyDlg::OnParaFormat(UINT nID)
{
CLDCText40Control *pCtrl = (CLDCText40Control *)m_pCtrl;
CWnd *pWnd = CWnd::FromHandle(pCtrl->m_hWnd);
CRichEditCtrlEx *pEdit = (CRichEditCtrlEx *)pWnd;

PARAFORMAT pf;
pf.dwMask = PFM_ALIGNMENT;
switch (nID)
{
case ID_PARA_LEFT:
pf.wAlignment = PFA_LEFT;
break;
case ID_PARA_CENTER:
pf.wAlignment = PFA_CENTER;
break;
case ID_PARA_RIGHT:
pf.wAlignment = PFA_RIGHT;
break;
default:
return;
break;
}

pEdit->SetParaFormat(pf);

this->SetDlgAttFromCtrl(pCtrl);
}

另外附上根据edit控件最新状态设置工具栏按钮状态的代码,但是代码对 居中+对齐 有效,对粗斜体无效(???原因待查)
void CPropertyDlg::SetDlgAttFromCtrl(const CLDCControl *pCtrl)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

CLDCText40Control *pTxtCtrl = (CLDCText40Control *)pCtrl;
m_pCtrl = pTxtCtrl;

// sec.1 x,y,w,h
m_SP_TopLeftX = pCtrl->m_Att.m_lpShapePoints[0].x;
m_SP_TopLeftY = pCtrl->m_Att.m_lpShapePoints[0].y;
m_SP_BottomRightX = pCtrl->m_Att.m_lpShapePoints[4].x;
m_SP_BottomRightY = pCtrl->m_Att.m_lpShapePoints[4].y;
//m_SP_TopLeftX = pTxtCtrl->m_pEdtFrame->m_lpShapePoints[0].x;
//m_SP_TopLeftY = pTxtCtrl->m_pEdtFrame->m_lpShapePoints[0].y;
//m_SP_BottomRightX = pTxtCtrl->m_pEdtFrame->m_lpShapePoints[4].x;
//m_SP_BottomRightY = pTxtCtrl->m_pEdtFrame->m_lpShapePoints[4].y;

m_Width = m_SP_BottomRightX - m_SP_TopLeftX + 1;//add by zcl for debug 0000379
m_Height= m_SP_BottomRightY - m_SP_TopLeftY + 1;//add by zcl for debug 0000379

// sec.2 font
if (m_hWnd!=NULL)
{
/*CHARFORMAT*/WPD_CHARFORMAT cf;
long nStart, nEnd;

CWnd *pWnd = CWnd::FromHandle(pTxtCtrl->m_hWnd);
CRichEditCtrlEx *pEdit = (CRichEditCtrlEx *)pWnd;

pEdit->GetSel(nStart, nEnd);
pEdit->SetSel(nStart, (nEnd>nStart?nStart+1:nStart));
pEdit->GetSelectionCharFormat(cf);
pEdit->SetSel(nStart, nEnd);

// font name
CObList listButtons;

CString strFontName;
if (m_wndFormatBar.GetCommandButtons (IDC_FONTNAME, listButtons) > 0)
{
for (POSITION posCombo = listButtons.GetHeadPosition (); posCombo != NULL;)
{
CBCGPToolbarFontCombo* pCombo =
DYNAMIC_DOWNCAST (CBCGPToolbarFontCombo, listButtons.GetNext (posCombo));

if (pCombo != NULL/* && !pCombo->HasFocus ()*/)
{
if ((cf.dwMask & (CFM_FACE|CFM_CHARSET)) == (CFM_FACE|CFM_CHARSET))
{
#if _MSC_VER >= 1300
strFontName = cf.szFaceName;
#else
strFontName = A2T(cf.szFaceName);
#endif
pCombo->SetFont(strFontName, DEFAULT_CHARSET, TRUE);
}
else
{
pCombo->SetText(_T(""));
}
}
}
}

// font size
if (m_wndFormatBar.GetCommandButtons (IDC_FONTSIZE, listButtons) > 0)
{
for (POSITION posCombo = listButtons.GetHeadPosition (); posCombo != NULL;)
{
CBCGPToolbarFontSizeCombo* pCombo =
DYNAMIC_DOWNCAST (CBCGPToolbarFontSizeCombo, listButtons.GetNext (posCombo));

if (pCombo != NULL && !pCombo->HasFocus ())
{
if (!strFontName.IsEmpty () && pCombo->GetCount () == 0)
{
pCombo->RebuildFontSizes (strFontName);
}

pCombo->SetTwipSize( (cf.dwMask & CFM_SIZE) ? cf.yHeight : -1);
}
}
}

// font ID_CHAR_BOLD -- ID_CHAR_UNDERLINE
for (int i=0; i<3 ; i++)
{
UINT nCmdID = m_FontControlPara[i].nCmdID;

int index = m_wndFormatBar2.CommandToIndex(nCmdID);
if (index >=0)
{
CBCGPToolbarButton *pBtn = m_wndFormatBar2.GetButton(index);
if ((cf.dwMask & (m_FontControlPara[i].dwMask)) == (m_FontControlPara[i].dwMask ))
{
LONG nStyle = pBtn->m_nStyle;
if (cf.dwEffects &m_FontControlPara[i].dwEffects)
{
pBtn->SetStyle(nStyle|TBBS_CHECKED);
}
else
{
pBtn->SetStyle(nStyle&~TBBS_CHECKED);
}
}
}
}
m_wndFormatBar2.Invalidate(TRUE);

// ParaFormat
{
PARAFORMAT pf;
pEdit->GetParaFormat(pf);
if ( PFM_ALIGNMENT==(pf.dwMask&PFM_ALIGNMENT) )
{
int nCmdID = 0;

if ( PFA_LEFT==pf.wAlignment )
{
nCmdID = ID_PARA_LEFT;
}
else if ( PFA_CENTER==pf.wAlignment )
{
nCmdID = ID_PARA_CENTER;
}
else if ( PFA_RIGHT==pf.wAlignment )
{
nCmdID = ID_PARA_RIGHT;
}
else
{
nCmdID = 0;
}

if (0==nCmdID)
return;

UINT nvCmdID[6];
nvCmdID[3] = ID_PARA_LEFT;
nvCmdID[4] = ID_PARA_CENTER;
nvCmdID[5] = ID_PARA_RIGHT;

for (int i=3; i<6 ; i++)
{
int index = m_wndFormatBar2.CommandToIndex(nvCmdID[i]);
if (index >=0)
{
CBCGPToolbarButton *pBtn = m_wndFormatBar2.GetButton(index);
UINT nStyle = pBtn->m_nStyle;
if (nCmdID==nvCmdID[i])
{
pBtn->SetStyle(nStyle|TBBS_CHECKED);
}
else
{
pBtn->SetStyle(nStyle&(~TBBS_CHECKED));
}
}
}

m_wndFormatBar2.Invalidate();
}

this->UpdateData(FALSE);
}

} // if (m_hWnd!=NULL)

m_Dis_RollMode = pCtrl->m_Att.m_Dis_RollMode;//Add by zhoufeng
m_Dis_RollSpeed = pCtrl->m_Att.m_Dis_RollSpeed;//Add by zhoufeng

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐