简体   繁体   English

覆盖OnPaint

[英]Override OnPaint

如果我重写OnPaint并在控件上绘制一个正方形,当我在设计器中预览它时,如何显示e.graphics.draw ...?

http://msdn.microsoft.com/en-us/magazine/cc164048.aspx http://msdn.microsoft.com/zh-CN/magazine/cc164048.aspx

http://msdn.microsoft.com/en-us/magazine/cc164145.aspx http://msdn.microsoft.com/en-us/magazine/cc164145.aspx

While you could manually register with Control.OnPaint to add your design time UI, you'll find that overriding OnPaintAdornments is a better option because it is only called after the control's design-time/run-time UI is painted, letting you put the icing on the cake (see Figure 20). 虽然您可以手动向Control.OnPaint注册以添加设计时用户界面,但您会发现覆盖OnPaintAdornments是一个更好的选择,因为仅在绘制控件的设计时/运行时用户界面后才调用该方法,锦上添花(见图20)。 Simply adding DesignerAttribute to the ClockControl class completes the association: Copy Code 只需将DesignerAttribute添加到ClockControl类即可完成关联:复制代码

[ Designer(typeof(ClockControlDesigner)) ] class ClockControl : Control { ... } [Designer(typeof(ClockControlDesigner))类ClockControl:控件{...}

If you're trying to draw a focus rectangle for you control, and have it be visible when manipulating your control after it has been added to a form, then Gaijin42's approach should work. 如果您想为控件绘制一个焦点矩形,并且在将控件添加到表单后对其进行处理时可以看到它,那么Gaijin42的方法应该可以使用。

If you're trying to see what your custom drawn control looks like... 如果您尝试查看自定义绘制控件的外观...

  • If you've overriden the OnPaint method in the control, you should see the results of that OnPaint when you look at your control after it has been added to a form (or another control). 如果你已经被覆盖的的OnPaint的控制方法中,你应该看到的结果OnPaint ,当你在你的控制,已添加到窗体(或其他控件)之后。

  • If you're trying to see the results of the Paint when looking at the designer for the control itself, I don't think that is possible (at least it wasn't with VS2005): 如果您在查看控件本身的设计器时尝试查看Paint的结果, 我认为这是不可能的 (至少在VS2005中不是):

    Unfortunately this is not possible. 不幸的是,这是不可能的。 While you're writing the behaviours of the user control the code isn't compiled and the designer actually uses an ordinary user control to host the child controls. 在编写用户控件的行为时,不会编译代码,而设计人员实际上使用普通的用户控件来承载子控件。 The OnPaint override etc will not run. OnPaint覆盖等将不会运行。

    To debug the overrides in your control you need to run it in some sort of host. 要在控件中调试替代,您需要在某种主机中运行它。 VS2005 provides a new control host for this purpose but I find that even this is not sufficient sometimes. VS2005为此提供了一个新的控制主机,但我发现有时这还不够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM