Grundsätzlich versuche ich ein gControl-Fenster auf Widows Forms in Echtzeit zu erstellen. Ich mache das, weil ich mehrere Fenster verwenden werde, und wahrscheinlich ihre Position neu justieren, und ich möchte das nicht manuell machen.Erstellen von glControl Window Realtime
Ich verstehe nicht, warum mein Code nicht funktioniert. Hier ist es:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
namespace Draw3Dv01wf
{
public partial class Form1 : Form
{
GLControl renderCanvas1;
int winX, winY, winW, winH;
public Form1()
{
// required method for designer support
InitializeComponent();
}
// load event handler
private void Form1_Load(object sender, EventArgs e)
{
// create and setup gl.control windows
this.renderCanvas1 = new GLControl();
//this.SuspendLayout();
winX = this.Location.X; winY = this.Location.Y;
winW = this.Width; winH = this.Height;
this.renderCanvas1.BackColor = System.Drawing.Color.CadetBlue;
this.renderCanvas1.Location =
new System.Drawing.Point(winX + 50, winY + 50);
this.renderCanvas1.Name = "renderCanvas1";
this.renderCanvas1.Size = new System.Drawing.Size(winW/3, winH/2);
this.renderCanvas1.TabIndex = 1;
this.renderCanvas1.VSync = false;
this.renderCanvas1.Load +=
new System.EventHandler(this.renderCanvas_Load);
this.renderCanvas1.Paint +=
new System.Windows.Forms.PaintEventHandler(
this.renderCanvas_Paint);
//renderCanvas1.Paint += renderCanvas_Paint;
//this.ResumeLayout(false);
}
private void renderCanvas_Paint(object sender, PaintEventArgs e)
{
GL.Viewport(winX + 20, winY + 25, Width, Height);
// Clear the render canvas with the current color
GL.Clear(
ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
GL.Flush();
renderCanvas1.SwapBuffers();
}
private void renderCanvas_Load(object sender, EventArgs e)
{
// Specify the color for clearing
GL.ClearColor(Color.SkyBlue);
}
}
}
Ich sehe nur die Windows-Form. Das glControl-Fenster wird nicht angezeigt. Allerdings, wenn ich die glControl zum Formular manuell hinzufügen, und fügen Sie diesen Code:
//____________________________________________
private void glControl1_Load(object sender, EventArgs e)
{
// Specify the color for clearing
GL.ClearColor(Color.SkyBlue);
this.Paint += new PaintEventHandler(glControl1_Paint);
}
private void glControl1_Paint(object sender, PaintEventArgs e)
{
GL.Viewport(this.Location.X, this.Location.Y, Width, Height);
// Clear the render canvas with the current color
GL.Clear(
ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
GL.Flush();
glControl1.SwapBuffers();
}
//________________________________________________________
... In diesem Fenster wird auf dem Formular oben.
Der Code in meinem Programm ist nicht anders als der Code im Designer, so dass ich völlig verwirrt bin.
Dies ist der Designer-Code:
this.glControl1 = new OpenTK.GLControl();
this.SuspendLayout();
//
// glControl1
//
this.glControl1.BackColor = System.Drawing.Color.Black;
this.glControl1.Location = new System.Drawing.Point(385, 12);
this.glControl1.Name = "glControl1";
this.glControl1.Size = new System.Drawing.Size(476, 284);
this.glControl1.TabIndex = 0;
this.glControl1.VSync = false;
this.glControl1.Load += new System.EventHandler(this.glControl1_Load);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Desktop;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(1264, 681);
this.Controls.Add(this.glControl1);
this.HelpButton = true;
this.MaximumSize = new System.Drawing.Size(3840, 2160);
this.MinimumSize = new System.Drawing.Size(640, 480);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Draw3D";
this.ResumeLayout(false);
Der Designer-Code diese Zeile hat 'this.Controls.Fügen Sie (this.glControl1) hinzu: 'was Ihnen fehlt. Ohne es ist es keine Überraschung, dass das GL-Fenster nicht angezeigt wird. Sie erstellen es (außerhalb des Bildschirms) und fügen es nie einem sichtbaren Formular hinzu. –
Danke. Das habe ich total vermisst, aber ich muss noch etwas vermissen, denn obwohl ich diesen Code "this.Controls.Add (this.renderCanvas1);" Im Ereignishandler Form1_Load wird das Fenster immer noch nicht angezeigt. Siehst du noch etwas, das ich übersehen habe? – Jillinger