从组合框中选择颜色时更改文本颜色。

huangapple go评论76阅读模式
英文:

Changing Color of text when color is selected from combo box

问题

IDC_EDIT1框中的文本颜色可以在从组合框选择新颜色时更改,但它现在无效。

OnCbnSelchangeComboColorBox:Combo Box的事件处理程序

m_Combo_Box:组合框的变量名称

m_textIDC_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));
}

huangapple
  • 本文由 发表于 2023年7月31日 20:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定