-
January 3rd, 2011, 02:56 AM
#1
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
-
January 3rd, 2011, 06:19 AM
#2
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
-
January 3rd, 2011, 11:34 PM
#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);
-
January 4th, 2011, 08:10 AM
#4
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
-
January 14th, 2011, 02:06 AM
#5
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 ?
-
January 14th, 2011, 01:13 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|