简体   繁体   中英

Override OnPaint

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

http://msdn.microsoft.com/en-us/magazine/cc164048.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). Simply adding DesignerAttribute to the ClockControl class completes the association: Copy Code

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

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.

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).

  • 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):

    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.

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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