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

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

Changing Color of text when color is selected from combo box

问题

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

OnCbnSelchangeComboColorBox:Combo Box的事件处理程序

m_Combo_Box:组合框的变量名称

m_textIDC_EDIT1的变量名称

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

  1. void CFontFormatter::OnCbnSelchangeComboColorBox()
  2. {
  3. int comboBoxSelectedItem = m_Combo_Box.GetCurSel();
  4. if (comboBoxSelectedItem != LB_ERR) {
  5. CString selectedColorText;
  6. m_Combo_Box.GetLBText(comboBoxSelectedItem, selectedColorText);
  7. if (selectedColorText == _T("Red"))
  8. fontColor = RGB(255, 0, 0);
  9. else if (selectedColorText == _T("Green"))
  10. fontColor = RGB(0, 255, 0);
  11. else if (selectedColorText == _T("Blue"))
  12. fontColor = RGB(0, 0, 255);
  13. CWnd* pEditControl = GetDlgItem(IDC_EDIT1);
  14. if (pEditControl)
  15. {
  16. CDC* pDC = m_editText.GetDC();
  17. pDC->SetTextColor(fontColor);
  18. pEditControl->Invalidate();
  19. pEditControl->UpdateWindow();
  20. }
  21. m_text.SetString(selectedColorText);
  22. UpdateData(FALSE);
  23. }
  24. }

我已经尝试过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.

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

  1. void CFontFormatter::OnCbnSelchangeComboColorBox()
  2. {
  3. int comboBoxSelectedItem = m_Combo_Box.GetCurSel();
  4. if (comboBoxSelectedItem != LB_ERR) {
  5. CString selectedColorText;
  6. m_Combo_Box.GetLBText(comboBoxSelectedItem, selectedColorText);
  7. if (selectedColorText == _T("Red"))
  8. fontColor = RGB(255, 0, 0);
  9. else if (selectedColorText == _T("Green"))
  10. fontColor = RGB(0, 255, 0);
  11. else if (selectedColorText == _T("Blue"))
  12. fontColor = RGB(0, 0, 255);
  13. CWnd* pEditControl = GetDlgItem(IDC_EDIT1);
  14. if (pEditControl)
  15. {
  16. CDC* pDC = m_editText.GetDC();
  17. pDC->SetTextColor(fontColor);
  18. pEditControl->Invalidate();
  19. pEditControl->UpdateWindow();
  20. }
  21. m_text.SetString(selectedColorText);
  22. UpdateData(FALSE);
  23. }
  24. }

I have Tried through CWnd* and CDC*.

答案1

得分: 2

  1. 这里有一个资源 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:

  1. case WM_CTLCOLOREDIT:
  2. {
  3. HDC hdcStatic = (HDC) wParam;
  4. SetTextColor(hdcStatic, RGB(0,0,255));
  5. SetBkColor(hdcStatic, RGB(0,230,0));
  6. return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
  7. }

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:

确定