Centering the display as a .NET UserControl

I would like use this in a user control with .NET. However, the CSharp examples are using MDI forms.

When I zoom all as a user control, the result seems off-center. Please see if I am missing something.

Following is the description of the attached files:
C++ CLR DLL:
1. OcctClrClsDotNetFrameworkLib.h: CLR C++ code based on provided example with OCCT. This is compiled targeting .NET Framework 4.8. (attached)

.NET 4.8 C# UserControl:
2. OcctControl.cs: The user control referring the above library to be used in a windows form.
3. OcctControl.Designer.cs: Designed code accompanying the above user control.
.NET 4.8 C# Windows Form:
4. Form1.cs: The form using the user control.
5. Form1.Designer.cs: Designer code for the above form.

OcctControl.cs:

using System;
using System.Windows.Forms;
using OcctClrClsDotNetFrameworkLib;

namespace OcctWindowsFormsDotNetFrameworkControlLibrary
{
  public partial class OcctControl : UserControl
  {
    private readonly OCCTProxy _occtProxy;
    public OcctControl()
    {
      InitializeComponent();

      if (!DesignMode)
      {
        _occtProxy = new OCCTProxy();
      }
    }

    protected override void OnLoad(EventArgs e)
    {
      if (!DesignMode)
      {
        _occtProxy.InitOCCTProxy();
        _occtProxy.InitViewer(Handle);
      }

      base.OnLoad(e);
    }

    protected override void OnResize(EventArgs e)
    {
      if (!DesignMode && _occtProxy != null)
      {
        _occtProxy.UpdateView();
        _occtProxy.UpdateCurrentViewer();
        _occtProxy.RedrawView();
      }

      base.OnResize(e);
    }

    public void DrawBox()
    {
      _occtProxy.DrawBox();
      _occtProxy.UpdateCurrentViewer();
    }
  }
}

OcctControl.Designer.cs:

namespace OcctWindowsFormsDotNetFrameworkControlLibrary
{
    partial class OcctControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
        }

        #endregion
    }
}

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OcctWindowsFormsDotNetFrameworkControlLibrary;

namespace OcctDotNetFrameworkWinForm
{
  public partial class Form1 : Form
  {
    private OcctControl occtControl1;
    public Form1()
    {
      InitializeComponent();

      if (!DesignMode)
      {
        this.occtControl1 = new OcctWindowsFormsDotNetFrameworkControlLibrary.OcctControl();
        this.SuspendLayout();
        this.occtControl1.SuspendLayout();
        this.occtControl1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.occtControl1.Location = new System.Drawing.Point(400, 100);
        this.occtControl1.Name = "occtControl1";
        this.occtControl1.Size = new System.Drawing.Size(600, 450);
        this.occtControl1.TabIndex = 1;
        this.Controls.Add(this.occtControl1);
        this.occtControl1.ResumeLayout(false);
        this.ResumeLayout(false);
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      occtControl1.DrawBox();
    }
  }
}

Form1.Designer.cs:

namespace OcctDotNetFrameworkWinForm
{
  partial class Form1
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.groupBox1 = new System.Windows.Forms.GroupBox();
      this.button1 = new System.Windows.Forms.Button();
      this.groupBox1.SuspendLayout();
      this.SuspendLayout();
      // 
      // groupBox1
      // 
      this.groupBox1.Controls.Add(this.button1);
      this.groupBox1.Dock = System.Windows.Forms.DockStyle.Left;
      this.groupBox1.Location = new System.Drawing.Point(0, 0);
      this.groupBox1.Name = "groupBox1";
      this.groupBox1.Size = new System.Drawing.Size(200, 450);
      this.groupBox1.TabIndex = 0;
      this.groupBox1.TabStop = false;
      this.groupBox1.Text = "groupBox1";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(33, 71);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(75, 23);
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // Form1
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(800, 450);
      this.Controls.Add(this.groupBox1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.groupBox1.ResumeLayout(false);
      this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Button button1;
  }
}


DKGH's picture

Figured it out:
1. First a call to ZoomAllView() is missing above after DrawBox().
2. Second _myView()->SetWindow() actually sets the view to the controls parent. So all one needs to do is to wrap the control in a panel and that's it.