-1

在 Paint Event 中调用 RedrawLines() 方法是有效的,但我做了一些应该没有效果的小改动,现在我遇到了问题。首先,当我切换选项卡时,每个选项卡都包含我的 UserControl,这些线不会像以前那样重绘。此外,当我使用 MouseWheel 时,线条并没有完全绘制出来,因为它们在 UserControl 的顶部和底部被截断。然而,当我使用 ScrollBar 时,它们是完整绘制的。任何的想法?

在获得必要的点后,这是我的 DrawLine() 方法的一部分:

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Black);
myPen.Width = 3;
System.Drawing.Graphics formGraphics = this.CreateGraphics();

formGraphics.DrawLine(myPen, p1.X, p1.Y, p2.X, p2.Y);
myPen.Dispose();
formGraphics.Dispose();

所以我有一个 RedrawLines 方法可以准确地调用它

private void RedrawLines(){
    Graphics g = Graphics.FromHwnd(this.Handle);
    g.Clear(Color.White);
    g.Dispose();
    for (int i =0; i < Set_Of_Connections.Count; i++)
    {
         DrawLine(Set_Of_Connections[i].ins.cb, Set_Of_Connections[i].outs.cb, Color.Green);
    }
}

在 Paint 事件中调用它:

private void Switch_Paint(object sender, PaintEventArgs e)
{
    RedrawLines();
}

但就像我说的那样,这可能根本不会帮助你。

4

1 回答 1

0

试试这样:

public UserControl1() {
  InitializeComponent();
  this.DoubleBuffered = true;
  this.ResizeRedraw = true;
}

protected override void OnPaint(PaintEventArgs e) {
  base.OnPaint(e);

  e.Graphics.Clear(Color.White);
  for (int i = 0; i < Set_Of_Connections.Count; ++i)
  {
     DrawLine(e.Graphics, 
              Set_Of_Connections[i].ins.cb,
              Set_Of_Connections[i].outs.cb,
              Color.Green);
  }
}

protected override void OnMouseWheel(MouseEventArgs e) {
  base.OnMouseWheel(e);
  this.Invalidate();
}

protected override void OnScroll(ScrollEventArgs se) {
  base.OnScroll(se);
  this.Invalidate();
}

You should pass the Graphic object from the Paint event to your DrawLine method and use the graphic object there instead of using the CreateGraphic function, which is just a temporary drawing and will cause flickering since it will ignore any DoubleBuffering settings.

于 2012-10-26T03:09:37.903 回答