none
Read-only edit control gets control type of CTLCOLOR_BTN in OnCtlColor RRS feed

  • Question

  • Anyone run across this?

     

    If I set an edit control to be read-only, the OnCtlColor gets a CTLCOLOR_BTN type (3) rather than CTLCOLOR_EDIT (1).

    Anyone know of a workaround other than catching 'every' control by the ID (which I have > 40 dialogs).

     

    WM5; C++; dialog based; VS2005

     

    Thanks! 


     

    Sunday, December 30, 2007 2:00 PM

All replies


  • A read only edit control can't respond to CTLCOLOR_BTN .I think you had done something wrong inside your code.Just check it out Try following code.

    Code Block
    HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        //GetDlgCtrlID will return you ,your control ID just use it in following manner  
        if(pWnd->GetDlgCtrlID() ==IDC_EDIT_NAME)
        {
             //Perform your painting operation here
        }
     return hbr;
    }






    Thanx


    Sunday, December 30, 2007 3:50 PM
  • This appears to be an MFC issue.  I just verified it by creating a new (WM6  SDK) Dialog based app.  Added 2 edit boxes; one read only.  I TRACE out the OnCtlColor, and sure enough, the read-only edit comes in with nCtlColor = 3, which is CTLCOLOR_BTN  and not CTLCOLOR_EDIT.  Like I stated in my earlier post, I was hoping to avoid having to change colors on a per-dialog basis.  Thoughts?
    Monday, December 31, 2007 4:56 PM
  • First thing what is 3 here second i think you should use == operator instead of = operator. just check out your code once again.if still problem show your code here.And don't perform this type of checking .use proper macro or have a look on my code .

    Thanx
    Monday, December 31, 2007 5:27 PM
  • 3 is CTLCOLOR_BTN .  1 is CTLCOLOR_EDIT.

    I am using a switch so == operator is moot.

     

    Code Block

    HBRUSH CAbstractDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        COLORREF CR_WHITE = (COLORREF)RGB(255,255,255);
        COLORREF CR_BLUE  = (COLORREF)RGB(255,255,255);

        //Set background and text color depending on TYPE
        switch (nCtlColor)
        {
     case CTLCOLOR_BTN/*3*/:  pDC->SetTextColor(CR_WHITE);
         bkColor = CR_BLUE; 
         break;
     case CTLCOLOR_EDIT/*1*/: pDC->SetTextColor(CR_BLUE);
         bkColor = CR_WHITE;    
         break;
     ... other controls ...
        }

        ...Create brushes, set colors, return brush ...
     
    }

     

     

    So, to reiterate.  If I make an EDIT control read-only.  It comes across here as a button.  Is this an MFC defect?  I cannot find documentation on this?

     

    Thanks! Tim

    Monday, December 31, 2007 7:43 PM
  • I Don't know what the wrong with the code.Try this
    Code Block


    HBRUSH CSssDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
     
        if(nCtlColor == CTLCOLOR_EDIT)
        {
            if(pWnd->GetDlgCtrlID() == IDC_ED1)
            {
                pDC->SetTextColor(RGB(255, 0, 0));
                pDC->SetBkColor(RGB(255,255,0));
                return m_brh;
            }
        }

        return hbr;
    }



    Thanx
    Tuesday, January 1, 2008 7:42 AM
  • That would never work because nCtlColor never equals CTLCOLOR_EDIT.  It equals CTLCOLOR_BTN. 

     

    Wednesday, January 2, 2008 6:09 PM
  • Still i am sure that there is something wrong inside your code. just go to your resource and check out the Id and Type of Control may be you had assign some wrong ID or you are using button as a control type.

    Thanx
    Friday, January 4, 2008 1:38 AM
  • It is an EDIT, I checked again.  When it IS NOT read-only, it works fine.  When it IS read-only, it comes across as a BTN.  I think this is a bug.  Anyone know where I can report it at?

     

    Friday, January 4, 2008 6:22 PM
  • Can you Send me your Project ZIP File.May be i can help you.

    Thanx
    Saturday, January 5, 2008 7:02 PM
  • I have a same issue.

    VS2008, c++, Standart SDK for windows ce 5.0.

    ---

    Yes, it is 2018 ))

    Thursday, March 29, 2018 10:31 AM