【问题标题】:WinForms multiple DPI, multiple developersWinForms 多个 DPI,多个开发人员
【发布时间】:2014-11-25 20:15:02
【问题描述】:

我正在与大量开发人员一起开发 WinForms 应用程序,他们具有不同的屏幕配置,包括不同的 DPI 设置。因此,我们的应用程序可以扩展我们使用AutoScaleMode = AutoScaleMode.Font 将所有表单设置为自动扩展,并设置相应的AutoScaleDimensions,具体取决于开发表单的设置。

使用这些配置,WinForms 可以在不同的屏幕上正确缩放,问题是当具有不同屏幕配置的开发人员在设计器模式下打开表单时,Visual Studio 通过实际修改自动缩放控件来缩放控件。生成的代码包含对象的“新”维度,并且还修改了和AutoScaleDimensions 属性以匹配新监视器。

如果我没有几个开发人员在同一个表单上工作,这种行为会很好。如果发生这种情况,并且那些开发人员有不同的屏幕配置,那么在使用我们的 VCS 合并更改时会产生大量冲突,并不是说我会为不同的屏幕分辨率存储值,从而弄乱 UI。

为了解决这个问题,我尝试通过设置 AutoScaleMode = AutoScaleMode.None 并为我的控件实现自定义设计器来关闭自动缩放。此设计器仅以一种分辨率保存对象的大小,然后通过遮蔽 Size 属性并根据当前 DPI 返回一个缩放值。我这样做只是为了发现 VS 设计器根据自定义设计器所说的内容生成代码,而不是实际存储在对象中的值。

那么,有谁知道如何解决这个问题?

【问题讨论】:

  • AutoScaleMode = AutoScaleMode.Dpi MSDN
  • @bansi 你可以这样做,它会导致相同的行为。设计者将修改自动生成代码上的值以匹配新的 DPI..
  • 在提交时忽略一些自动生成的代码(如果您的 VCS 可以做到的话)?
  • @LazyBadger 好吧,这在尝试在多个开发人员之间开发表单时有点用处,因为一个开发人员无法看到其他开发人员所做的更改!

标签: c# winforms version-control visual-studio-2013 dpi


【解决方案1】:

嗯,看来我自己找到了答案。

解决方案是创建一个自定义设计器,通过ShadowedProperties array自定义设计器,如下面的代码示例所述:

// 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 ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多