英文:
Changing Color of text when color is selected from combo box
问题
IDC_EDIT1
框中的文本颜色可以在从组合框选择新颜色时更改,但它现在无效。
OnCbnSelchangeComboColorBox
:Combo Box的事件处理程序
m_Combo_Box
:组合框的变量名称
m_text
:IDC_EDIT1
的变量名称
void CFontFormatter::OnCbnSelchangeComboColorBox()
{
int comboBoxSelectedItem = m_Combo_Box.GetCurSel();
if (comboBoxSelectedItem != LB_ERR) {
CString selectedColorText;
m_Combo_Box.GetLBText(comboBoxSelectedItem, selectedColorText);
if (selectedColorText == _T("Red"))
fontColor = RGB(255, 0, 0);
else if (selectedColorText == _T("Green"))
fontColor = RGB(0, 255, 0);
else if (selectedColorText == _T("Blue"))
fontColor = RGB(0, 0, 255);
CWnd* pEditControl = GetDlgItem(IDC_EDIT1);
if (pEditControl)
{
CDC* pDC = m_editText.GetDC();
pDC->SetTextColor(fontColor);
pEditControl->Invalidate();
pEditControl->UpdateWindow();
}
m_text.SetString(selectedColorText);
UpdateData(FALSE);
}
}
我已经尝试过CWnd*
和CDC*
。
英文:
I would like to change the text color of the text present in IDC_EDIT1
box when a new color is selected from the combo box. But it is now working,
OnCbnSelchangeComboColorBox
:- Event Handler for Combo Box
m_Combo_Box
:- Variable name for combo box.
m_text
:- Variable name for IDC_EDIT1
.
void CFontFormatter::OnCbnSelchangeComboColorBox()
{
int comboBoxSelectedItem = m_Combo_Box.GetCurSel();
if (comboBoxSelectedItem != LB_ERR) {
CString selectedColorText;
m_Combo_Box.GetLBText(comboBoxSelectedItem, selectedColorText);
if (selectedColorText == _T("Red"))
fontColor = RGB(255, 0, 0);
else if (selectedColorText == _T("Green"))
fontColor = RGB(0, 255, 0);
else if (selectedColorText == _T("Blue"))
fontColor = RGB(0, 0, 255);
CWnd* pEditControl = GetDlgItem(IDC_EDIT1);
if (pEditControl)
{
CDC* pDC = m_editText.GetDC();
pDC->SetTextColor(fontColor);
pEditControl->Invalidate();
pEditControl->UpdateWindow();
}
m_text.SetString(selectedColorText);
UpdateData(FALSE);
}
}
I have Tried through CWnd*
and CDC*
.
答案1
得分: 2
这里有一个资源 https://www.equestionanswers.com/vcpp/background-color-edit-static.php)。它们提供了一些代码:
case WM_CTLCOLOREDIT:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,255));
SetBkColor(hdcStatic, RGB(0,230,0));
return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
}
英文:
Here is one resource https://www.equestionanswers.com/vcpp/background-color-edit-static.php). They provide some code:
case WM_CTLCOLOREDIT:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,255));
SetBkColor(hdcStatic, RGB(0,230,0));
return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论