简体   繁体   中英

Is there any way to prevent my application from running on Windows 7 machines?

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.

您可以使用System.Environment.OSVersion ,但最好的方法是让您的应用程序在Windows 7上运行并正确查看,您真的不想丢失具有Windows 7的客户端(特别是现在当它使用最多时) Windows版)

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM