none
使用DWM 做毛玻璃后,按钮的字体颜色发生改变?该这么解决? RRS feed

答案

  • 你好,

    此问题已经在下面这个帖子里面讨论过并给出了答案,你可以点击进行查看,但其中的代码是VB。NET版本的,下面的代码是C#版本的。

    此方法为自定义控件,重写WndProc事件处理方法,来达到预期的效果。

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/316a178e-252b-480d-8cc9-85814c2073d8/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;

    namespace CSWinFormExAeroToClient
    {
        /// <summary>
        /// Text box that will redraw itself as a graphic on top itself which will work with the
        /// Aero Glass effect.
        /// </summary>
        public class AeroTextBox : TextBox
        {

            /// <summary>
            /// Constructor
            /// </summary>
            /// <remarks></remarks>

            //public AeroTextBox()
            //{
            //}

            /// <summary>
            /// Processing of incoming messages.  We're going to get a bitmap of the control (as TextBox in this case) and then
            /// redraw it onto the form when the WM_PAINT message comes through.
            /// </summary>
            /// <param name="m"></param>
            /// <remarks>
            /// We're only going to repaint when the WM_PAINT event comes through, partly because I didn't have time to find the
            /// rest of the events I needed.  Instead as kind of a hack I overrode the events that cause said messages and then
            /// called Invalidate from them which will force a repaint.
            /// </remarks>
            const int WM_PAINT = 0xf;
            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                base.WndProc(ref m);

                //int WM_PAINT = 0xf;

                switch (m.Msg)
                {
                    case WM_PAINT:
                        RedrawControlAsBitmap(this.Handle);
                        break;
                }

            }

            /// <summary>
            /// Redraws a given control as a bitmap ontop of itself.
            /// </summary>
            /// <param name="hwnd"></param>
            /// <remarks></remarks>

            public void RedrawControlAsBitmap(IntPtr hwnd)
            {
                Control c = Control.FromHandle(hwnd);

                using (Bitmap bm = new Bitmap(c.Width, c.Height))
                {
                    c.DrawToBitmap(bm, c.ClientRectangle);

                    using (Graphics g = c.CreateGraphics())
                    {
                        g.DrawImage(bm, new Point(-1, -1));
                    }

                }

                c = null;

            }

            protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseDown(e);
                Invalidate();
            }

            protected override void OnMouseHover(System.EventArgs e)
            {
                base.OnMouseHover(e);
                Invalidate();
            }

            protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseClick(e);
                Invalidate();
            }

            protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseMove(e);
                Invalidate();
            }

            protected override void OnTextChanged(System.EventArgs e)
            {
                base.OnTextChanged(e);
                Invalidate();
            }

            protected override void OnMouseEnter(System.EventArgs e)
            {
                base.OnMouseEnter(e);
                Invalidate();
            }

            protected override void OnMouseLeave(System.EventArgs e)
            {
                base.OnMouseLeave(e);
                Invalidate();
            }
        }
    }

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月21日 12:49
    版主

全部回复

  • 你好,

    此问题已经在下面这个帖子里面讨论过并给出了答案,你可以点击进行查看,但其中的代码是VB。NET版本的,下面的代码是C#版本的。

    此方法为自定义控件,重写WndProc事件处理方法,来达到预期的效果。

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/316a178e-252b-480d-8cc9-85814c2073d8/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;

    namespace CSWinFormExAeroToClient
    {
        /// <summary>
        /// Text box that will redraw itself as a graphic on top itself which will work with the
        /// Aero Glass effect.
        /// </summary>
        public class AeroTextBox : TextBox
        {

            /// <summary>
            /// Constructor
            /// </summary>
            /// <remarks></remarks>

            //public AeroTextBox()
            //{
            //}

            /// <summary>
            /// Processing of incoming messages.  We're going to get a bitmap of the control (as TextBox in this case) and then
            /// redraw it onto the form when the WM_PAINT message comes through.
            /// </summary>
            /// <param name="m"></param>
            /// <remarks>
            /// We're only going to repaint when the WM_PAINT event comes through, partly because I didn't have time to find the
            /// rest of the events I needed.  Instead as kind of a hack I overrode the events that cause said messages and then
            /// called Invalidate from them which will force a repaint.
            /// </remarks>
            const int WM_PAINT = 0xf;
            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                base.WndProc(ref m);

                //int WM_PAINT = 0xf;

                switch (m.Msg)
                {
                    case WM_PAINT:
                        RedrawControlAsBitmap(this.Handle);
                        break;
                }

            }

            /// <summary>
            /// Redraws a given control as a bitmap ontop of itself.
            /// </summary>
            /// <param name="hwnd"></param>
            /// <remarks></remarks>

            public void RedrawControlAsBitmap(IntPtr hwnd)
            {
                Control c = Control.FromHandle(hwnd);

                using (Bitmap bm = new Bitmap(c.Width, c.Height))
                {
                    c.DrawToBitmap(bm, c.ClientRectangle);

                    using (Graphics g = c.CreateGraphics())
                    {
                        g.DrawImage(bm, new Point(-1, -1));
                    }

                }

                c = null;

            }

            protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseDown(e);
                Invalidate();
            }

            protected override void OnMouseHover(System.EventArgs e)
            {
                base.OnMouseHover(e);
                Invalidate();
            }

            protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseClick(e);
                Invalidate();
            }

            protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
            {
                base.OnMouseMove(e);
                Invalidate();
            }

            protected override void OnTextChanged(System.EventArgs e)
            {
                base.OnTextChanged(e);
                Invalidate();
            }

            protected override void OnMouseEnter(System.EventArgs e)
            {
                base.OnMouseEnter(e);
                Invalidate();
            }

            protected override void OnMouseLeave(System.EventArgs e)
            {
                base.OnMouseLeave(e);
                Invalidate();
            }
        }
    }

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月21日 12:49
    版主
  • 效果是有@!但是看起来很生硬!没有预想那么好!
    2011年4月4日 10:59