3

我正在尝试构建自己的“PictureBox like”控件来添加一些功能。例如,我希望能够通过简单地用鼠标单击和拖动来平移大图像。

问题似乎出在我的OnMouseMove方法上。如果我使用下面的代码,我会得到我想要的拖动速度和精度,但是当然,当我释放鼠标按钮并再次尝试拖动时,图像会恢复到原来的位置。

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

namespace Testing
{
    public partial class ScrollablePictureBox : UserControl
    {
        private Image image;
        private bool centerImage;

        public Image Image
        {
            get { return image; }
            set { image = value; Invalidate(); }
        }

        public bool CenterImage
        {
            get { return centerImage; }
            set { centerImage = value; Invalidate(); }
        }

        public ScrollablePictureBox()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            Image = null;
            AutoScroll = true;
            AutoScrollMinSize = new Size(0, 0);
        }

        private Point clickPosition;
        private Point scrollPosition;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            clickPosition.X = e.X;
            clickPosition.Y = e.Y;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                scrollPosition.X = clickPosition.X - e.X;
                scrollPosition.Y = clickPosition.Y - e.Y;
                AutoScrollPosition = scrollPosition;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.FillRectangle(new Pen(BackColor).Brush, 0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);

            if (Image == null)
                return;

            int centeredX = AutoScrollPosition.X;
            int centeredY = AutoScrollPosition.Y;

            if (CenterImage)
            {
               //Something not relevant
            }

            AutoScrollMinSize = new Size(Image.Width, Image.Height);
            e.Graphics.DrawImage(Image, new RectangleF(centeredX, centeredY, Image.Width, Image.Height));
        }
    }
}

但是,如果我将OnMouseMove方法修改为如下所示:

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    if (e.Button == MouseButtons.Left)
    {
        scrollPosition.X += clickPosition.X - e.X;
        scrollPosition.Y += clickPosition.Y - e.Y;
        AutoScrollPosition = scrollPosition;
    }
}

...你会看到拖动不像以前那样平滑,有时表现得很奇怪(比如滞后或其他东西)。

我究竟做错了什么?

我还尝试在绝望的运动中删除所有“基本”调用来解决这个问题,哈哈,但同样,它没有用。

谢谢你的时间。

4

2 回答 2

3

最后,我设法找到了解决方案:

protected Point clickPosition;
protected Point scrollPosition;
protected Point lastPosition;

protected override void OnMouseDown(MouseEventArgs e)
{
    clickPosition.X = e.X;
    clickPosition.Y = e.Y;
}

protected override void OnMouseUp(MouseEventArgs e)
{
    Cursor = Cursors.Default;
    lastPosition.X = AutoScrollPosition.X;
    lastPosition.Y = AutoScrollPosition.Y;
}

protected override void OnMouseMove(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Cursor = Cursors.Hand;
        scrollPosition.X = clickPosition.X - e.X - lastPosition.X;
        scrollPosition.Y = clickPosition.Y - e.Y - lastPosition.Y;
        AutoScrollPosition = scrollPosition;
    }
}
于 2010-04-22T12:19:51.320 回答
2

每次我必须这样做时,这总是令人困惑。为了那些新来这个线程的人的利益,我想出了一个稍微简单的解决方案,它可以提供平滑的平移。

Private GrabPoint As Point

Private Sub OnMouseDown(MouseEventArgs e)
  GrabPoint = e.Location
End Sub

Private Sub OnMouseMove(MouseEventArgs e)
  If e.Button = System.Windows.Forms.MouseButtons.Left Then
    AutoScrollPosition = GrabPoint - e.Location - AutoScrollPosition
  End If
End Sub

Private Sub OnMouseUp(MouseEventArgs e)
  GrabPoint = Point.Empty
End Sub

顺便说一句,抓取和抓取手形光标可以从以下位置下载: http: //theburningmonk.com/2010/03/wpf-loading-grab-and-grabbing-cursors-from-resource/

您可以将它们作为嵌入式资源添加到您的项目中,并设置它们:

Cursor = New Cursor(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(String.Format("{0}.{1}.cur", Me.GetType.Namespace, "grabbing")))
于 2012-01-19T15:59:46.207 回答