CreateGraphics () Args drawing method and event

I read somewhere that CreateGraphics() will follow these steps for us:

  • Beginpaint
  • Drawing
  • Endpaint

I have my code:

 private void Form1_Load(object sender, EventArgs e) { grFrom = this.CreateGraphics(); grFrom.FillRectangle(Brushes.Red, this.ClientRectangle); } 

There is no red rectangle ... but when I copy the line below into Form1_paint , each thing works correctly.

 grFrom.FillRectangle(Brushes.Red, this.ClientRectangle); 

So the question here is: What is e.Graphics in Form1_paint ?

CreateGraphics or e.Graphics ?

+4
source share
3 answers

Two things:

  • CreateGraphics gives you a graphic that should always Dispose() before exiting. You must put your instruction inside the block you are using.
  • The graphics you draw are only valid until the shape is repainted. In your case, calling this in Form_Load, this happens before the first render and is “discarded”. You should always put this in OnPaint () so that it is “persistent” on the screen, as this will make it redraw when the control is redrawn.
+5
source

The form loading image refers to the form, but then the first regular event of the form drawing is written on top of it, so you will never see it. (Since this happens before you submit the form at all)

So the question here is: what is

e.Graphics in form1_paint?

CreateGraphics or e.Graphics?

I am sure that they are equivalent to what you need, this is a better understanding of the Windows Forms Event Lifecycle.

This answer has related links: WinForms Event Life Cycle

+1
source

You are creating a new graphic object, which is most likely supported by the memory buffer. Graphics objects that you get from e.Graphics are supported by a buffer that represents the area of ​​the screen in the current window (the window is like a Window Handle, not a window with a title, etc.).

You can always beat data from the created graphic object to one of e.Graphics .

I am sure that someone will develop much more than me.

0
source

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


All Articles