How do I put text on ProgressBar?

后端 未结 9 1374
一向
一向 2020-11-28 07:23

I have used ProgressBar Control in my c# desktop application.I have used it in a thread other then the thread in which control has been declared.Its working Fine. Now I am w

9条回答
  •  迷失自我
    2020-11-28 07:48

    I tried placing a label with transparent background over a progress bar but never got it to work properly. So I found Barry's solution here very useful, although I missed the beautiful Vista style progress bar. So I merged Barry's solution with http://www.dreamincode.net/forums/topic/243621-percent-into-progress-bar/ and managed to keep the native progress bar, while displaying text percentage or custom text over it. I don't see any flickering in this solution either. Sorry to dig up and old thread but I needed this today and so others may need it too.

    public enum ProgressBarDisplayText
    {
        Percentage,
        CustomText
    }
    
    class ProgressBarWithCaption : ProgressBar
    {
        //Property to set to decide whether to print a % or Text
        private ProgressBarDisplayText m_DisplayStyle;
        public ProgressBarDisplayText DisplayStyle {
            get { return m_DisplayStyle; }
            set { m_DisplayStyle = value; }
        }
    
        //Property to hold the custom text
        private string m_CustomText;
        public string CustomText {
            get { return m_CustomText; }
            set {
                m_CustomText = value;
                this.Invalidate();
            }
        }
    
        private const int WM_PAINT = 0x000F;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(m);
    
            switch (m.Msg) {
                case WM_PAINT:
                    int m_Percent = Convert.ToInt32((Convert.ToDouble(Value) / Convert.ToDouble(Maximum)) * 100);
                    dynamic flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
    
                    using (Graphics g = Graphics.FromHwnd(Handle)) {
                        using (Brush textBrush = new SolidBrush(ForeColor)) {
    
                            switch (DisplayStyle) {
                                case ProgressBarDisplayText.CustomText:
                                    TextRenderer.DrawText(g, CustomText, new Font("Arial", Convert.ToSingle(8.25), FontStyle.Regular), new Rectangle(0, 0, this.Width, this.Height), Color.Black, flags);
                                    break;
                                case ProgressBarDisplayText.Percentage:
                                    TextRenderer.DrawText(g, string.Format("{0}%", m_Percent), new Font("Arial", Convert.ToSingle(8.25), FontStyle.Regular), new Rectangle(0, 0, this.Width, this.Height), Color.Black, flags);
                                    break;
                            }
    
                        }
                    }
    
                    break;
            }
    
        }
    
    }
    

提交回复
热议问题