【问题标题】:Recommendation for Windows.Forms Control that Supports Mouse Panning and Zooming支持鼠标平移和缩放的 Windows.Forms 控件的推荐
【发布时间】:2011-02-23 14:30:47
【问题描述】:

我在http://www.codeproject.com/KB/miscctrl/CustomAutoScrollPanel.aspx 发现使用 Autoscroll 属性自定义一个面板,它是用 AutoScroll = True 围绕 Panel 的包装器。

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

这是一个好用的控件吗?我认为它应该使用ScrollableControl 而不是Windows API。你怎么看?有更好的控制吗?我什至需要控制吗?我认为ScrollableControl 提供了我需要的一切。

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

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

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

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

非常感谢任何建议、帮助或需要查看的领域。一个控件会很好,因为我还没有那么好。

【问题讨论】:

    标签: winforms scroll mouse panel


    【解决方案1】:

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

    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;
    }
    

    【讨论】:

    • 我将如何使用计时器。我不知道我会从哪里开始。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2016-02-28
    相关资源
    最近更新 更多