Reputation: 49
As suggested in the title, I've created an application (windows forms) on Visual Express 2010 on a machine using Windows XP. I've tested the application on a machine running Windows 7 and the format change of buttons and controls kind of ruin the general look of the application.
Is there any way to prevent my application from running on a Windows 7 computer?
Other alternative would be checking what OS is the user using and depending on this, change the alignment/position of buttons/controls/etc...
What would be the best approach?
Thanks in advance for your answers.
Upvotes: 3
Views: 351
Reputation: 7398
Simply check what operating system version your app is running on, if your program is running on windows 7 then exit the program, example,
if (Environment.OSVersion.ToString().Contains("Microsoft Windows NT 6.1"))
{
//code to execute if running win 7
}
Upvotes: 4
Reputation: 942297
You almost certainly mis-identified the source of the problem. By far the most common cause of form layout corruption is the video adapter's DPI setting. It determines the number of pixels per inch and is very easy to change on Windows 7. It can also be changed on XP, it is just harder to do so. The setting is a big deal, monitors are getting better especially with the Apple's push for "retina" displays.
Add the checking code by tweaking your Main() method in Program.cs. Make it look similar to this:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var main = new Form1();
using (var gr = main.CreateGraphics()) {
if (gr.DpiX != 96) {
MessageBox.Show("Sorry, didn't get this done yet. Bye");
Environment.Exit(1);
}
}
Application.Run(main);
}
Fixing your form layout is of course the long term solution. The standard mistake is to give controls their own Font property that doesn't match the form's Font property or their container's. An easy way to trigger the same problem on your XP machine is to emulate what the DPI setting does. Saves you the hassle of finding the setting on XP and having to reboot the machine when you change it. Paste this code into your form:
protected override void OnLoad(EventArgs e) {
this.Font = new Font(this.Font.FontFamily, this.Font.SizeInPoints * 120f / 96f);
base.OnLoad(e);
}
Which is equivalent to selecting 125% scaling in the Win7 Display preferences.
Upvotes: 4
Reputation: 7044
You can use System.Environment.OSVersion, but the best approach is to make your application run and look correctly on Windows 7 too, you don't really want to lose the clients that have Windows 7 (especially now when that is the most used Windows version)
Upvotes: 4