我正在与大量的开发人员一起开发一个WinForms应用程序,这些开发人员拥有不同的屏幕配置,包括不同的DPI设置。因此,我们的应用程序可以使用AutoScaleMode = AutoScaleMode.Font将所有表单设置为自动缩放,并拥有相应的AutoScaleDimensions集,这取决于开发表单的设置。
使用这些配置,WinForms可以在不同的屏幕上适当扩展,问题是,当具有不同屏幕配置的开发人员以设计器模式打开窗体时,Visual通过实际修改自动生成的代码以包含对象的“新”维度,以及通过修改和AutoScaleDimensions属性来匹配新的监视器来扩展控件。
如果没有几个开发人员使用相同的表单,这种行为就可以了。如果发生这种情况,并且那些开发人员有不同的屏幕配置,那么在使用我们的VCS合并更改时会产生大量冲突,这并不是说我会为不同的屏幕分辨率存储值,从而导致UI的混乱。
为了解决这个问题,我尝试通过设置AutoScaleMode = AutoScaleMode.None和为我的控件实现一个自定义设计器来关闭自动缩放。此设计器只在一种分辨率中保存对象的大小,然后通过隐藏Size属性并根据当前DPI返回一个缩放值。我这样做只是为了发现VS designer是根据定制设计器的话生成代码的,而不是实际存储在对象中的值。
那么,有没有人知道如何解决这个问题呢?
发布于 2014-10-02 00:06:25
看来我自己找到了答案。
解决方案是创建一个自定义设计器,该设计器通过自定义设计器的ShadowedProperties数组,使设计人员相信它在显示表单时收到了缩放的值,但在生成给他非缩放值的代码时,如下所示:
// This is the custom designer
public class ScalingDesigner : ControlDesigner
{
public ScalingDesigner(){};
// Say we want to correct the Size property
public Size Size
{
get
{
// When the serializer asks for the value, give him the shadowed one
return (Size)this.ShadowedProperties["Size"]
}
set
{
// When setting the value, assign the standard-DPI based value to the one the serializer would use
this.ShadowedProperties["Size"] = value;
// ... perform all the DPI scaling logic ...
// Then assign a scaled value to the displayed control
this.Control.Size = new Size(scaledWidth, scaledHeight)
}
}
// Associate the shadowed values
public override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);
properties["Size"] =
TypeDescriptor.CreateProperty(
typeof(ScalingDesigner),
(PropertyDescriptor)properties["Size"],
new Attribute[0]);
}
}
// ... and on your control ...
[Designer(typeof(ScalingDesigner))]
public class MyControl : Control
{
// ... whatever logic you want to implement ...
}https://stackoverflow.com/questions/26133744
复制相似问题