The function is triggered only when the form window is resized.

public partial class MainForm : Form { Timer timer; bool flag; public MainForm() { InitializeComponent(); simpleOpenGlControl1.InitializeContexts(); } private void timerFunc(object sender, EventArgs e) { uint texture_text = 0; Gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); Gl.glClear(Gl.GL_COLOR_BUFFER_BIT); // ! Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ var text_bmp = new Bitmap(simpleOpenGlControl1.Width, simpleOpenGlControl1.Height); // ! Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΠΎΠ²Π΅Ρ€Ρ…Π½ΠΎΡΡ‚ΡŒ рисования GDI+ ΠΈΠ· ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Graphics gfx = Graphics.FromImage(text_bmp); // ! ΠžΡ‡ΠΈΡ‰Π°Π΅ΠΌ ΠΏΠΎΠ²Π΅Ρ€Ρ…Π½ΠΎΡΡ‚ΡŒ рисования Ρ†Π²Π΅Ρ‚ΠΎΠΌ gfx.Clear(Color.Beige); // ! Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΡˆΡ€ΠΈΡ„Ρ‚ var font = new Font(FontFamily.GenericSerif, 14.0f); // ! ΠžΡ‚Ρ€ΠΈΡΠΎΠ²Ρ‹Π²Π°Π΅ΠΌ строку Π² ΠΏΠΎΠ²Π΅Ρ€Ρ…Π½ΠΎΡΡ‚ΡŒ рисования (Π² ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ) if(flag) { gfx.DrawString("ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠœΠΈΡ€!", font, Brushes.Black, new PointF(30, 30)); flag = false; } else { gfx.DrawString("ΠŸΡ€ΠΎΡ‰Π°ΠΉ, ΠœΠΈΡ€!", font, Brushes.Black, new PointF(30, 30)); flag = true; } // ! ВытягиваСм Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ BitmapData data = text_bmp.LockBits(new Rectangle(0, 0, text_bmp.Width, text_bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // ! Π’ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ тСктстурированиС Gl.glEnable(Gl.GL_TEXTURE_2D); // ! Π“Π΅Π½Π΅Ρ€ΠΈΡ€ΡƒΠ΅ΠΌ Ρ‚Π΅ΠΊΡ‚ΡƒΡ€Π½Ρ‹ΠΉ ID Gl.glGenTextures(1, out texture_text); // ! Π”Π΅Π»Π°Π΅ΠΌ тСкстуру Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture_text); // ! НастраиваСм свойства тСкстуры Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_REPLACE); // ! ΠŸΠΎΠ΄Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Π² тСкстуру Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, text_bmp.Width, text_bmp.Height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, data.Scan0); text_bmp.UnlockBits(data); // ! Π’ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ смСшиваниС Gl.glEnable(Gl.GL_BLEND); Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA); // ! Π”Π΅Π»Π°Π΅ΠΌ тСкстру Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture_text); // ! РисуСм ΠΏΡ€ΡΠΌΠΎΠ³ΡƒΠ»ΡŒΠ½ΠΈΠΊ с нашСй Ρ‚Π΅ΠΊΡ‚ΡƒΡ€ΠΎΠΉ, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ тСкст Gl.glBegin(Gl.GL_QUADS); Gl.glTexCoord2f(0f, 1f); Gl.glVertex2f(-1.0f, -1.0f); Gl.glTexCoord2f(1f, 1f); Gl.glVertex2f(1f, -1.0f); Gl.glTexCoord2f(1f, 0f); Gl.glVertex2f(1f, 1f); Gl.glTexCoord2f(0f, 0f); Gl.glVertex2f(-1.0f, 1f); Gl.glEnd(); Gl.glFlush(); Invalidate(); } void MainForm_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); this.Dispose(); } void MainForm_Load(object sender, EventArgs e) { timer = new Timer(); timer.Interval = 100; timer.Tick += timerFunc; timer.Start(); } } 
  • @rdorn Thank you. - SVD102
  • @rdorn Yes. Right. - SVD102
  • everything is in response, comments are deleted - rdorn

1 answer 1

The Control.Invalidate() method, according to the source code, causes a redraw only the control to which it is applied. In order to redraw child controls, you need to explicitly call its overloaded version Invalidate(true) , or apply it to the control that needs to be redrawn.

Separately, I want to note that if the control container contains many child controls, it is highly undesirable to use the first option, since This can cause interface hang. It is more correct to use Invalidate() for the control in which the changes occurred.

In extreme cases, cause Invalidate overload (Rectangle rc, bool invalidateChildren) indicating the rectangular area that needs to be redrawn, this is not much better than Invalidate(true) , but if there is no direct reference to the necessary control, it allows the redrawing algorithm to be more precisely focused and reduce GDI + overhead .