The image is duplicated at the maximum magnification of the window

I am developing a HangMan game to learn how to use C # with DevExpress. My problem is that I am drawing a message with a size relative to the size of the panel into which I enter it, so that its size is recalculated if the window is resized. My code is as follows:

namespace HangMan { public partial class HangMan : Form { public HangMan() { InitializeComponent(); this.Paint += new System.Windows.Forms.PaintEventHandler(HangMan_Paint); } void drawHangPost() { //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 Graphics g = pnlHang.CreateGraphics(); 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)); } private void HangMan_Paint(object sender, EventArgs e) { drawHangPost(); } } } 

This works fine for me, and the size of the drawing changes. However, when I manually resize the window, a new post is drawn at each step, so many of them appear. If I maximize the window, two columns of two different sizes will appear.

However, when I minimize the window and open it again, only the correct drawing remains. Is there a way to orient previous drawings when a new one is drawn?

Should I include some kind of erinization command to write after InitializeComponent () ?

+5
source share
2 answers

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.

+4
source

Your problem is that you use the same control over and over again. A simple solution would be to fill in the rectangle that draws the previous image. This will make the frame flash often, so what I do to solve it is to create a new bitmap and draw it. Then I draw a compiled bitmap in the panel.

 public void Draw(Graphics g){ Bitmap canvas = new Bitmap(pnlHang.Width, pnlHang.Height); Graphics canvg = Graphics.FromImage(canvas); canvg.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), pnlHang.Size); //Draw using canvg canvg.Dispose(); g.DrawImage(canvas, new Rectangle(new Point(0, 0), pnlHang.Size)); } 
-1
source

Source: https://habr.com/ru/post/1242639/


All Articles