Click here to Skip to main content
15,884,624 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi every one

I need to build windows application work on every screen resolution
now I am using following code
public Form1()
        {

            InitializeComponent();
            DPI_test();
        }
public void DPI_test()
        {
            //Specify Here the Resolution Y component in which this form is designed
            //For Example if the Form is Designed at 800 * 600 Resolution then   
            //DesignerHeight=600
            int i_StandardHeight = 768;
            //Specify Here the Resolution X component in which this form is designed
            //For Example if the Form is Designed at 800 * 600 Resolution then  
            //DesignerWidth=800
            int i_StandardWidth = 1366;
            int i_PresentHeight = Screen.PrimaryScreen.Bounds.Height;
            //Present Resolution Height
            int i_PresentWidth = Screen.PrimaryScreen.Bounds.Width;
            //Presnet Resolution Width
            f_HeightRatio = (float)((float)i_PresentHeight / (float)i_StandardHeight);
            f_WidthRatio = (float)((float)i_PresentWidth / (float)i_StandardWidth);
            this.AutoScaleMode = AutoScaleMode.None;//Make the Autoscale Mode=None
            this.Scale(new SizeF(f_WidthRatio, f_HeightRatio));
            foreach (Control c in this.Controls)
            {
                get_all_controls(c);
            }

            foreach (Control c in All_controls)
            {
                c.Scale(new SizeF(f_WidthRatio, f_HeightRatio));
                c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio,  
                    c.Font.Style, c.Font.Unit, ((byte)(0)));
            }
        }
public void get_all_controls(Control control)
        {
            c_all.Add(control.Name);
            All_controls.Add(control);
            foreach (Control child in control.Controls)
            {
                get_all_controls(child);
            }
        }


but that didn't work
how can I make my application has same DPI what ever screen resolution is??
I am using vs2010 - windowsform
can any one help me!!!
Posted
Comments
Philippe Mori 9-May-15 18:21pm    
You probably better to leave auto scaling ON and fix issues. If the application is simple and don't uses much controls, then it might almost works.

However, if you are using third-party components that were not designed to be DPI aware or if your own code do some drawing in absolute pixels, then it might be very hard to adapt the application if not impossible.

It might be better to consider switching to WPF which handle scaling much better.
Philippe Mori 9-May-15 18:24pm    
The computation you make for scaling don't make much sense. For a given resolution, it might be hi-DPI or not. A 13 inch laptop in full HD would be hi-DPI while a 27 inches screen at the same resolution would have a very low DPI.

1 solution

A number of suggestions to be considered at https://www.google.com/search?q=dpi+aware+c%23[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900