Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I changed my CButtons to CMFCButtons and the behavior is different when they are BS_OWNERDRAW:

C#
void CPlayerPane::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	int s = (lpDrawItemStruct->itemState & ODS_SELECTED);


With CButtons, OnDrawItem() gets called only once with (s != 0), indicating that the user has clicked on the button ... and I can draw the "on" bitmap.

With CMFCButtons, OnDrawItem() gets called *twice* with (s != 0)when the user clicks the button (left button down), and twice again with (s != 0) when the user lets off the mouse (left button up). This is playing havoc with my button drawing logic.

Anyone know why these two types of buttons should behave differently?

Thx.
Posted

1 solution

I don't know why the handler is called multiple times. But you should check also the lpDrawItemStruct->itemAction member to know what kind of drawing is requested. Possible values are ODA_SELECT, ODA_FOCUS, and ODA_DRAWENTIRE.

Edit: Maybe this will show you the reason for the multiple calls.
 
Share this answer
 
v2
Comments
Chris___808 2-Jan-12 14:25pm    
Thanks for the reply Jochen. For the following:

// when selected
if (lpDrawItemStruct->itemState & ODS_SELECTED) {
printf ("...", lpDrawItemStruct->itemAction);
}

... it prints itemAction=1 (ODA_DRAWENTIRE) on left-button-down, and itemAction=1 again on left-button-up. ODA_SELECT is never set.

Interestingly I did find the following on the Web:

I have an owner-drawn CButton-derived class. In my overridden DrawItem method, I have some logic based on whether the ODA_SELECT flag is set in the itemAction of the DRAWITEMSTRUCT object. I've noticed that if my application is setup for XP-style themes (I have a .manifest file in my executable directory), the ODA_SELECT flag is not part of the itemAction when the button is pressed. It does work when I delete my .manifest file. Would anyone know why this might be?
Jochen Arndt 3-Jan-12 3:40am    
The text from the web:
With classic buttons, it may be sufficient to redraw only the buttons border when the select state changes, while with themed buttons the complete button must be redrawn (changed background color).

With themes enabled, there are also additional draw requests for hovering.

I suggest to trace actions and states in your handler:
TRACE("CPlayerPane::OnDrawItem: action %#X, state %#X\n", lpDrawItemStruct->itemAction, lpDrawItemStruct->itemState);
Chris___808 7-Jan-12 19:48pm    
I tried the TRACE, and I found out that by changing the declaration from CButton to CMFCButton, the messages sent to DrawItem() are different when I click and then let go. CMFCButton messages are too complicated so I think I will stick to CButton.

Thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900