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

    Post OwnerDraw buttons

    I have derived a CCustomButton class from CButton and creating ownerdraw buttons from that class. The button state is controlled by a bool (m_bMap1State). Ive also included the DrawItem() function in the derived CButton class (given below). Problem is that after clicking a button when i click any other button; the previous one becomes unpressed. As far as i have analysed, when any other button is clicked; the previous button function [OnBnClickedBtnMap1()] is called again and the code inside it gets executed. . How to resolve this?

    Following is the button click function:-

    Code:
    void CLeftTIDDlg::OnBnClickedBtnMap1()
    {
    	m_bMap1State  = !m_bMap1State;
    
    	if(m_bMap1State == true)
    	{			
    		((CButton*)GetDlgItem(IDC_BTN_MAP1))->SetState(true);			
    	}
    	else if(m_bMap1State == false)
    	{
    		((CButton*)GetDlgItem(IDC_BTN_MAP1))->SetState(false);			
    	}
    }
    
    void CCustomButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    	// TODO:  Add your code to draw the specified item
    
    	  CDC dc;
    	  dc.Attach(lpDrawItemStruct->hDC);     //Get device context object
    
    	  CRect rt;
    	  rt = lpDrawItemStruct->rcItem;        //Get button rect
    
    	  UINT state = lpDrawItemStruct->itemState; //Get state of the button
    	  if ( (state & ODS_SELECTED) )            // If it is pressed
    	  {
    		dc.FillSolidRect(&rt,RGB(22,81,81));
    		dc.DrawEdge(&rt,EDGE_SUNKEN,BF_RECT);   // Draw a sunken face
    		dc.SetBkColor(RGB(22,81,81));
    		dc.SetTextColor(RGB(255,255,0));
    	  }
    	  else
    	  {
    		dc.FillSolidRect(&rt,RGB(162,213,201));
    		dc.DrawEdge(&rt,EDGE_RAISED,BF_RECT);  // Draw a raised face
    		dc.SetBkColor(RGB(162,213,201));
    		dc.SetTextColor(RGB(0,0,0));			 
    	  }
    	
    	  CString strTemp;
    	  GetWindowText(strTemp); // Get the caption which have been set							
    	  dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); // Draw the caption								  	
    	  dc.Detach();
    }
    Last edited by Marc G; January 5th, 2011 at 05:28 AM. Reason: Added code tags

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: OwnerDraw buttons

    The provided information is not enough. What are the buttons' other styles? What does the message map look like? It would be good if you provide the minimal and compilable project that exactly reproduces your issue.

    Problem is that after clicking a button when i click any other button; the previous one becomes unpressed.
    I can conclude from this that your buttons are not usual BS_PUSHBUTTON, but implement some logic similar to checkbox.
    Best regards,
    Igor

  3. #3
    Join Date
    Jan 2011
    Posts
    3

    Re: OwnerDraw buttons

    In fact u r right. I want these buttons to behave like checkboxes. Actually i am creating 24 buttons on a dialog box. I want few out of these to behave like checkbox and few others would have the functionality of a pushbutton. Now push button functionality is achieved; but the checkbox functionality of other buttons is not there because of the problem stated. The buttons are created as under, here is one of them

    CRect rctButton(50, 477, 150, 537);
    m_btnPlanning = new CCustomButton();
    m_btnPlanning->Create(_T("PLANNING"), WS_CHILD | BS_OWNERDRAW , rctButton, this, IDC_BTN_PLANNING);

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: OwnerDraw buttons

    You could try to add a couple of styles more: BS_AUTOCHECKBOX and BS_PUSHLIKE (not sure though about the latter). Autocheckbox style should let you get rid of the internal state variable, as the state can be found by CButton::GetCheck (BST_CHECKED and BST_UNCHECKED).
    Best regards,
    Igor

  5. #5
    Join Date
    Jan 2011
    Posts
    3

    Re: OwnerDraw buttons

    Ive tried the BS_AUTOCHECKBOX and BS_PUSHLIKE but these donot resolve the problem. rather they create a check box in place of the button(which i dont want). I cant find the reason that on pressing a button why the OnButtonClick function of the previous pressed button is called ?

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: OwnerDraw buttons

    I think it is happening not because you are clicking on the other button but when you move focus from your custom drawn button, for example clicking on any other control or on a window of other application.
    Is that correct?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

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