亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在C#.NET 3.5中更改Progressbar的顏色?

如何在C#.NET 3.5中更改Progressbar的顏色?

慕工程0101907 2019-11-27 13:07:27
我想在進度欄上做兩件事。將綠色更改為紅色。卸下磚塊,使其成為一種顏色。我不知道如何完成有關這兩件事的任何信息,我們將不勝感激!謝謝。
查看完整描述

3 回答

?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

這是最受接受的代碼的無閃爍版本,您可以找到該問題的答案。所有這些都歸功于這些致命答案的發布者。感謝Dusty,Chris,Matt和Josh!


就像其中一個評論中“ Fueled”的請求一樣,我還需要一個表現得更...專業的版本。此代碼與以前的代碼一樣保留樣式,但是添加了屏幕外圖像渲染和圖形緩沖(并正確處理了圖形對象)。


結果:一切正常,沒有閃爍。:)


public class NewProgressBar : ProgressBar

{

    public NewProgressBar()

    {

        this.SetStyle(ControlStyles.UserPaint, true);

    }


    protected override void OnPaintBackground(PaintEventArgs pevent)

    {

        // None... Helps control the flicker.

    }


    protected override void OnPaint(PaintEventArgs e)

    {

        const int inset = 2; // A single inset value to control teh sizing of the inner rect.


        using (Image offscreenImage = new Bitmap(this.Width, this.Height))

        {

            using (Graphics offscreen = Graphics.FromImage(offscreenImage))

            {

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


                if (ProgressBarRenderer.IsSupported)

                    ProgressBarRenderer.DrawHorizontalBar(offscreen, rect);


                rect.Inflate(new Size(-inset, -inset)); // Deflate inner rect.

                rect.Width = (int)(rect.Width * ((double)this.Value / this.Maximum));

                if (rect.Width == 0) rect.Width = 1; // Can't draw rec with width of 0.


                LinearGradientBrush brush = new LinearGradientBrush(rect, this.BackColor, this.ForeColor, LinearGradientMode.Vertical);

                offscreen.FillRectangle(brush, inset, inset, rect.Width, rect.Height);


                e.Graphics.DrawImage(offscreenImage, 0, 0);

                offscreenImage.Dispose();

            }

        }

    }

}


查看完整回答
反對 回復 2019-11-27
  • 3 回答
  • 0 關注
  • 1217 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號