C # WinForm automatically scaled based on screen size

Source: Internet
Author: User

1 Form Base class

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.runtime.interopservices;using System.IO Using system.reflection;///<summary>///Usage:///1 new Form form, inherit baseform///2 new form add panel, dock = fill;///3 Ad event Add///</summary>public class baseform:form{#region Property double formwidth, formheight;//initialization size Double X    scale, yscale;////control piece T sit on the mark,??????    dictionary<string, string> controlinfo = new dictionary<string, string> (); #endregion #region-Party Law public void Removevirtualborder (object obj) {MethodInfo MethodInfo = obj. GetType (). GetMethod ("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance |        BindingFlags.InvokeMethod); MethodInfo. Invoke (obj, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, NULL, new object[] {controlstyles.selectable, false}, ApPlication.    CurrentCulture);    }///<summary>///For the original-start? </summary>//<param name= "Crlcontainer" ></param> protected void Getinitinfo (Control crlconta            Iner) {if (crlcontainer.parent = = this) {formwidth = convert.todouble (crlcontainer.width);        Formheight = convert.todouble (crlcontainer.height); } foreach (Control C in Crlcontainer.controls) {if (C.name.trim () = "") Controli Nfo. Add (C.name, (C.left + C.WIDTH/2) + "," + (C.top + C.HEIGHT/2) + "," + C.width + "," + C.height + ",            "+ c.font.size);            if ((c as UserControl) = = null && c.controls.count > 0) getinitinfo (c);        Removevirtualborder (c); }}///<summary>////For the collection of <param///////</summary>///Name= "Crlcontainer" ></para   m> private void Controlschangeinit (Control crlcontainer) {     XScale = (convert.todouble (crlcontainer.width))/formwidth;    Yscale = (convert.todouble (crlcontainer.height))/formheight;    }///<summary>///<param name= "Crlcontainer" ></param> private void Controlschange (Control Crlcontainer) {double[] pos = new double[5];//save? When the Rex before the control piece t off? Key U value μ.                £foreach (Control C in Crlcontainer.controls) {if (C.name.trim () = "") {                if ((c as UserControl) = = null && c.controls.count > 0) controlschange (c); string[] strtmp = Controlinfo[c.name].                Split (', ');                for (int i = 0; i < 5; i++) {Pos[i] = convert.todouble (Strtmp[i]);                } double cwidth = pos[2] * XScale;                Double cheigth = pos[3] * Yscale;                C.left = Convert.ToInt32 (pos[0] * XSCALE-CWIDTH/2); C.top = COnvert.                ToInt32 (pos[1] * YSCALE-CHEIGTH/2);                C.width = Convert.ToInt32 (cwidth);                C.height = Convert.ToInt32 (cheigth);                Double tmp;                if (XScale = = 0 && Yscale = = 0) tmp = 1;                else TMP = Math.min (XScale, Yscale); C.font = new Font (C.font.name, float. Parse ((pos[4] * tmp).            ToString ())); }}} protected override void Onsizechanged (EventArgs e) {base.        Onsizechanged (e); if (Controlinfo.count > 0) {controlschangeinit (this.            Controls[0]); Controlschange (this.        Controls[0]); }} #endregion}






C # WinForm automatically scaled based on screen size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.