c#在WinForm中重写ProgressBar控件(带%的显示)

本文介绍如何在C# WinForm中通过重写ProgressBar控件实现带有百分比显示的功能。利用System.Windows.Forms.ProgressBar控件并结合底层绘图API,实现了自定义进度条样式和文本颜色。

c#在WinForm中重写ProgressBar控件(带%的显示)

2009-05-14 13:13

 

    #region 定义textProgressBar控件的类

    namespace csPublish

    {

        [ToolboxItem(true)]

        class textProgressBar : System.Windows.Forms.ProgressBar

        {

            [System.Runtime.InteropServices.DllImport("user32.dll ")]

            static extern IntPtr GetWindowDC(IntPtr hWnd);

            [System.Runtime.InteropServices.DllImport("user32.dll ")]

            static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

            private System.Drawing.Color _TextColor = System.Drawing.Color.Black;

            private System.Drawing.Font _TextFont = new System.Drawing.Font("SimSun ", 12);

            public System.Drawing.Color TextColor

            {

                get { return _TextColor; }

                set { _TextColor = value; this.Invalidate(); }

            }

            public System.Drawing.Font TextFont

            {

                get { return _TextFont; }

                set { _TextFont = value; this.Invalidate(); }

            }

            protected override void WndProc(ref   Message m)

            {

                base.WndProc(ref   m);

                if (m.Msg == 0xf || m.Msg == 0x133)

                {

                    //拦截系统消息,获得当前控件进程以便重绘。  

                    //一些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint方法将不起作用.  

                    //所有这里并没有使用重载OnPaint方法绘制TextBox边框。  

                    //  

                    //MSDN:重写   OnPaint   将禁止修改所有控件的外观。  

                    //那些由   Windows   完成其所有绘图的控件(例如   Textbox)从不调用它们的   OnPaint   方法,  

                    //因此将永远不会使用自定义代码。请参见您要修改的特定控件的文档,  

                    //查看   OnPaint   方法是否可用。如果某个控件未将   OnPaint   作为成员方法列出,  

                    //则您无法通过重写此方法改变其外观。  

                    //  

                    //MSDN:要了解可用的   Message.Msg、Message.LParam   和   Message.WParam   值,  

                    //请参考位于   MSDN   Library   中的   Platform   SDK   文档参考。可在   Platform   SDK(“Core   SDK”一节)  

                    //下载中包含的   windows.h   头文件中找到实际常数值,该文件也可在   MSDN   上找到。  

                    IntPtr hDC = GetWindowDC(m.HWnd);

                    if (hDC.ToInt32() == 0)

                    {

                        return;

                    }

                    //base.OnPaint(e);

                    System.Drawing.Graphics g = Graphics.FromHdc(hDC);

                    SolidBrush brush = new SolidBrush(_TextColor);

                    string s = string.Format("{0}%", this.Value * 100 / this.Maximum);

                    SizeF size = g.MeasureString(s, _TextFont);

                    float x = (this.Width - size.Width) / 2;

                    float y = (this.Height - size.Height) / 2;

                    g.DrawString(s, _TextFont, brush, x, y);

                    //返回结果  

                    m.Result = IntPtr.Zero;

                    //释放  

                    ReleaseDC(m.HWnd, hDC);

                }

            }

        }

    }

    #endregion

转载于:https://www.cnblogs.com/carekee/articles/2321985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值