dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Changing background color of CButton

  1. #1
    Join Date
    Oct 2019
    Posts
    36

    Changing background color of CButton

    I want to change the background ground color of CButton, i dont want to change it to CMFCButton and than use SetFaceColor() .For changing background color of CButton my code is as follows:

    MyDialog.h
    Code:
    afx_msg void onDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
    MyDialog.cpp
    Code:
    BEGIN_MESSAGE_MAP (MyDialog, CDialog)
    ON_WM_DRAWITEM()
    END_MESSAGE_MAP 
    
    void MyDialog ::  OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
       If(nIDCtl == IDOK)
        {
           CDC dc;
           dc.Attach(lpDrawItemStruct->hDC);
           RECT rect;
           rect = lpDrawItemStruct->rcItem;
           
          dc.FillSolidRect(&rect,RGB(0,255,0));
    		UINT state=lpDrawItemStruct->itemState;
    
    		if((state & ODS_SELECTED))
    		{
    			dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
    
    		}
    		else
    		{
    			dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
    		}
    
    		dc.SetBkColor(RGB(100,100,255));
    		dc.SetTextColor(RGB(255,0,0));
    		dc.SetBkMode( TRANSPARENT );
    
    		TCHAR buffer[MAX_PATH];
    		ZeroMemory(buffer,MAX_PATH );
    		::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH);
    		dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    
    		dc.Detach();
    	}				
    
    	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
    }
    MyDialog.rc

    DEFPUSHBUTTON "OK" IDOK,10,200,60,18 | BS_OWNERDRAW

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Changing background color of CButton

    And does it work?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2019
    Posts
    36

    Re: Changing background color of CButton

    No.Compiling successfully but color not reflected

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Changing background color of CButton

    Did you debugged this code? Is the OnDrawItem called?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2019
    Posts
    36

    Re: Changing background color of CButton

    I debugged. onDrawItem() in not getting called.Not able to figure where i am doing wrong

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Changing background color of CButton

    I'd recommend you to derive your own class of the CButton (say, CMyButton), declare your button member to be of CMyButton type.
    Then implement the custom or owner drawing within this class.
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2019
    Posts
    36

    Re: Changing background color of CButton

    Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: Changing background color of CButton

    Quote Originally Posted by Beginner_MFC View Post
    Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?
    Yes, of course!
    Victor Nijegorodov

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,554

    Re: Changing background color of CButton

    I showed you how to do it with CMFCButton. There was obviously something simple wrong with your code and suggested you use the debugger to figure out what. Did you?

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)