支持鼠标平移和缩放的 Windows.Forms 控件的建议

发布于 2024-09-03 05:09:44 字数 1282 浏览 6 评论 0原文

我在 http:// 找到使用 Autoscroll 属性自定义面板 www.codeproject.com/KB/miscctrl/CustomAutoScrollPanel.aspx 是一个带有 AutoScroll = TruePanel 包装器。

我喜欢这个控件,因为它提供了“performScrollHorizo​​ntal”和“performScrollVertical”方法。然而,它使用 Windows API 函数而不是 ScrollableControl 及其 VerticalScrollHorizo​​ntalScroll 属性。

这是一个很好用的控件吗?我认为应该使用 ScrollableControl 而不是 Windows API。你怎么认为?有没有更好的控制方法?我还需要一个控件吗?我认为 ScrollableControl 提供了我需要的一切。

编辑:我找到了 HScrollBar 和 VScrollBar 控件。我应该使用它们吗?

这两个其他控件很好,但没有给我提供像上面的控件那样控制滚动条的方法。

我真正想要的是一个控件:

  • 当用户将鼠标移向控件边缘时滚动,
  • 允许用户平移
  • 允许用户缩放
  • 支持使用鼠标并按下 Shift 或 Ctrl 或 Alt 键

任何建议、帮助或需要关注的领域都将不胜感激。一个控制会很好,因为我还没有那么好。

I found Customize a panel with Autoscroll property at http://www.codeproject.com/KB/miscctrl/CustomAutoScrollPanel.aspx that is wrapper around a Panel with AutoScroll = True.

I like this control because it provides the "performScrollHorizontal" and "performScrollVertical" methods. Yet, it uses the Windows API functions instead of ScrollableControl and its VerticalScroll and HorizontalScroll properties.

Is this a good control to use? I think it should be using ScrollableControl instead of the Windows API. What do you think? Is there a better control available? Do I even need a control? I would think that ScrollableControl provides everything I would need.

EDIT: I found the HScrollBar and VScrollBar controls. Should I be using them?

These two other controls are nice but do not give me a way to control the scroll bars like the above control does.

What I really want is a control:

  • that scrolls when the user moves the mouse toward the edge of the control,
  • allows the user to pan
  • allows the user to zoom
  • supports using the mouse with Shift or Ctrl or Alt keys pressed

Any recommendations, help, or areas to look at is greatly appreciated. A control would be nice as I am not that good yet.

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

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

发布评论

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

评论(1

抱着落日 2024-09-10 05:09:44

一些可以玩的代码。它支持焦点、平移和滚动。缩放是一项工作,我的笔记本电脑的鼠标垫妨碍了测试。使用定时器实现边缘自动滚动。

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

class ZoomPanel : Panel {
    public ZoomPanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.Selectable, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.AutoScroll = this.TabStop = true;
    }
    public Image Image {
        get { return mImage; }
        set { 
            mImage = value; 
            Invalidate();
            mZoom = 1.0;
            this.AutoScrollMinSize = (mImage != null) ? mImage.Size : Size.Empty;
        }
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            this.Cursor = Cursors.SizeAll;
            mLastPos = e.Location;
            this.Focus();
        }
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) this.Cursor = Cursors.Default;
        base.OnMouseUp(e);
    }
    protected override void OnMouseMove(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            this.AutoScrollPosition = new Point(
                -this.AutoScrollPosition.X - e.X + mLastPos.X,
                -this.AutoScrollPosition.Y - e.Y + mLastPos.Y);
            mLastPos = e.Location;
            Invalidate();
        }
        base.OnMouseMove(e);
    }
    protected override void OnMouseWheel(MouseEventArgs e) {
        if (mImage != null) {
            mZoom *= 1.0 + 0.3 * e.Delta / 120;
            this.AutoScrollMinSize = new Size((int)(mZoom * mImage.Width),
                (int)(mZoom * mImage.Height)); \
            // TODO: calculate new AutoScrollPosition...
            Invalidate();
        }
        base.OnMouseWheel(e);
    }
    protected override void OnPaint(PaintEventArgs e) {
        if (mImage != null) {
            var state = e.Graphics.Save();
            e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
            e.Graphics.DrawImage(mImage, 
                new Rectangle(0, 0, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height));
            e.Graphics.Restore(state);
        }
        //if (this.Focused) ControlPaint.DrawFocusRectangle(e.Graphics,
        //    new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height));
        base.OnPaint(e);
    }
    protected override void OnEnter(EventArgs e) { Invalidate(); base.OnEnter(e); }
    protected override void OnLeave(EventArgs e) { Invalidate(); base.OnLeave(e); }

    private double mZoom = 1.0;
    private Point mLastPos;
    private Image mImage;
}

Some code to play with. It supports focus, panning and scrolling. Zooming is work-to-do, my laptop's mousepad got in the way of testing it. Use a timer to implement auto-scrolling at the edges.

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

class ZoomPanel : Panel {
    public ZoomPanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.Selectable, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.AutoScroll = this.TabStop = true;
    }
    public Image Image {
        get { return mImage; }
        set { 
            mImage = value; 
            Invalidate();
            mZoom = 1.0;
            this.AutoScrollMinSize = (mImage != null) ? mImage.Size : Size.Empty;
        }
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            this.Cursor = Cursors.SizeAll;
            mLastPos = e.Location;
            this.Focus();
        }
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) this.Cursor = Cursors.Default;
        base.OnMouseUp(e);
    }
    protected override void OnMouseMove(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            this.AutoScrollPosition = new Point(
                -this.AutoScrollPosition.X - e.X + mLastPos.X,
                -this.AutoScrollPosition.Y - e.Y + mLastPos.Y);
            mLastPos = e.Location;
            Invalidate();
        }
        base.OnMouseMove(e);
    }
    protected override void OnMouseWheel(MouseEventArgs e) {
        if (mImage != null) {
            mZoom *= 1.0 + 0.3 * e.Delta / 120;
            this.AutoScrollMinSize = new Size((int)(mZoom * mImage.Width),
                (int)(mZoom * mImage.Height)); \
            // TODO: calculate new AutoScrollPosition...
            Invalidate();
        }
        base.OnMouseWheel(e);
    }
    protected override void OnPaint(PaintEventArgs e) {
        if (mImage != null) {
            var state = e.Graphics.Save();
            e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
            e.Graphics.DrawImage(mImage, 
                new Rectangle(0, 0, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height));
            e.Graphics.Restore(state);
        }
        //if (this.Focused) ControlPaint.DrawFocusRectangle(e.Graphics,
        //    new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height));
        base.OnPaint(e);
    }
    protected override void OnEnter(EventArgs e) { Invalidate(); base.OnEnter(e); }
    protected override void OnLeave(EventArgs e) { Invalidate(); base.OnLeave(e); }

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