question

NorthernSun-8235 avatar image
0 Votes"
NorthernSun-8235 asked RLWA32-6355 answered

[WinAPI] Curiosity question about owner draw and custom draw

I am curious about why NM_CUSTOMDRAW and WM_DRAWITEM are sent to the parent. Why aren't these two messages/notifications instead sent to the controls themselves? I don't get why we are supposed to do the drawing in the parent. For example, receiving NM_CUSTOMDRAW in a subclassed Listview procedure would make sense to me to easily change some item text colors/fonts etc. without having to draw the control fully the control yourself. Why would it have to happen within the parent?

I would appreciate it if somebody could clarify this for me.

windows-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered Castorix31 commented

In the parent, you can handle the drawing for several windows
Otherwise, you would have to subclass each window

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

But wouldn't just writing one custom window procedure that handles the NM_CUSTOMDRAW be almost the same thing? You could then just give all the subclassed controls this same procedure if they need to be painted the same way. I guess the "problem" would indeed be then that all controls that need a different handling of NM_CUSTOMDRAW to appear different need to be subclassed differently just for the drawing part. So it is basically just for convenience then? I just find it a little counter-intuitive and maybe a bit counter-OOP to let another control (the parent) handle the drawing for it. Thanks for your reply by the way.



0 Votes 0 ·

The drawing is very different for each control (TreeView vs simple Tooltip for example)
It is like WM_COMMAND which handles child controls notifications
For example BN_CLICKED from buttons, it is simpler than subclassing all the buttons

0 Votes 0 ·
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered

The document hasn‘t claimed the reason why the destination of the message must be the HWND of the parent of the control. But the fact is the destination of the message must be the HWND of the parent of the control.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered

The concept of handling a control's notification messages in the control instead of its parent window is implemented in Microsoft's MFC framework. MFC refers to this as message reflection. ( Message Reflection for Windows Controls ) An example would be to derive your own class from the MFC class that encapsulates the Windows control. In the derived class you would include code to handle the NM_CUSTOMDRAW notification that is reflected back to it instead of handling the notification in the parent window.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.