C# Win.Form - 加载后图形消失
C# Win.Form - graphics disappear after load
我正在使用用户控件来显示两个日期之间的时间。我的问题是当应用程序启动时 运行 或每次我 Refresh() 用户控件时,所有图形都会消失,直到调整表单大小。
观察窗体在刷新时的行为,用户控件在消失前在屏幕上显示了几帧。
我一头雾水,希望你能帮我解开这个谜。
此致,
马蒂亚斯
PS:这是我的用户控件代码:
public partial class CalendarView : UserControl
{
[Category("Behavior")]
//these reads the start and end date for the datastrip from the parent form
public DateTime StartDateTime (...)
public DateTime EndDateTime (...)
public int Space
{
get
{
double totalDays = (EndDateTime - StartDateTime).TotalDays;
if (totalDays == 0) { totalDays = 1; }
return (int)(GraphicsWidth / totalDays);
}
}
private int GraphicsWidth
{
get
{
return panelDateTimeStrip.Width - 2;
}
}
public CalendarView()
{
InitializeComponent();
}
private void drawDates()
{
Pen darkGray = new Pen(SystemColors.ControlDarkDark);
Pen lightGray = new Pen(Color.LightGray);
Pen gray = new Pen(Color.Gray);
SolidBrush brush = new SolidBrush(SystemColors.ControlDarkDark);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
int graphicsHeight = panelDateTimeStrip.Height - 2;
Graphics graphics = panelDateTimeStrip.CreateGraphics();
//<-----drawing all graphics------>\
graphics.Dispose();
darkGray.Dispose();
lightGray.Dispose();
gray.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
drawDates();
base.OnPaint(e);
}
private void CalendarView_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void CalendarView_Load(object sender, EventArgs e)
{
drawDates();
}
}
评论几乎回答了这个问题,但由于还没有人发布答案,因此您应该如何更改代码:
private void drawDates(Graphics graphics)
{
// create pens and brushes
// do not CreateGraphics, use the parameter instead
//<-----drawing all graphics------>\
// dispose pens and brushes; do not dispose graphics!
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
drawDates(e.Graphics);
}
请注意,您在自己绘制之前调用了 base.OnPaint
,并且您不再使用 CreateGraphics
。
我正在使用用户控件来显示两个日期之间的时间。我的问题是当应用程序启动时 运行 或每次我 Refresh() 用户控件时,所有图形都会消失,直到调整表单大小。
观察窗体在刷新时的行为,用户控件在消失前在屏幕上显示了几帧。
我一头雾水,希望你能帮我解开这个谜。
此致, 马蒂亚斯
PS:这是我的用户控件代码:
public partial class CalendarView : UserControl
{
[Category("Behavior")]
//these reads the start and end date for the datastrip from the parent form
public DateTime StartDateTime (...)
public DateTime EndDateTime (...)
public int Space
{
get
{
double totalDays = (EndDateTime - StartDateTime).TotalDays;
if (totalDays == 0) { totalDays = 1; }
return (int)(GraphicsWidth / totalDays);
}
}
private int GraphicsWidth
{
get
{
return panelDateTimeStrip.Width - 2;
}
}
public CalendarView()
{
InitializeComponent();
}
private void drawDates()
{
Pen darkGray = new Pen(SystemColors.ControlDarkDark);
Pen lightGray = new Pen(Color.LightGray);
Pen gray = new Pen(Color.Gray);
SolidBrush brush = new SolidBrush(SystemColors.ControlDarkDark);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
int graphicsHeight = panelDateTimeStrip.Height - 2;
Graphics graphics = panelDateTimeStrip.CreateGraphics();
//<-----drawing all graphics------>\
graphics.Dispose();
darkGray.Dispose();
lightGray.Dispose();
gray.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
drawDates();
base.OnPaint(e);
}
private void CalendarView_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void CalendarView_Load(object sender, EventArgs e)
{
drawDates();
}
}
评论几乎回答了这个问题,但由于还没有人发布答案,因此您应该如何更改代码:
private void drawDates(Graphics graphics)
{
// create pens and brushes
// do not CreateGraphics, use the parameter instead
//<-----drawing all graphics------>\
// dispose pens and brushes; do not dispose graphics!
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
drawDates(e.Graphics);
}
请注意,您在自己绘制之前调用了 base.OnPaint
,并且您不再使用 CreateGraphics
。