Quantcast
Channel: SEO博客_网站建设_趣味数学题_日语学习 - Wxxslt's blog
Viewing all articles
Browse latest Browse all 10

C#地图效果(PictureBox与Panel滚动条)

$
0
0
拖动大图片改变滚动条位置,模拟鼠标中键。


private Point pt = Point.Empty; //记录鼠标按下时的坐标
private Point def = Point.Empty; //记录鼠标按下panel横纵滚动条的值



private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
 if (MouseButtons.Left != e.Button) return;
 Cursor = Cursors.SizeAll;
 pt = panel1.PointToClient(pictureBox1.PointToScreen(e.Location)); //鼠标是按在picturebox上,需要转化成相对于panel的坐标
 def.X = panel1.HorizontalScroll.Value; //HScroll值
 def.Y = panel1.VerticalScroll.Value; //VScroll值
}



private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
 if (MouseButtons.Left != e.Button) return;
 Point cur = panel1.PointToClient(pictureBox1.PointToScreen(e.Location)); //当前鼠标坐标,同样需要转化成相对于panel的坐标
 cur = new Point(pt.X - cur.X, pt.Y - cur.Y); //计算差
 cur.X = def.X + cur.X; //计算和
 cur.Y = def.Y + cur.Y;
 if (0 > cur.X) cur.X = 0;  //如果超出范围,需要修正
 if (panel1.Width < cur.X) cur.X = panel1.Width;
 if (0 > cur.Y) cur.Y = 0;
 if (panel1.Height < cur.Y) cur.Y = panel1.Height;

 if (panel1.HorizontalScroll.Visible) panel1.HorizontalScroll.Value = cur.X;  //如果存在对应的滚动条,则赋值
 if (panel1.VerticalScroll.Visible) panel1.VerticalScroll.Value = cur.Y;
}



private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
 Cursor = Cursors.Default;
}

Tags -

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images