如何使Compact Framework的自定义控件意识到自动定标 [英] How to make compact framework custom controls AutoScale aware

查看:127
本文介绍了如何使Compact Framework的自定义控件意识到自动定标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows窗体(包括Windows窗体紧凑型Framwork,这是我在用的)具有的自动功能。通过设置 AutoScaleMode 属性 AutoScaleMode.Dpi ,您的320x200的应用而设计,比如说,会自动调节以较大的显示例如,一个VGA设备。

Windows Forms (including Windows Forms for Compact Framwork, which is what I am using) has an AutoScale feature. By setting the AutoScaleMode property to AutoScaleMode.Dpi, your application designed for, say, 320x200 automatically scales to the larger display of, for example, a VGA device.

这个伟大的工程,但我有做一些自制的自定义控件自己的的OnPaint 的东西,我希望他们扩展为好。不幸的是,我还没有发现好的文档或如何做到这一点的例子

This works great, but I have a few self-made custom controls that do their own OnPaint stuff, and I'd like them to scale as well. Unfortunately, I've not found good documentation or an example on how to do that.

目前,我这样做:

protected SizeF zoom = new SizeF(1.0, 1.0);

protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
    base.ScaleControl(factor, specified);
    zoom = factor;        // remember the zoom factor
}

protected override void OnPaint(PaintEventArgs e)
{
    // scale everything by zoom.Width and zoom.Height
    ...
    e.Graphics.DrawImage(...);
    ...
}



它的工作原理,但我想知道如果这是正确的方式来做到这一点。由于(根据ILSpy)没有其他CF控件有一个内部字段来存储规模的因素,我不知道是否有这样做的更容易或更好的方法。

It works, but I'm wondering if this is "the right way" to do it. Since (according to ILSpy) none of the other CF controls have an internal field to store the scale factor, I'm wondering if there's an easier or better way to do this.

推荐答案

我们一般处理所有的缩放在 onResize受到在我们的控制和形式。我们还支持了很多疯狂的尺寸和DPI不同的设备(有些paltforms甚至不报告正确的DPI!)。我们发现与 AutoScaleMode 关你可以使用proportionaly的助手这样的 onResize受到来扩展表单的孩子。您只需添加一个尺寸_initalSize 成员设置为在构造函数中窗体大小。不过我一般在我编写自定义布局代码适当处理纵向和横向显示大多数形式的发现。

We generally handle all of the scaling in OnResizein our controls and forms. We also have to support a lot of different devices with crazy dimensions and DPI (some paltforms don't even report the correct DPI!). We found with AutoScaleMode off you can proportionaly use a helper like this to scale a form's children in OnResize. You simply add a Size _initalSize member set to the form size in the constructor. However I've generally found on most forms I have to write custom layout code to appropriate deal with portrait and landscape displays.

        protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        // Scale the control
        ScaleChildren(this, ref _initialFormSize);
    }


        public static void ScaleChildren(Control control, ref Size initialSize, float fontFactor)
    {
        if (control == null || control.Size == initialSize)
            return;

        SizeF scaleFactor = new SizeF((float)control.Width / (float)initialSize.Width, (float)control.Height / (float)initialSize.Height);
        initialSize = control.Size;

        if (!float.IsInfinity(scaleFactor.Width) || !float.IsInfinity(scaleFactor.Height))
        {
            foreach (Control child in control.Controls)
            {
                child.Scale(scaleFactor);

                if (child is Panel)
                    continue;

                try
                {
                    // scale the font
                    float scaledFontSize = (float)(int)(child.Font.Size * scaleFactor.Height * fontFactor + 0.5f);

                    System.Diagnostics.Debug.WriteLine(
                        string.Format("ScaleChildren(): scaleFactor = ({0}, {1}); fontFactor = {2}; scaledFontSize = {3}; \"{4}\"",
                        scaleFactor.Width, scaleFactor.Height, fontFactor, scaledFontSize, child.Text));

                    child.Font = new Font(child.Font.Name, scaledFontSize, child.Font.Style);
                }
                catch { }
            }
        }
    }

这篇关于如何使Compact Framework的自定义控件意识到自动定标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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