ControlDesigner.OnPaint(PaintEventArgs) 方法


当控件设计器在设计图面上绘制关联控件,且 CustomPaint 的值为 true 时调用。Called when the control designer draws the associated control on the design surface, if the CustomPaint value is true.

 virtual void OnPaint(System::Windows::Forms::PaintEventArgs ^ e);
protected virtual void OnPaint (System.Windows.Forms.PaintEventArgs e);
abstract member OnPaint : System.Windows.Forms.PaintEventArgs -> unit
override this.OnPaint : System.Windows.Forms.PaintEventArgs -> unit
Protected Overridable Sub OnPaint (e As PaintEventArgs)



指定用于绘制控件的图形和矩形边界的 PaintEventArgs 对象。A PaintEventArgs object that specifies the graphics and rectangle boundaries used to draw the control.


对于设计图面上的控件上的某些操作,由设计宿主引发 ViewEvent 事件。The ViewEvent event is raised by the design host for certain actions on a control in the design surface. 例如,以下条件引发事件:For example, an event is raised for the following conditions:

  • 用户单击控件。The user clicks the control.

  • 设计宿主请求在设计图面上呈现关联控件的 HTML 标记。The design host requests HTML markup to render the associated control on the design surface.

  • 用户进入或退出控件的模板编辑模式。The user enters or exits template editing mode for the control.

ControlDesigner 类提供了一个用于处理 IControlDesignerView.ViewEvent 事件的默认委托。The ControlDesigner class supplies a default delegate to handle the IControlDesignerView.ViewEvent event. 派生自 ControlDesigner 的类应设置 ViewFlags.CustomPaint 值并重写 OnPaint 方法,以处理设计宿主在设计图面上绘制控件时引发的事件。Classes deriving from ControlDesigner should set the ViewFlags.CustomPaint value and override the OnPaint method to process events that are raised when the design host draws the control on the design surface.

使用 OnPaint 方法,派生类可以处理事件,而无需附加委托。The OnPaint method allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

OnPaint 方法的默认实现将返回,而不执行任何处理。The default implementation of the OnPaint method returns without performing any processing.


重写从 ControlDesigner 类派生的类中的 OnPaint(PaintEventArgs) 方法,以便在设计图面中处理控件的绘制事件。Override the OnPaint(PaintEventArgs) method in a class deriving from the ControlDesigner class to handle paint events for a control in the design surface. 但是,仅当已用 SetViewFlags(ViewFlags, Boolean) 方法设置 CustomPaint 值时,才会发生此事件。However, the event occurs only if the CustomPaint value has been set with the SetViewFlags(ViewFlags, Boolean) method.