Winform DPI 放大修复

        [STAThread]
        static void Main()
        {
#if !DEBUG
            if (!IsAdmin())UpAdmin();
#endif
            if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();

        private static bool IsAdmin()
        {
            return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
        }

        private static void UpAdmin()
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = Application.ExecutablePath;
            psi.Verb = "runas";
            try
            {
                Process.Start(psi);
                Application.Exit();
                Environment.Exit(0);
            }
            catch (Exception eee)
            {
                Trace.WriteLine(eee.Message);
            }
        }

        #region Windows 窗体设计器生成的代码

        private void InitializeComponent()
        {
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

        }

        #endregion
posted @ 2022-07-17 23:01  月渊  阅读(65)  评论(0)    收藏  举报