用于绘制图形和滚动的面板

发布于 2024-10-04 23:41:37 字数 146 浏览 6 评论 0原文

我希望能够使用 Panel 或类似工具将图形绘制到 Winform 上。如果图形变得大于控件,我似乎看不到任何有关添加滚动条的信息?

是否可以使用 Panel 来执行此操作,或者是否有类似的控件可以实现此目的?

I want to be able to use a Panel or similar to draw graphics onto a Winform. I cannot seem to see anything regarding adding scrollbars if the graphics become larger than the control?

Is it possible to do this with a Panel or is there a similar control that will allow it?

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

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

发布评论

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

评论(2

治碍 2024-10-11 23:41:37

将 AutoScroll 属性设置为 true,并将 AutoScrollMinSize 属性设置为图像的大小。当图像太大时,滚动条现在会自动出现。

您需要从 Panel 继承自己的类,以便可以在构造函数中将 DoubleBuffered 属性设置为 true。否则闪烁会很明显。一些示例代码:

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

class ImageBox : Panel {
    public ImageBox() {
        this.AutoScroll = true;
        this.DoubleBuffered = true;
    }
    private Image mImage;
    public Image Image {
        get { return mImage; }
        set {
            mImage = value;
            if (value == null) this.AutoScrollMinSize = new Size(0, 0);
            else {
                var size = value.Size;
                using (var gr = this.CreateGraphics()) {
                    size.Width = (int)(size.Width * gr.DpiX / value.HorizontalResolution);
                    size.Height = (int)(size.Height * gr.DpiY / value.VerticalResolution);
                }
                this.AutoScrollMinSize = size;
            }
            this.Invalidate();
        }
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
        base.OnPaint(e);
    }
}

Set the AutoScroll property to true and the AutoScrollMinSize property to the size of the image. The scrollbars will now automatically appear when the image is too large.

You'll want to inherit your own class from Panel so that you can set the DoubleBuffered property to true in the constructor. Flicker would be noticeable otherwise. Some sample code:

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

class ImageBox : Panel {
    public ImageBox() {
        this.AutoScroll = true;
        this.DoubleBuffered = true;
    }
    private Image mImage;
    public Image Image {
        get { return mImage; }
        set {
            mImage = value;
            if (value == null) this.AutoScrollMinSize = new Size(0, 0);
            else {
                var size = value.Size;
                using (var gr = this.CreateGraphics()) {
                    size.Width = (int)(size.Width * gr.DpiX / value.HorizontalResolution);
                    size.Height = (int)(size.Height * gr.DpiY / value.VerticalResolution);
                }
                this.AutoScrollMinSize = size;
            }
            this.Invalidate();
        }
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
        base.OnPaint(e);
    }
}
雪化雨蝶 2024-10-11 23:41:37

我不是 100% 确定你想要完成什么,但这里有一个类似的 这个问题可能对您有帮助。

您还可以尝试使用 PictureBox当图形变大时,您可以手动更改其大小。然后将表单 AutoScroll 设置为 true。

I'm not 100% sure what you're trying to accomplish, but here is a similar SO question that might help you.

You could also try using a PictureBox that you would manually change its size as the graphics get larger. Then set your form AutoScroll to true.

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