我在C#,WindowsForms中有一个项目,我创建了一个panel
,其中包含的pictureBox
比他父母大得多。
我将panel.AutoScroll
转为true
我想要做的就是在pictureBox
中拖动panel
而不是抓住滚动并移动它。
即。当我抓住一个图像并将光标向左和向下移动时,我希望得到与panel
的卷轴相同的行为。
怎么做?
答案 0 :(得分:5)
好的,我明白了。 ;-)如果其他人有同样的问题,这里有解决方案:
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;
}
}
答案 1 :(得分:0)
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);
}
}
答案 2 :(得分: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;
}
}