zoukankan      html  css  js  c++  java
  • c#在WinForm中重写ProgressBar控件(带%的显示)

        #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

  • 相关阅读:
    [BZOJ4869][洛谷P3747][六省联考2017]相逢是问候(线段树)
    [WC2014][BZOJ3435][洛谷P3920]紫荆花之恋(动态点分治+treap)
    JavaScript对象JQuery In Action
    每日一条SQL LEFT JOIN
    Div border 显示不出来的原因
    HTML列表标记:dl、dt、dd
    The Effective Executive 笔记 一
    c# 解析JSON的几种办法
    使用if else 容易犯的错
    每日一句SQL:内联视图
  • 原文地址:https://www.cnblogs.com/zhangq/p/3929249.html
Copyright © 2011-2022 走看看