CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    6

    Dialog CEdit Control Color

    I have created a single dialog box with 3 controls. I created the following function and attatched it to WM_CTLCOLOR of the dialog box using the appwizard. However, the dialog is created and my custom colors are not even posted to the box. I can step through the code with debug and it is finding the controls, just not setting the right colors.

    I'm new to MFC and this is driving me crazy. You would think there should be an easier way to change a dialog controls background and text color.

    TIA
    Andy


    HBRUSH CSafeMsgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if(nCtlColor == CTLCOLOR_EDIT)
    {
    int ID = pWnd->GetDlgCtrlID();
    if(ID == IDC_EDIT1)
    {
    pDC->SetTextColor(RGB(23, 29, 230));
    pDC->SetBkMode(TRANSPARENT);
    hbr = (HBRUSH)(RGB(253, 253, 0));
    }
    if(ID == IDC_EDIT2)
    {
    pDC->SetTextColor(RGB(23, 29, 230));
    pDC->SetBkMode(TRANSPARENT);
    hbr = (HBRUSH)(RGB(253, 253, 0));
    }

    if(ID == IDC_STATIC)
    {
    pDC->SetTextColor(RGB(255,255,254));
    pDC->SetBkMode(TRANSPARENT);
    hbr = (HBRUSH)::GetStockObject(BLACK_BRUSH);
    }
    }
    return hbr;
    }


    ô¿ô
    «=»

  2. #2
    Join Date
    Apr 1999
    Posts
    6

    Re: Dialog CEdit Control Color

    Answered my own question and hopefully this will help other newbies.

    Using the ClassWizard, create a function for the WM_CTLCOLOR for you dialog class.

    Then use some thing like this.

    HBRUSH CSafeMsgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC)
    {
    pDC->SetTextColor(RGB(255,255,255));
    pDC->SetBkColor(RGB(0,0,0));
    hbr = CreateSolidBrush(RGB(0,0,0));
    }

    return hbr;
    }


    Then, use SetDialogItemText(<control name>, <text&gt

    Example:
    SetDialogItem(IDC_EDIT1, "This is some text");

    When the control is drawn, it will have the colors you specified in the OnCtlColor().

    For other CTL controls, check out the help text for OnCtlColor().

    ô¿ô
    «=»

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured