Thus, the main part of your problem is that you are responding to the Paint event, and then draw on a control that may or may not be invalidated.
Instead, you should subscribe to the panel draw event and draw your picture there with the Graphics object, which is passed into the event arguments. You will also want to handle the panel resize event to nullify it. I assume that you use either snapping or docking to automatically resize the panel when resizing the form. If not, you need to resize it so that the size of the panel changes when the shape is.
public Form1() { InitializeComponent(); pnlHang.Paint += PnlHangPaint; pnlHang.Resize += (sender, args) => pnlHang.Invalidate(); } private void pnlHang_Paint(object sender, PaintEventArgs paintEventArgs) { drawHangPost(paintEventArgs.Graphics); } void drawHangPost(Graphics g) { //Use panel size percentages to draw the post double dWidth = pnlHang.Width; double dHeight = pnlHang.Height; int x1 = (int)Math.Round(0.8 * dWidth); int x2 = (int)Math.Round(0.45 * dWidth); int y1 = (int)Math.Round(dHeight); int y2 = (int)Math.Round(0.23 * dHeight); int xInit = x1; int xFinal = x1 - x2; int yInit = y1; int yMiddle = 10; int yFinal = y2; //Paint Post using (Pen p = new Pen(Color.Brown, 10)) { g.DrawLine(p, new Point(xInit, yInit), new Point(xInit, yMiddle)); g.DrawLine(p, new Point(xInit, yMiddle), new Point(xFinal, yMiddle)); g.DrawLine(p, new Point(xFinal, yMiddle), new Point(xFinal, yFinal)); } }
In addition, every time you create a GDI resource, such as a pen or brush, remember to destroy them! A block using C # is great for you.
source share