Coding my project was going well. But today I noticed a problem.
My main notebook has full hd (1920x1080) resolution and I am coding my projects here. When I changed my main notebook's resolution to 1280x1024, 1280x800, or 1024x768 there is no problem. My application's resolution is 1024x768 and it is not collapsing. This is the printscreen.
But my other notebook has 1366x768 resolution. And I am running my application on this notebook. Oh! There is a disappointment. My application screen shifted. This is the bad printscreen.
I do not understand why. What can I do correcting this error?
It arises from different DPI settings. You can do this in the form load:
// Get DPI width
float x = this.CreateGraphics().DpiX;
// If screen is width
if (x == 120)
// Get big image from Resources
this.BackgroundImage = Properties.Resources.BigImage;
else
// Get small image from Resources
this.BackgroundImage = Properties.Resources.SmallImage;
I had the same problem with a Windows Forms form. All I had to do was change the AutoScaleMode setting for the form from Font to DPI and change the FormBorderStylefor setting from Fixed to Sizable. Now the Windows Forms form displays correctly on the desktop and the laptop.
You can check if the DPI settings of the two screens are the same. You do this by going through Control Panel or Display options (I can't remember exactly, and I don't have Windows 7 in front of me) (You probably have 120 DPI on your HD capable laptop, and standard 96 on the other).
In your program, set the form's AutoScaleMode
to None
and try again.
Here is a resource to assist in how to handle auto-scaling forms:
Automatic scaling in Windows Forms
There is no direct solution to fix the pixel problem. But we can do it ourselves.
First we have to find the controls available in our form, and then we have to resize them.
Add a class, "IdentifyControls.cs", that identifies all the controls of a form and returns the list of control in your application. A class can be added into the application selecting project -> Add class from the menu bar. Then type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FallingFlowers // Falling Flowers is the name of my project
{
public static class IdentifyControl
{
public static List<Control> findControls(Control c)
{
List<Control> list = new List<Control>();
foreach (Control control in c.Controls)
list.Add(control);
return list;
}
}
}
Then add another class into your application, say "demo.cs":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace FallingFlowers // Falling Flowers is the name of my project
{
public class demo
{
public static void relocate(Form fo, int ox, int oy)
{
List<Control> list = new List<Control>();
list = IdentifyControl.findControls(fo);
for (int i = 0; i < list.Count; i++)
reposition(list[i], ox, oy);
}
public static void reposition(Control c, int ox, int oy)
{
int x, y;
x = ((c.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox);
y = ((c.Location.Y * Screen.PrimaryScreen.Bounds.Height) / oy);
c.Location = new Point(x, y);
x = ((c.Width * Screen.PrimaryScreen.Bounds.Width) / ox);
y = ((c.Height * Screen.PrimaryScreen.Bounds.Height) / oy);
c.Width = x;
c.Height = y;
if (c is Label || c is Button)
resizeText(c, ox, oy);
}
public static void resizeText(Control l, int ox, int oy)
{
float s;
float txtsize = l.Font.Size;
s = ((txtsize * Screen.PrimaryScreen.Bounds.Width) / ox)+1;
l.Font = new Font(l.Font.Name, s,l.Font.Style);
}
public static void repositionForm(Form f, int ox, int oy)
{
int x, y;
x = (f.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox;
y = (f.Location.Y * Screen.PrimaryScreen.Bounds.Width) / oy;
f.Location = new Point(x, y);
}
}
}
This class contains methods to relocate the controls, resize the text and to resize the form.
Call these functions in the load event of your form.
To relocate all the controls in the form
demo.relocate(this, 1366, 768);
Here 1366 and 768 are the original resolution in which the application is developed.
To relocate the form:
demo.repositionForm(this, 1366, 768);
1366 and 768 are the original resolution in which the application is developed.
For you it will be demo.relocate(this, 1920, 1080);
.
I hope that this will help you :-)...
I agree the code for the screen settings would help a lot in finding out your issue.
But it seems like you're setting the pictures to set coordinate points instead of points relative to the screen size. You might want to make the coordinates a ratio of the screen size to have the display always look nice.