我在C#Winform onPaint事件中绘制了一个区域。我不希望在全屏模式下进行这些更改。我试图保存并恢复图形但无法做到,如果我遗漏了某些内容,请告诉我。
//class member
bool mFullSize = false;
GraphicsState transState;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Save translated graphics state.
Graphics gr = e.Graphics;
transState = gr.Save();
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
Region region = new Region(rect);
GraphicsPath path = new GraphicsPath();
path.AddPie(x, y, width, height, 180, 90);
region.Exclude(path);
if (!mFullSize)
{
gr.FillRegion(Brushes.Black, region);
}
else
{
gr.Restore(transState);
}
}
protected override void OnResize(EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
mFullSize = true;
}
else
{
mFullSize = false;
}
}
答案 0 :(得分:0)
为什么不在表单完整大小时返回?无论如何它都是在最大化上绘制的,所以我认为没有必要(重新)存储任何图形状态。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (WindowState == FormWindowState.Maximized) return; // just don't paint the things you don't want to
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
Region region = new Region(rect);
GraphicsPath path = new GraphicsPath();
path.AddPie(x, y, width, height, 180, 90);
region.Exclude(path);
gr.FillRegion(Brushes.Black, region);
}
更新:最大化时问题是OnResize
未被调用。您需要使用OnSizeChanged
或直接在WindowState
方法中查看OnPaint
。
UPDATE2:第一次更新不完全正确(调试消息中的错误)。 OnResize
实际上是被调用的。但您忘记拨打base.OnResize
,因此OnPaint
未被呼叫。
因此请务必在重载中调用base.OnResize
。在您的OnPaint
中,您可以直接查看WindowState
,如果您的表单已最大化,请避免绘画。
protected override void OnResize(EventArgs e)
{
// DON'T FORGET TO CALL THE BASE METHOD
base.OnResize(e);
mFullSize = this.WindowState == FormWindowState.Maximized;
}