I am not really sure how to accurately word this question, but you will understand after I explain what the problem is.
I have a winform on which I draw shapes like rectangle, oval, line, etc just like the winform below. After that, I save those shapes into a binary file.
However, if you notice that when my program starts up and I open the saved shapes, for some odd reason the winform is keeping its design-time width and height to display the shapes that the shapes look chopped off even though winform has plenty of paint area to draw all the shapes completely.
Furthermore, if I reload the winform, it uses its current width and height to draw the shapes. Thus, the winform looks like the way I am expecting it too as below.
What am I missing or need to do? So, the winform will update to its current width and height during runtime all the time.
UPDATE:
Here is a code for drawing Rectangle. Rectangle is an object of a class in my program design. Thus, each object has its own draw method. So, within paint event all the objects in a list calls draw method as follows.
method ViewFrmpas.ViewFrmpas_Paint(sender: System.Object; e: System.Windows.Forms.PaintEventArgs);
begin
myObjects.Draw;
end;
Here is my actual draw method for drawing Rectangle on a winform.
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;
The
g
variable fell from the sky, it is unclear where it came from. But judging from the outcome, you probably made the mistake of initializing it early, possibly by using CreateGraphics(). This does not work properly, the Graphics object will be initialized from the window's device context that represents the size of the window at the time you created it. Resizing the window cannot change the Graphics.ClipBounds anymore.It is essential that you use the e.Graphics object that was passed in the Paint event handler. Simply pass it as an argument to Draw(). Not just to ensure the clip bounds are correct, it is also very important to make double-buffering work properly.