6

我有一个 C#、WindowsForms 项目,我创建了一个panel包含pictureBox比他的父级大得多的项目。

我转身panel.AutoScrolltrue我想做的是把它拖pictureBox进去,panel而不是抓住一个卷轴并移动它。

即,当我抓取图像并将光标向左和向下移动时,我希望获得与使用panel's 卷轴相同的行为。

怎么做 ?

4

3 回答 3

7

好,我知道了。;-) 如果其他人有同样的问题,这里是解决方案:

    protected Point clickPosition;
    protected Point scrollPosition; 

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition.X = e.X;
        this.clickPosition.Y = e.Y;
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            scrollPosition.X = scrollPosition.X + clickPosition.X - e.X;
            scrollPosition.Y = scrollPosition.Y + clickPosition.Y - e.Y;
            this.panel.AutoScrollPosition = scrollPosition;
        }
    }  
于 2009-12-22T01:14:45.230 回答
1

hsz 解决方案的一个较小的变体:)

    protected Point clickPosition;
    protected Point scrollPosition;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition = e.Location;            
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.SuspendLayout();
            this.scrollPosition += (Size)clickPosition - (Size)e.Location;
            this.panel1.AutoScrollPosition = scrollPosition;                    
            this.ResumeLayout(false);
        }
    }
于 2009-12-22T11:46:50.700 回答
0

hsz 的改进解决方案,有滚动限制,但我只允许垂直滚动

protected Point clickPosition;
protected Point scrollPosition;

private void picBoxScan_MouseDown(object sender, MouseEventArgs e)
{
    this.clickPosition.X = e.X;
    this.clickPosition.Y = e.Y;
}

private void picBoxScan_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        scrollPosition.X = panelViewFile.AutoScrollPosition.X;
        scrollPosition.Y = scrollPosition.Y + (clickPosition.Y - e.Y);
        scrollPosition.Y = Math.Min(scrollPosition.Y,panelViewFile.VerticalScroll.Maximum);
        scrollPosition.Y = Math.Max(scrollPosition.Y,panelViewFile.VerticalScroll.Minimum);
        panelViewFile.AutoScrollPosition = scrollPosition;
    }
}
于 2017-10-21T15:20:36.020 回答