问题
VS 2010 seems to be updating my form's designer.cs file when I open the form in designer view.
For example, say I have a form with a label of type System.Windows.Forms.Label and I had created the label such that the designer file has (auto generated code)
this.myLabel.AutoSize = true;
this.myLabel.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.myLabel.Location = new System.Drawing.Point(**25, 63**);
this.myLabel.Name = "myLabel";
this.myLabel.Size = new System.Drawing.Size(**101, 13**);
this.myLabel.TabIndex = 1;
this.myLabel.Text = "A simple windows label.";
when I close out of designer view and open it again, VS 2010 will sometimes alter it to
this.myLabel.AutoSize = true;
this.myLabel.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.myLabel.Location = new System.Drawing.Point(**29, 78**);
this.myLabel.Name = "myLabel";
this.myLabel.Size = new System.Drawing.Size(**124, 17**);
this.myLabel.TabIndex = 1;
this.myLabel.Text = "A simple windows label.";
when I've not done anything other than open the file.
Does anyone know why this is happening? Has anyone else experienced anything like this?
回答1:
Feature, not a bug. The form was originally designed on a machine that had its video adapter dots-per-inch setting at 96. Your machine has a different DPI, 120. Very easy to change on modern versions of Windows, you picked the 125% (Medium) setting in Control Panel + Display.
A different dpi setting affects the size of the fonts you use. There's one visible in your code snippet, it is 8.25 points. One point is 1/72 inch. So the text was originally 8.25 * 1/72 = 0.114583 inches high. With the dpi at 96, that's 0.114583 x 96 = 11 pixels. Note how this also explains why you got the extra 0.25 added to the point size, it made a nice round number.
But now your dpi is 120, the text will be 8.25 x 1/72 x 120 = 14 pixels. The text got bigger, your control needs to get bigger too or it will clip the text, shearing off the descenders.
This was done automatically when you loaded the form. The essential properties are AutoScaleMode, set to Font by default. And AutoScaleDimensions, you'll find it assigned at the top of the InitializeComponent() method. That's the one that remembered what the original DPI setting was.
This is going to happen on your user's machine as well. Do make sure that the form auto-scaled properly. You may have some trouble if you have controls that have a different font size. And just check the form back into source control so this rescaling doesn't happen again.
来源:https://stackoverflow.com/questions/15557352/vs-2010-updates-designer-cs-when-no-changes-are-made