为什么winform不会在运行时更新其宽度和高度?

时间:2012-06-01 15:44:53

标签: .net winforms drawing runtime delphi-prism

我不确定如何准确地说出这个问题,但在我解释问题后你会理解。

我有一个winform,我在其上绘制像矩形,椭圆形,线条等形状,就像下面的winform一样。之后,我将这些形状保存为二进制文件。

enter image description here

但是,如果你注意到我的程序启动时我打开保存的形状,由于一些奇怪的原因,winform保持其设计时宽度和高度,以显示形状看起来被切掉的形状,即使winform有充足的油漆区域可以完全绘制所有形状。

此外,如果我重新加载winform,它会使用当前的宽度和高度来绘制形状。因此,winform看起来就像我期望的那样,如下所示。

enter image description here

我缺少或需要做什么?因此,winform将始终在运行时更新为当前的宽度和高度。

更新:

这是绘制Rectangle的代码。 Rectangle是程序设计中类的对象。因此,每个对象都有自己的绘制方法。因此,在paint事件中,列表中的所有对象都会调用draw方法,如下所示。

method ViewFrmpas.ViewFrmpas_Paint(sender: System.Object; e: System.Windows.Forms.PaintEventArgs);
begin
     myObjects.Draw;
end;

这是我在winform上绘制Rectangle的实际绘制方法。

method TMakerRect.Draw;
var
  outpoints:Array of point;
  inpoints:Array of point;
  r,fr:Rectangle;
  midx,midy:integer;
  theBrush1:HatchBrush;
  theBrush2:SolidBrush;
begin
  r := bounds;
  fr := bounds;

  if (theBrushStyle = HatchStyle.Wave) then
     theBrush1 := new HatchBrush(theBrushStyle,Color.Transparent,color.Transparent)
  else if (theBrushStyle = HatchStyle.ZigZag) then
     thebrush2 := new SolidBrush(FillColor)
  else
     theBrush1 := new HatchBrush(theBrushStyle,FillColor,color.Transparent);

  if (thePen.DashStyle = DashStyle.Custom) then
    thepen.Color := Color.Transparent;

  outpoints := new point[5];
  inpoints := new point[4];
  with r do
  begin
    midx := ((right - left) div 2) + left;
    midy := ((bottom - top) div 2) + top;
    inpoints[0].x := left; inpoints[0].y := top;
    inpoints[1].x := right; inpoints[1].y := top;
    inpoints[2].x := right; inpoints[2].y := bottom;
    inpoints[3].x := left; inpoints[3].y := bottom;
  end;

  if Active then
  begin
    Fill(var fr);
    with fr do
    begin
      outpoints[0].x := r.Left; outpoints[0].y := r.Top;
      outpoints[1].x := left; outpoints[1].y := top;
      outpoints[2].x := right; outpoints[2].y := top;
      outpoints[3].x := right; outpoints[3].y := bottom;
      outpoints[4].x := left; outpoints[4].y := bottom;
    end;

    Scale(var inpoints,4,midx,midy);
    Rotate( var inpoints,4,midx,midy);
    Translate(var inpoints,4);

    Scale(var outpoints,5,midx,midy);
    Rotate( var outpoints,5,midx,midy);
    Translate(var outpoints,5);

    if Visible then
    begin    
        if theBrushStyle = HatchStyle.ZigZag then
            g.FillPolygon(theBrush2,inpoints)
        else 
            g.FillPolygon(thebrush1,inpoints);

      g.DrawPolygon(thepen,outpoints);
    end;
  end
  else
  begin
      outpoints[0].x := r.Left; outpoints[0].y := r.Top;
      outpoints[1].x := r.left; outpoints[1].y := r.top;
      outpoints[2].x := r.right; outpoints[2].y := r.top;
      outpoints[3].x := r.right; outpoints[3].y := r.bottom;
      outpoints[4].x := r.left; outpoints[4].y := r.bottom;

    if theBrushStyle = HatchStyle.ZigZag then
        g.FillPolygon(thebrush2,inpoints)
    else
        g.FillPolygon(theBrush1,inpoints);

    g.DrawPolygon(thepen,outpoints);
  end;
end;

1 个答案:

答案 0 :(得分:4)

      g.FillPolygon(theBrush2,inpoints)

g变量从天而降,目前还不清楚它来自哪里。但从结果来看,你可能错误地提前初始化它,可能是使用CreateGraphics()。这不能正常工作,Graphics对象将从窗口的设备上下文初始化,该上下文表示创建窗口时窗口的大小。调整窗口大小不能再更改Graphics.ClipBounds。

必须使用在Paint事件处理程序中传递的e.Graphics对象。只需将其作为参数传递给Draw()。不仅要确保剪辑边界正确,使双缓冲正常工作也非常重要。