Home > Software engineering >  drawitem
drawitem

Time:11-23

Encountered in the study to rewrite the DrawItem (), but have a WM_DRAWITEM message again, they are what kind of relationship do,

If we want to rewrite a named CButton CMyButton, we can rewrite CMyButton DrawItem () function to achieve our

Demand, but CMyButton: : the DrawItem () is called when what? It is in its host type OnDrawItem () is called,

OnDrawItem (int nIDCtl, LPDRAWITEMSTRUCT LPDRAWITEMSTRUCT) is a function corresponding to WM_DRAWiTEM,

Host class could be determined according to the nIDCtl is which child controls, actually we can pair in OnDrawItem function controls to paint, but there are a lot of

The child controls of looks bad, so we should be drawn in a subclass of DrawItem to subclass, for example CMyButton: : DrawItem, so can

Such understanding, OnDrawItem is to draw the child controls in the window, because its entrance parameter LPDRAWITEMSTRUCT into different child control phase

Pass parameters, moreover, you have to put the word control is set to "painting" type, will call to OnDrawItem,

When the draw button (the owner - the draw button), the drop-down list box (combo box), list box, list box) visual properties, or menu changes,

Framework for their owner call OnDrawItem (sending WM_DRAWITEM), the host class call subclasses of DrawItem (WM_DRAWITEM messages sent),

We can override the subclass DrawItem can draw controls, you need not all set to the painting types of controls will be called the parent window OnDrawItem,

For example of the ListBox from a picture, you must override the CListBox DrawItem method and MeasureItem method can, but as menu, button, etc of the painting will call

OnDrawItem in SDK, WM_DRAWITEM subclass is impossible, can in the MFC, this is the class designer design (reflection), it is really good,

In study, and there was a message also class is invoked by the host, it is WM_CTRCOLOR, the news is in the child controls will be painting, to host

Class to send, the host class using the emission mechanism to subclass a chance to deal with again, OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)

PDC, pWnd is associated with a subclass, here you can set, foreground color, background color, brush type, font, and so on, but it can't change the interface framework elements,

This is DrawItem capable,

If at the same time have the DrawItem (subclass), OnDrawItem (hosts), OnCtlColor category (the host), and they call order is:

OnCtlColor OnDrawItem, DrawItem,

If we again at the same time corresponding subclass the WM_PAINT message, perhaps this OnPaint some in internal processing, determine whether the painting class to decide whether to host

Send WM_DRAWITEM, so if the response the WM_PAINT subclass will not class WM_DRAWITEM messages sent to the host, you must complete a subclass of all painting

Work, if a subclass is a list box, is very troublesome, then call order is OnCtlColor, OnPaint,

Before sending a WM_PAINT message, always sends a WM_ERASEBACK message first, we are here in a background image,

For us to control the draw at ordinary times, described above about, there is a problem that the CView, namely OnPaint and ontouch relations,

This is very simple, actually the CView: : OnPaint () source is as follows:

Void the CView: : OnPaint ()

{

CPaintDC dc (this);

OnPrepareDC (& amp; Dc);

Ontouch (& amp; Dc);

}

CodePudding user response: