切换到VS 2012,现在表格大小无法正确调整吗? [英] Switched to VS 2012 and now form doesnt resize properly?

查看:34
本文介绍了切换到VS 2012,现在表格大小无法正确调整吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我从VS 2010切换到了VS 2012,除此之外一切似乎都很顺利.

I switched to VS 2012 yesterday from VS 2010 and all seemed to go well except this.

我在表单上有一个按钮,当按下该按钮时,它将扩展表单的宽度以显示其他控件.再次按下按钮,它将减小宽度以隐藏那些控件.现在,所有这些在VS 2010中都运行良好,并且在VS 2012中调试时也可以正常运行,但是一旦发布或编译项目并在单击按钮时打开.exe,它就会像宽度一样增加5而不是需要超过100.我再次单击它,然后它将像应有的一样将其更改为372,并显示我的所有控件.我再次单击它可以隐藏控件,并且可以部分隐藏控件(进入188 +神秘的5),我希望所有这些都有意义,并希望有一种更好的方法来运行所需的过程.

I have a button on my form that when pressed extends the width of the form to show additional controls. Press the button again and it reduces the width to hide those controls. Now all of this worked great in VS 2010 and also works fine when I debug in VS 2012 but as soon as I publish or compile the project and open the .exe when you click on the button it adds like 5 to the width instead of the 100+ it needs to. I click it again and it will then change it to 372 like it should and shows all my controls. I click it again to hide the controls and it hides the controls partially (goes to the 188 + the mysterious 5) I hope all of this makes sense and am hoping there is a better way to run the process I need to.

这是我目前正在使用的代码,在从2010年切换到2012年之间,我没有进行任何更改.实际上,如果我在2010年打开相同的解决方案并发布一切正常,就可以了.

Here is the code I am currently working with and I didn't change anything between switching from 2010 to 2012. In fact, if I open this same solution in 2010 and publish everything works fine.

    private void button1_Click(object sender, EventArgs e)
    {
        if (this.Width == 188)
        {
            this.Width = 372;
            this.Height = 540;
            progressBar.Value = 100;
            copied_status.Text = ("Output View Enabled");
        }
        else
        {
            progressBar.Value = 100;
            copied_status.Text = ("Output View Disabled");
            this.Width = 188;
            this.Height = 540;
        }

        if (this.Width == 372)
        {
            button1.Text = "<<";
        }
        else
            button1.Text = ">>";

    }

推荐答案

您的表单的宽度在很长一段时间内都不是188像素.现在有了VS2012,Windows终于停止了说谎.

The width of your form hasn't been 188 pixels in a long time. Now with VS2012, Windows finally stops lying about it.

有争议的是Aero中的胖窗口边框.当Vista中引入该功能时,它们是一个极端的appcompatpat问题.这是非常必要的,因为这两个像素很难用鼠标击中.但是与应用程序创建窗口的方式完全不兼容.它要求特定的窗口大小,外部大小,CreateWindow()函数的nWidth和nHeight参数.但是真正重要的是客户区的大小,即边界内窗口的一部分.如果Microsoft对此不做任何事情,那么旧的应用程序最终将拥有一个很小的客户区.看起来很糟糕,窗口内容不再适合.例如,无法完全显示表单底部或右侧的控件.

At issue are the fat window borders in Aero. They were an extreme appcompat problem when the feature was introduced in Vista. Very necessary because those two pixels where getting hard to hit with a mouse. But drastically incompatible with the way an application creates a window. It asks for a specific window size, the outer size, the nWidth and nHeight arguments of the CreateWindow() function. But what really matters is the size of the client area, the part of the window inside the borders. If Microsoft wouldn't have done something about it, old applications would have ended up with a client area that was too small. Which looks very bad, the window content would not fit anymore. A control towards the bottom or right side of the form would not be completely displayed for example.

