1. ActiveX control rendering process
When ActiveX controls are displayed or re-painted, the rendering process is similar to other applications developed using MFC. However, ActiveX controls are in active and inactive states.
The ActiveX control in the ActiveX control container can be considered as a subwindow. Like other windows, the ActiveX control can be automatically painted when the wm_paint message is received. ActiveX's base class colecontrol processes this message in its onpaint. By default, it calls the ondraw method of your ActiveX control.
When the status of the ActiveX control is inactive, the rendering process is different. In this case, the control window is invalid or does not exist, so it cannot receive pain messages. At this time, the control container will directly call the ondraw method of the control, which is different from the control Painting Process in the active State, because the onpaint member function will not be called.
As discussed in the previous natural section, ActiveX controls depend on two States for rendering. Both methods are called by the ondraw member function by the framework, therefore, you should add the main draw code in the ondraw method.
The ondraw method is used to plot controls. when the control is inactive, when the control container calls ondraw, it transmits the container's device context and the coordinates of the rectangle occupied by the control.
The rectangle passed in by the frame contains the area occupied by the control. If the control is active, the coordinates in the upper left corner of the rectangle are (0, 0 ), the device context is the context of the sub-window containing the control. If ActiveX is inactive, the upper left corner of the rectangle is not necessarily (0, 0), and the device context is the context of the window containing the control.
PS: In ondraw, it is very important not to rely on the coordinates in the upper left corner of the rectangle for plotting. Unexpected results may occur if you depend on the coordinates.
The default ondraw is as follows:
void CMyAxUICtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& /*rcInvalid*/){ if (!pdc) return; // TODO: Replace the following code with your own drawing code. pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); pdc->Ellipse(rcBounds);}
PS: When drawing controls, do not assume the status of the device context that is passed in to the ondraw method, because sometimes the context passed in by the container is not necessarily initialized to the default status, so you need to clearly point out the pen, brushes, colors, fonts and so on in your drawing code.
Ii. code optimization
After the control is successfully re-painted, the next step is to optimize the ondraw method.
By default, the control is repainted as a whole, which is suitable for simple controls. However, in most cases, only some areas that need to be re-painted by the re-painting control can increase the painting speed.
The ondraw method provides a simple optimization method-use the rcinvalid parameter, which indicates the area in the control that needs to be repainted. This area is usually smaller than the control, because it can accelerate the painting process.
3. Use the metadata Drawing Control
In most cases, the ondraw parameter PDC points to the device context of a screen. However, when drawing control images or printing and previewing, the received DC is a special type-Metafile DC ). If the source file DC stores the operation and plays back the painting operation in a later time. Some container applications may select the Metafile DC in the design mode to render the control image.
The drawing request of a Metafile can be generated by the container using two interface functions: iviewobject: Draw, which can be called by a non-Metafile drawing. The other is idataobjet: getdata. When the Metafile DC is passed as a parameter, the MFC framework generates a call request for colecontrol: ondrawmetafile because it is a virtual function, therefore, rewrite this function in the control class for some special processing. By default, colecontrol: ondraw is called.
To ensure that the control can be drawn under both DC types, the drawing method used must support both DC types. You need to realize that the coordinate system is not measured in pixels.
Because the default Implementation of ondrawmetafile calls the ondraw method of the control. So you can only use methods that support both DC types, unless you override the ondrawmetafile method. The following list shows two methods to support DC. For more information, see the CDC class.
For more information, see msdn: http://msdn.microsoft.com/en-us/library/552z1tz3.aspx#_core_the_painting_process_of_an_activex_control