2012-02-29 62 views
0

有一個玻璃按鈕,在這裏一些真正偉大的代碼:http://www.lukesw.net/articles/GlassButton.aspx如何使玻璃按鈕忽略窗體的漸變?

我有這個按鈕,唯一的問題是,如果我申請的梯度來我的形式會影響按鈕的顏色,所以它不是完全是我在設計時選擇。我不知道是否我使用的代碼來應用造成這種情況的表單漸變,或者如果按鈕不是完全不透明或什麼。我嘗試了一下按鈕代碼,但沒有得到任何答案。您可以在我上面發佈的鏈接中獲取該按鈕的代碼。下面是我用我的形式梯度位於窗體本身對現在的代碼:

private Color _Color1 = Color.Gainsboro; 
    private Color _Color2 = Color.Blue; 
    private float _ColorAngle = 60f; 

    public Color Color1 
    { 

     get { return _Color1; } 
     set { 
      _Color1 = value; 
      this.Invalidate(); // Tell the Form to repaint itself 
     } 
    } 

    public Color Color2 
    { 
     get { return _Color2; } 
     set { 
      _Color2 = value; 
      this.Invalidate(); // Tell the Form to repaint itself 
     } 
    } 

    public float ColorAngle 
    { 
     get { return _ColorAngle; } 
     set { 
      _ColorAngle = value; 
      this.Invalidate(); // Tell the Form to repaint itself 
     } 
    } 

    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 
     Graphics g = pevent.Graphics; 
     Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height); 

     System.Drawing.Drawing2D.LinearGradientBrush bBackground 
      = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, 
               _Color1, _Color2, _ColorAngle); 

     g.FillRectangle(bBackground, rBackground); 
     bBackground.Dispose(); 
    } 

我如何能得到這個按鈕在運行時顯示相同的,因爲它確實在設計時任何指針將不勝感激!

+0

這是該按鈕是不完全不透明 - 這就是所謂的玻璃按鈕,因爲你可以透過它看到。 – Ryan 2012-02-29 04:40:07

+0

@minitech這當然是有道理的,但我只是希望我知道在按鈕代碼中要改變什麼才能使它變得不透明。我對光澤外觀更感興趣,而不是實際看到:/ – 2012-02-29 04:43:00

+0

僅供參考:我顯然現在不能對此加以獎勵,但是我會向一些人提出一些賞金點,因爲我知道這一點需要比平常做更多的工作,不得不去按鈕和所有的源。 – 2012-02-29 04:44:56

回答

2

GlowButton.csDrawButtonBackground方法,只是改變不透明度爲完全不透明(255):

#region " content " 
using (GraphicsPath bb = CreateRoundRectangle(rect, 2)) 
{ 
    //int opacity = pressed ? 0xcc : 0x7f; 
    int opacity = 255; 
    using (Brush br = new SolidBrush(Color.FromArgb(opacity, backColor))) 
    { 
     g.FillPath(br, bb); 
    } 
} 
#endregion 
+1

啊......我試圖把不透明度設置爲100而不是255.謝謝! – 2012-02-29 04:58:38