日期:2014-05-18  浏览次数:21082 次

C#中鼠标移动画图问题
给位大侠,我想在C#中通过鼠标移动时label控件能跟着鼠标位置而动,并画出图来为什么不可以实现,这是我的程序,请各位大侠帮忙,谢谢了。
  private void ZhuKong_MouseMove(object sender, MouseEventArgs e)
  {
  Graphics g = 三维图.CreateGraphics();
  Pen myPen = new Pen(Color.Black);
  Pen myPen1 = new Pen(Color.SteelBlue);
  Pen myPen2 = new Pen(Color.Green);
  myPen.Width = 1;
  myPen1.Width = 1;
  myPen2.Width = 1;
  int r = (三维图.Width) / 60;
  int h = (三维图.Height) / 40;
  int x1=label8.Location.X;
  int y1=label8.Location.Y;
  Point p = new Point(e.X, e.Y);
  label8.Location = p;
  X坐标.Text = e.X.ToString();
  Y坐标.Text = e.Y.ToString();
  Z坐标.Text = e.Y.ToString();  
  g.DrawLine(myPen1, r * 7, h * 3, x1,y1);
  g.DrawLine(myPen1, r * 5, h * 20, x1, y1);
  g.DrawLine(myPen1, r * 57, h * 3, x1, y1);
  g.DrawLine(myPen1, r * 55, h * 20, x1, y1);
  g.DrawLine(myPen2, r * 7, h * 17, r * 5, h * 35);
  g.DrawLine(myPen2, r * 7, h * 17, r * 57, h * 17);
  g.DrawLine(myPen2, r * 57, h * 17, r * 55, h * 35);
  g.DrawLine(myPen2, r * 5, h * 35, r * 55, h * 35);//画地面线
  g.DrawLine(myPen, r * 7, h * 3, r * 7, h * 17);
  g.DrawLine(myPen, r * 7, h * 3, label8.Location.X, label8.Location.Y);
  g.DrawLine(myPen, r * 5, h * 20, label8.Location.X, label8.Location.Y);
  g.DrawLine(myPen, r * 57, h * 3, label8.Location.X, label8.Location.Y);
  g.DrawLine(myPen, r * 55, h * 20, label8.Location.X, label8.Location.Y);

------解决方案--------------------
Graphics g = 三维图.CreateGraphics();
 
把这句话放到外面。。

private Graphics g = 三维图.CreateGraphics();
 
------解决方案--------------------
这些代码都应该放在OnPaint里
在Move里面只是改变数值然后Invalidate