Custom control under modaldialog not repainting when moving dialog

What causes this ? Other controls are shown fine ...

    public CustomControl()
    {
        SetStyle(ControlStyles.ResizeRedraw, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        SetStyle(ControlStyles.UserPaint, true);
    }

The customcontrol is placed in a TableLayoutPanel.

...................

Edit: For clarification: Suppose you have a window-sized control with a small modal dialog form on top. When you move the dialog window, it's like your painting with the window on the control (the borders are painted on the control). The control doesn't repaint itself like other controls do on the same form, ie montcalendar or other custom controls. I can't seem to find the cause of this ?

Small detail of drawing artifact:


Hard to tell from the snippet. The standard mistake is to draw through Control.CreateGraphics() instead of the OnPaint() method. Won't work, Windows lets the OnPaint method run when parts of the control get uncovered. Which wipes out whatever you drew. Another failure mode is deriving from a control that's a wrapper for a native Window control. UserPaint is not supported for these type of controls, the native Windows code has to do the drawing.


It is clear from the screen shot, note how the text is staggered. That's because the OnPaint() override is using the e.ClipRectangle property to figure out where to draw. That value always changes when you slowly drag a window across your control, it only tells you what part of the control needs to be redrawn. It does not tell you where to draw. That has to be based on the control bounds, routinely the rectangle from (0,0) to (ClientSize.Width, ClientSize.Height).

Only ever use e.ClipRectangle to optimize the drawing. Like skipping an expensive drawing detail when it is outside of the clipping rectangle. It is otherwise a small one, Windows is already quite good at clipping automatically.

链接地址: http://www.djcxy.com/p/50302.html

上一篇: 为什么从WM中使用DrawImageUnscaled会导致闪烁

下一篇: 移动对话框时,modaldialog下的自定义控件不会重新绘制