2016-03-23 5 views
1

MouseEnterMouseLeaveMouseDownMouseUpのような異なる色のカスタムボタンがあります。しかし、FolderBrowserDialogMouseClickの後に開いていて、そこからディレクトリを選択した場合、ボタンの外にマウスがあるにもかかわらず、色はまだMouseEnterの色にとどまります。なぜこのようなことが起こるのか分かりません。私はCaptureを使用して問題を解決しようとしましたが、それは私のためには機能しませんでした。たぶん私は間違ってそれを使用した。folderbrowserdialogが開いていてディレクトリを選択したときにMouseLeaveイベントがトリガーされないのはなぜですか?

MyButtonクラスで私のコードのイベントの一部:

//..... 

protected override void OnPaint(PaintEventArgs e) 
    { 
     //..... 
     #region Drawing 
     e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
     roundedRect = new RoundedRectangle(Width, Height, radius); 
     e.Graphics.FillRectangle(Brushes.Transparent, this.ClientRectangle); 

     int R1 = (active1.R + inactive1.R)/2; 
     int G1 = (active1.G + inactive1.G)/2; 
     int B1 = (active1.B + inactive1.B)/2; 

     int R2 = (active2.R + inactive2.R)/2; 
     int G2 = (active2.G + inactive2.G)/2; 
     int B2 = (active2.B + inactive2.B)/2; 

     Rectangle rect = new Rectangle(0, 0, Width, Height); 

     if (this.Enabled) 
     { 
      if (state == MouseState.Leave) 
       using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, inactive1, inactive2, 90f)) 
        e.Graphics.FillPath(inactiveGB, roundedRect.Path); 
      else if (state == MouseState.Over) 
       using (LinearGradientBrush activeGB = new LinearGradientBrush(rect, active1, active2, 90f)) 
        e.Graphics.FillPath(activeGB, roundedRect.Path); 
      else if (state == MouseState.Down) 
       using (LinearGradientBrush downGB = new LinearGradientBrush(rect, Color.FromArgb(R1, G1, B1), Color.FromArgb(R2, G2, B2), 90f)) 
        e.Graphics.FillPath(downGB, roundedRect.Path); 
      using (LinearGradientBrush BorderGB = new LinearGradientBrush(rect, inactive1, inactive2, 90f)) 
       e.Graphics.DrawPath(new Pen(BorderGB), roundedRect.Path); 
     } 
     else 
     { 
      Color linear1 = Color.FromArgb(190, 190, 190); 
      Color linear2 = Color.FromArgb(210, 210, 210); 
      using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, linear1, linear2, 90f)) 
      { 
       e.Graphics.FillPath(inactiveGB, roundedRect.Path); 
       e.Graphics.DrawPath(new Pen(inactiveGB), roundedRect.Path); 
      } 
     } 
     #endregion 
    //..... 
    } 
    protected override void OnClick(EventArgs e) 
    { 
     base.OnClick(e); 
    } 
    protected override void OnEnabledChanged(EventArgs e) 
    { 
     Invalidate(); 
     base.OnEnabledChanged(e); 
    } 
    protected override void OnMouseEnter(EventArgs e) 
    { 
     state = MouseState.Over; 
     Invalidate(); 
     base.OnMouseEnter(e); 
    } 
    protected override void OnMouseLeave(EventArgs e) 
    { 
     state = MouseState.Leave; 
     Invalidate(); 
     base.OnMouseLeave(e); 
    } 
    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     state = MouseState.Down; 
     Invalidate(); 
     base.OnMouseDown(e); 
    } 
    protected override void OnMouseUp(MouseEventArgs e) 
    { 
     state = MouseState.Up; 
     Invalidate(); 
     base.OnMouseUp(e); 
    } 
    #endregion 
    //..... 

答えて

0

私はMouseUpイベントコードを変更し、私の問題を解決しました。これにはMouseState.Upは必要ありません。

//code 
protected override void OnMouseUp(MouseEventArgs e) 
    { 
     if(state != MouseState.Leave) 
      state = MouseState.Over 
     Invalidate(); 
     base.OnMouseUp(e); 
    } 
//code 
関連する問題