WinForms Control.Scale 方法

发布于 2024-10-21 21:35:14 字数 1040 浏览 2 评论 0原文

下面的代码显示了一个小型 WinForms 应用程序,其中包含一个绘制圆圈的简单控件。我试图了解 Control.Scale 方法的行为。

如果我从 Main 调用 Control 上的 Scale 方法,如代码所示,它会正确缩放。但如果我从 Circle 的构造函数中调用 Scale,则不会发生缩放。

我在这里的困惑无疑表明我对 Scale 的用途存在严重误解。谁能启发我吗?

using System;
using System.Windows.Forms;
using System.Drawing;

class Program
{
    [STAThread]
    public static void Main()
    {
        var circle = new Circle(Color.Orange)
        {
            Size = new Size(23, 23),
            Location = new Point(50, 50)
        };
        circle.Scale(new SizeF(3.0f, 3.0f));      //  <-- scaling here works

        var form = new Form();
        form.Controls.Add(circle);

        Application.Run(form);
    }
}

class Circle : Control
{
    public Circle(Color color)
    {
        ForeColor = color;
        // Scale(new SizeF(3.0f, 3.0f));     //  <-- scaling here DOESN'T work
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
    }
}

The code below shows a small WinForms app which includes a simple Control that draws a circle. I'm trying to understand the behavior of the Control.Scale method.

If I call the Scale method on the Control from Main, as shown in the code, it scales properly. But if I instead call Scale from Circle's constructor, no scaling occurs.

My puzzlement here no doubt indicates a gross misunderstanding on my part regarding what Scale is supposed to do. Can anyone enlighten me?

using System;
using System.Windows.Forms;
using System.Drawing;

class Program
{
    [STAThread]
    public static void Main()
    {
        var circle = new Circle(Color.Orange)
        {
            Size = new Size(23, 23),
            Location = new Point(50, 50)
        };
        circle.Scale(new SizeF(3.0f, 3.0f));      //  <-- scaling here works

        var form = new Form();
        form.Controls.Add(circle);

        Application.Run(form);
    }
}

class Circle : Control
{
    public Circle(Color color)
    {
        ForeColor = color;
        // Scale(new SizeF(3.0f, 3.0f));     //  <-- scaling here DOESN'T work
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2024-10-28 21:35:14

Scale() 方法并不意味着执行此操作。它是实现 AutoScaleMode 属性的辅助方法。当您的控件是由表单的InitializeComponent() 方法创建时,缩放将通过SuspendLayout() 暂停。这就是为什么它对你的构造函数没有影响。创建表单句柄时将应用 AutoScaleMode 属性值。这会取消您应用的任何缩放。

我认为您正在 OnPaint 方法中寻找 e.Graphics.ScaleTransform() 。它不会缩放控件,而是缩放绘图。如果您确实想要缩放控件,则只需更改其 Size 属性即可。

The Scale() method isn't meant to do this. It is a helper method to implement the AutoScaleMode property. When your control is created by the form's InitializeComponent() method, scaling is suspended with SuspendLayout(). Which is why it has no effect in your constructor. The AutoScaleMode property value is applied when the form handle is created. Which cancels any scaling you applied.

I think you are looking for e.Graphics.ScaleTransform() in your OnPaint method. It doesn't scale the control, it scales the drawing. If you really did mean to scale the control then just change its Size property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文