Search code examples
c#.netwpfwinformswinforms-interop

Ensuring Winform control stays within boundary of WindowsFormsHost inside WPF control


I have a Winform control, inside a Windowsformshost, inside a WPF tab control (in a WPF window).

Depending on what the Winform control is drawing, sometimes it will go outside the bounds of the WindowsFormsHost, cutting off some of the images.

Is there any way to ensure the control stays within the bounds, or redraws/scales the images to stay within the boundaries?

EDIT: I was actually able to fix this. Right before I copy the Winform control into the WindowsFormsHost, I did:

                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
                this.PerformAutoScale();

The entire set of images scaled themselves to stay within the Host control.

EDIT 2: Looks like the this.PerformAutoScale(); wasn't necessary. Simply setting the AutoScaleMode took care of it.


Solution

  • I've had success using this with several different forms. The following options have all worked for me to ensure all the Winform controls inside the Form are displayed within the bounds of the WindowsFormsHost:

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
    

    or

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    

    And sometimes (depending what the control contains)

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    

    It hasn't been necessary to call any other sizing functions/methods besides simply setting the AutoScaleMode.