因此,Aero偷偷地使窗口变大了胖边框的额外宽度.当应用程序询问窗口大小时,它偷偷地说以相同的添加宽度来缩小窗口.该应用程序没有比它在XP上仍具有相同窗口大小的运行状态更好的信息了.这工作得很好,但并不完全理想.例如,很难使窗口边缘与该谎言正确对齐.

So, sneakily, Aero makes the window larger by the extra width of the fat borders. And when the app asks for the window size, it sneakily says that it is smaller by the same added width. The app doesn't know any better than it is still running with the same window size it had on XP. This works pretty well, but is not exactly ideal. Hard to get window edges to align properly with that lie for example.

Aero是否会撒谎窗口的大小取决于EXE标头中记录的目标操作系统.如果看到的版本早于Vista的版本号6.00,那么它将假定您的EXE是旧程序,不知道粗边框功能.因此需要说谎.您已经将目标版本号设置为4.00了很长时间,它是由.NET编译器在构建程序时编写的.您可以使用 dumpbin.exe/headers yourapp.exe 看到它.

Whether or not Aero will lie about the window size is based on the target operating system recorded in the EXE header. When it sees a version older then 6.00, the Vista version number, then it will assume that your EXE is a legacy program that doesn't know about the fat border feature. So needs to be lied to. You've been running with that target version number set to 4.00 for a long time, it is written by the .NET compiler when it builds your program. You can see it with dumpbin.exe /headers yourapp.exe.

这最终在VS2012和.NET 4.5中得到了改变.这是XP中不可用的.NET版本.最终,编译器可以严格假设XP是历史记录,您将在支持Aero的Windows版本上运行.因此,它将EXE标头中的目标Windows版本设置为6.00.相应地,Aero现在将停止在窗口大小附近躺下.您得到的是真实的,而不是假的.

This finally changed in VS2012 and .NET 4.5. Which is a .NET version that is not available in XP. The compiler can finally make the hard assumption that XP is history and you are going to run on a version of Windows that supports Aero. So it sets the target Windows version in the EXE header to 6.00. Correspondingly, Aero will now stop lying about the window size. You get the real one, not the faked one.

一个快速的解决方法是将目标.NET Framework版本更改为4.0.在XP上可以使用,因此您会再次撒谎.

So a quick fix is to change the target .NET framework version to 4.0. That's available on XP so you'll get lied to again.

当然,最好修复您的代码.永远不要使用Size,Width或Height属性,它们将不可避免地取决于边框和标题的大小.改用ClientSize属性,这是稳定的属性,也是您真正关心的属性.但是也要注意该属性,当窗体在其视频适配器设置为每英寸96点以上的计算机上运行时,窗体可能会重新缩放.Vista和更高版本中非常容易使用的另一个功能.重新缩放会通过DPI设置按比例更改ClientSize.

Of course it is better to fix your code. Never use the Size, Width or Height property, they'll inevitably depend on the border and caption size. Use the ClientSize property instead, that's the stable one and the one you really care about. But be careful with that property as well, a form may rescale when it runs on a machine that has its video adapter set to more than 96 dots per inch. Another feature that's very accessible in Vista and up. Rescaling changes the ClientSize proportionally by the DPI setting.

真正的解决方法是使用bool字段来跟踪窗口状态.并根据要隐藏或显示的控件的位置设置ClientSize属性.大致如此:

The real fix is to use a bool field instead that keeps track of the window state. And set the ClientSize property based on the position of a control you want to hide or reveal. So roughly:

private bool enlarged;

private void button1_Click(object sender, EventArgs e)
{
    enlarged = !enlarged;
    int width = someControl.Left - 5;
    if (enlarged) width = someControl.Right + 5;
    this.ClientSize = new Size(width, this.ClientSize.Height);
}

用您的控件名称替换此代码中的someControl.

Replace someControl in this code with the name of your controls.

这篇关于切换到VS 2012,现在表格大小无法正确调整吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