2014-10-19 64 views
0

我有一个自定义的控制和我画的一些内容是这样的:为什么不重绘用户控件?

public class TextItem 
{ 
    public Font Font { get; set; } 
} 

    public TaskBox() 
    { 
     SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 

     this.items = new List<TextItem>(); 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 

     foreach (TextItem item in items) 
     { 
      if (item.Bounds.Contains(e.Location)) 
      { 
       item.ForeColor = Color.Red; 
       Cursor.Current = Cursors.Hand; 
      } 
      else 
      { 
       item.ForeColor = Color.Black; 
       Cursor.Current = Cursors.Default; 
      } 
     } 
    } 

光标也随之改变,但文本不变色。我错过了一些初始化?

+2

你没有告诉它使用新颜色重绘 – Plutonix 2014-10-19 12:55:17

+0

这实际上是答案。该控件必须失效。 – 2014-10-19 13:02:06

回答

2

你没有告诉它重画。当您更改您的OnPaint()方法中也使用的任何字段或属性时,请致电this.Invalidate();,以便用户可以看到副作用。

另请注意,指定Cursor.Current为iffy,通常不会持续很长时间,因为光标形状由this.Cursor属性确定。我怀疑,但没有检查,它应该闪烁。分配财产更好。

相关问题