[英]OnPaint method not called for Child Controls - WinForms
我正在使用控件。 我在其中添加了两个面板,因为我想在一个控件上绘制多个面板。 我覆盖了这两个面板的 OnPaint() 方法,但是添加到控件的第一个面板的 OnPaint() 方法单独调用,我添加到控件的第二个面板没有调用 OnPaint() 方法。
注意:我已使用以下代码重绘表面以避免闪烁问题。 如果我从我的示例中删除以下代码,则 OnPaint() 方法会调用第二个面板,但在第二个面板中绘制的元素不在 Visual 中。 (即)不显示。
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.Selectable |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
如何在单个控件上绘制多个面板? 提前致谢。
代码:
public Class VContainer : Panel
{
public CPanel CPanel;
public SPanel SPanel;
public VContainer()
{
this.CPanel = new CPanel();
this.SPanel = new Spanel();
this.Controls.Add(Cpanel); **// first added Panel**
this.Controls.Add(SPanel);
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.CPanel.Invalidate();
this.SPanel.Invalidate();
this.SPanel.Update();
}
}
public class CPanel : Panel
{
public CPanel()
{
// Used to redraw the surface to avoid flickering issues
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
}
**// OnPaint() called since Cpanel is added first to the VContainer**
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, new Rect(0,0,50,50));
base.OnPaint(e);
}
}
public class SPanel : Panel
{
public SPanel()
{
// Used to redraw the surface to avoid flickering issues
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
}
**// OnPaint() method is not called while invalidating the Panel since the Spanel is added as second control to VContainer**
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Green, new Rect(0,0,50,50));
base.OnPaint(e);
}
}
文卡特桑,
您的两个控件CPanel
和SPanel
位于同一位置。
因此,一个重叠另一个,Winforms 不会绘制下面的一个。
改变控件的位置和大小,你会看到一些东西:
public VContainer()
{
this.CPanel = new CPanel();
CPanel.Top = 0;
CPanel.Left = 0;
CPanel.Width = 50;
CPanel.Height = 50;
this.Controls.Add(CPanel); // first added Panel**
this.SPanel = new SPanel();
SPanel.Top = 50;
SPanel.Left = 50;
SPanel.Width = 50;
SPanel.Height = 50;
this.Controls.Add(SPanel);
}
顺便说一下,更改 OnPaint 方法的指令顺序。
因为如果基类进行一些绘图,它将涵盖您所做的工作:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(0, 0, 50, 50));
}
工作解决方案的完整来源: http : //1drv.ms/1NtIttW
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.