WinForms Different DPI Layouts

前端 未结 3 991
挽巷
挽巷 2020-12-18 09:33

Somehow forms and controls created through Visual Studio and the designer have the great ability to scale themselves depending on the current DPI/font size of Windows. One p

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 10:16

    I solved the same issue, with controls created at runtime as needed, by doing what designer.cs does:

    void CreateRuntimePanel()
    {
        //instantiate controls here...
    
        //suspend layouts
        //begin inits
    
        this.SuspendLayout();
    
        //set control properties here
    
        //before adding any control to form's Controls collection, do this
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    
        //add controls to form's Controls collection here
    
        //resume layouts
        //end inits
    
        this.ResumeLayout(false);  
    }
    

提交回复
热议问题