我在C#,WindowsForms中創建了一個項目,並創建了一個panel
,其中包含比他的父項大得多的pictureBox
。面板中的移動圖片框
我轉身panel.AutoScroll
到true
,我想要做的就是拖着這pictureBox
在panel
,而不是追趕滾動,移動它是什麼。
I.e.當我抓取圖像並將光標向左和向下移動時,我想獲得與我將使用panel
的卷軸完全相同的行爲。
怎麼辦?
我在C#,WindowsForms中創建了一個項目,並創建了一個panel
,其中包含比他的父項大得多的pictureBox
。面板中的移動圖片框
我轉身panel.AutoScroll
到true
,我想要做的就是拖着這pictureBox
在panel
,而不是追趕滾動,移動它是什麼。
I.e.當我抓取圖像並將光標向左和向下移動時,我想獲得與我將使用panel
的卷軸完全相同的行爲。
怎麼辦?
好吧,我明白了。 ;-)如果任何人有同樣的問題,這裏是解決方案:
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;
}
}
的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);
}
}
從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;
}
}