SVat's picture

GLControl + Windows Forms problem

Hello! Sorry for my english. I use Google Translate.
Windows Forms Application + GLControl example:

using System;
using System.Drawing;
using System.Windows.Forms;
 
using OpenTK.Graphics.OpenGL;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            glControl1_Resize(this, EventArgs.Empty);   // Ensure the Viewport is set up correctly
            GL.ClearColor(Color.Crimson);
        }
 
        private void redButton_Click(object sender, EventArgs e)
        {
            GL.ClearColor(Color.Crimson);
            glControl1.Invalidate();
        }
 
        private void greenButton_Click(object sender, EventArgs e)
        {
            GL.ClearColor(Color.ForestGreen);
            glControl1.Invalidate();
        }
 
        private void blueButton_Click(object sender, EventArgs e)
        {
            GL.ClearColor(Color.RoyalBlue);
            glControl1.Invalidate();
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            glControl1.MakeCurrent();
 
            GL.Clear(ClearBufferMask.ColorBufferBit);
            glControl1.SwapBuffers();
        }
 
        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (glControl1.ClientSize.Height == 0)
                glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1);
 
            GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height);
        }
 
        private void glControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}

When you run an error:

Inline Images
glcontrol winforms

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

I just tested this code and it appears to be running correctly. Are you using the latest version opentk-2013-11-22.zip?

There is a step-by-step tutorial here: http://www.opentk.com/doc/chapter/2/glcontrol

SVat's picture

I used the nightly build opentk-2012-03-15.exe
Now install opentk-2013-11-22.zip, now everything works :)
Thank you!

the Fiddler's picture

You are welcome :)