皮肤包涵暗中认可控件

好久没来今日头条,明日捣鼓到近些日子正是为着把在此之前的皮层控件完善好,

那篇文章首要介绍了WinForm中DefWndProc、WndProc与IMessageFilter的界别,较为详细的深入分析了WinForm的音讯处理体制,须求的仇敌能够参照下

事先也看了好些个技艺文章,大许多都是和谐重写系统控件落成换肤,大概从未像东日的(IrisSkin)控件同样增加四个零部件
把系统的肌肤全换掉,小编一度也是重写系统的控件,但本人就喜欢瞎捣鼓,于是就起来找那上头的素材,苦于没学过底层,对windows窗口
以及新闻循环机制不精晓,找资料也基本上白搭了持久,但结尾感觉自身能体悟的最笨的格局完成了继续了协和写的父窗口,那么窗口增加的控件正是协和画的 却非系统画的,

 

澳门1495娱乐,先上传一张效果图:

 

澳门1495娱乐 1

貌似的话,Winform的新闻管理体制多数时候是透过事件管理程序举行的,但当未有对应的风云时平日的做法是声称DefWndProc大概WndProc或然IMessageFilter,平日在英特网看见有成文将三者并列,那么它们有如何分别呢?本文对此做一归纳深入分析如下:

 

DefWndProc和WndProc都以承袭自Control类中的虚方法,其原型如下:

 

?

整日效力应该也还算勉强吧,近些日子自己也就写了那多少个控件的吹捧,尽管那破代码捣鼓数天了,但代码照旧混乱不堪,加上反编写翻译了微软的一些东西,因为想趁早出来,代码都没看,直接复制过来修改成编写翻译不报错就完了了,

1
2
3
4
5
6
7
8
9
10
11
protected override void DefWndProc(ref Message m)
{
 ....
   base.DefWndProc(m);
}
   
protected override void WndProc(ref Message m);
{
 .....
   base.WndProc(m);
}

 

具有的有用户分界面包车型地铁控件都卫冕自Control,这种措施索要成立对应控件的派生类,不能够集结对一一窗口的新闻进行阻挠处理,因为从根本上说这两侧都以Windows的窗口进程,独有收纳针对本窗口自个儿的消息。

后天本人的话说本人实现的思路:

经过复习Windows的消息管理机制,对那三者的关系得以有越来越好的明白。应用程序的新闻来自于系统新闻队列,被应用程序的主程序中的音信循环所管理。那几个新闻循环从应用程序的音信队列中抽取新闻,举办预处理,然后派发到音讯对应的窗口进程,窗口进度在被调用后基于音讯的种类实行对应的拍卖,有些能够由Windows暗许管理的新闻就调用Windows的DefWindowProc。

前题条件继续的是友好写的FormBase
下一场重写了系统的OnControlAdded 方法,在那边为每一种控件增添美化的类,

那边的WndProc正是对应控件窗口的窗口进程,而DefWndProc会被WndProc调用,管理那一个WndProc中未处理的消息(包含WndProc未吞掉的),由此DefWndProc收到的新闻会比WndProc少。

可怎么加多了?
找了长久,找到了贰个可阻拦Control消息的接口:IWindowTarget
接口(具体请仿照效法
MSDN.aspx) 微软不建议直接行使的一个类)

IMessageFilter的调用发生在应用程序的新闻循环中,是新闻预管理的一有的,所以它接受的信息是更全的(除了间接发送到窗口过程不进去消息队列的那几个音讯)。动用形式如下:

Control 公开并同意修改那么些接口的音信,

?

IWindowTarget
接口有多个措施,三个是退换控件句柄的(大概应该叫设置控件句柄)
还应该有三个便是拍卖 Windows 消息的OnMessage
 小编要做的便是掣肘Windows 新闻的重画音讯,拦截下来
然后本身画,另外的如故丢个控件管理,(后来发觉一般完毕IMessageFilter接口也足以兑现)

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MessageFilter : IMessageFilter
{
 public bool PreFilterMessage(ref Message msg)
 {
   //识别消息并处理
  //return true;//吞掉消息,不派发
   return false;//进入下一步派发到对应窗口过程
 }
}
   
//在应用程序消息循环中加入消息过滤器
MessageFilter f = new MessageFilter(this.lbMsg);
Application.AddMessageFilter(f);

好了屁话比非常少说了 上代码 上demo 没写完  希望大神能帮助完善下就最棒了-.-!

三者都有一个一块的参数类型Message,它包裹了Windows消息。同一时间还包含四个比异常低价的ToString方法,能够将Message对象转变来包含新闻明称(WM_XXX)在内的字符串,通过Reflector能够看来落成是经过贰个内部类MessageDecoder,使用贰个相当短的switch语句将信息ID调换到新知名称。

对了
正是问个2B主题材料,怎么拦截全数的Form成立只怕Form创建句柄时?小编用IMessageFilter好像拦截不到,

Message的概念如下:

唉 代码有一些乱,各位将就着看呢

?

一部分代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[StructLayout(LayoutKind.Sequential), SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public struct Message
{
   private IntPtr hWnd;
   private int msg;
   private IntPtr wparam;
   private IntPtr lparam;
   private IntPtr result;
   public IntPtr HWnd { get; set; }
   public int Msg { get; set; }
   public IntPtr WParam { get; set; }
   public IntPtr LParam { get; set; }
   public IntPtr Result { get; set; }
   public object GetLParam(Type cls);
   public static Message Create(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
   public override bool Equals(object o);
   public static bool operator !=(Message a, Message b);
   public static bool operator ==(Message a, Message b);
   public override int GetHashCode();
   public override string ToString();
}
  

澳门1495娱乐 2澳门1495娱乐 3

中间hWnd是音讯对应的窗口句柄,分局方的分析可以知道在窗口过程(DefWndProc,WndProc)中收取的窗口句柄都是该窗口的句柄,而在PreFilterMessage中收受的音信的窗口句柄则基于触发消息的窗口分歧而各异。

using DotNet.Windows.Forms.Internal;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DotNet.Windows.Forms
{
    public abstract class WindowTarget<T> : IWindowTarget
        where T : Control
    {
        private static readonly ControlStyles UserControlStyles;
        private static readonly MethodInfo SetStyleMethod;
        private static readonly MethodInfo SetStateMethod;
        private static readonly PropertyInfo CacheTextInternalProperty;
        private static readonly FieldInfo windowField;

        static WindowTarget()
        {
            UserControlStyles = ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.ContainerControl | ControlStyles.UserPaint;
            SetStyleMethod = typeof(Control).GetMethod("SetStyle",BindingFlags.Instance |BindingFlags.NonPublic);
            SetStateMethod = typeof(Control).GetMethod("SetState", BindingFlags.Instance | BindingFlags.NonPublic);
            windowField = typeof(Control).GetField("window", BindingFlags.Instance | BindingFlags.NonPublic);
            CacheTextInternalProperty = typeof(Control).GetProperty("CacheTextInternal", BindingFlags.Instance | BindingFlags.NonPublic);
        }
        private T m_Control;
        private IWindowTarget m_WindowTarget;
        private MouseState m_MouseState = MouseState.Normal;
        protected WindowTarget(T control)
        {
            m_Control = control;
            m_WindowTarget = m_Control.WindowTarget;
            m_Control.MouseUp += (o, e) => { this.MouseState = MouseState.Up; };
            m_Control.MouseEnter += (o, e) => { this.MouseState = MouseState.Move; };
            m_Control.MouseLeave += (o, e) => { this.MouseState = MouseState.Leave; };
            m_Control.MouseDown += (o, e) => { this.MouseState = MouseState.Down; };
        }
        protected virtual void SetStyle(ControlStyles styles, bool value)
        {
            SetStyleMethod.Invoke(Control, new object[] { styles, value });
        }
        private object window
        {
            get
            {
                return windowField.GetValue(Control);
            }
        }
        private bool CacheTextInternal
        {
            get
            {
                return (bool)CacheTextInternalProperty.GetValue(Control, null);
            }
            set
            {
                CacheTextInternalProperty.SetValue(Control, value, null);
            }
        }
        protected virtual void SetState(int flag, bool value)
        {
            SetStyleMethod.Invoke(Control, new object[] { flag, value });
        }
        protected T Control { get { return m_Control; } }
        protected IWindowTarget Target { get { return m_WindowTarget; } }
        protected abstract void OnPaint(DotNet.Windows.Forms.Internal.PaintEventArgs e);
        protected MouseState MouseState
        {
            get
            {
                return m_MouseState;
            }
            set
            {
                if (m_MouseState != value)
                {
                    m_MouseState = value;
                    Control.Invalidate();
                }
            }
        }
        protected virtual Image GetImage()
        {
            switch (MouseState)
            {
                case MouseState.Leave:
                case MouseState.Normal:
                    return GetNormalImage();
                case MouseState.Up:
                case MouseState.Move:
                    return GetMoveImage();
                case MouseState.Down:
                    return GetDownImage();
                default:
                    return null;
            }
        }
        protected abstract Image GetNormalImage();
        protected abstract Image GetMoveImage();
        protected abstract Image GetDownImage();
        protected void RendererBackground(Graphics g, Rectangle rect, Image backgroundImage, bool method)
        {
            if (!method)
            {
                g.DrawImage(backgroundImage, new Rectangle(rect.X + 0, rect.Y, 5, rect.Height), 0, 0, 5, backgroundImage.Height, GraphicsUnit.Pixel);
                g.DrawImage(backgroundImage, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, rect.Height), 5, 0, backgroundImage.Width - 10, backgroundImage.Height, GraphicsUnit.Pixel);
                g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - 5, rect.Y, 5, rect.Height), backgroundImage.Width - 5, 0, 5, backgroundImage.Height, GraphicsUnit.Pixel);
            }
            else
            {
                RendererBackground(g, rect, 5, backgroundImage);
            }
        }
        /// <summary>
        /// 渲染背景图片,使背景图片不失真
        /// </summary>
        /// <param name="g"></param>
        /// <param name="rect"></param>
        /// <param name="cut"></param>
        /// <param name="backgroundImage"></param>
        protected void RendererBackground(Graphics g, Rectangle rect, int cut, Image backgroundImage)
        {
            //左上角
            g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y, cut, cut), 0, 0, cut, cut, GraphicsUnit.Pixel);
            //上边
            g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y, rect.Width - cut * 2, cut), cut, 0, backgroundImage.Width - cut * 2, cut, GraphicsUnit.Pixel);
            //右上角
            g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y, cut, cut), backgroundImage.Width - cut, 0, cut, cut, GraphicsUnit.Pixel);
            //左边
            g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y + cut, cut, rect.Height - cut * 2), 0, cut, cut, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
            //左下角
            g.DrawImage(backgroundImage, new Rectangle(rect.X, rect.Y + rect.Height - cut, cut, cut), 0, backgroundImage.Height - cut, cut, cut, GraphicsUnit.Pixel);
            //右边
            g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y + cut, cut, rect.Height - cut * 2), backgroundImage.Width - cut, cut, cut, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
            //右下角
            g.DrawImage(backgroundImage, new Rectangle(rect.X + rect.Width - cut, rect.Y + rect.Height - cut, cut, cut), backgroundImage.Width - cut, backgroundImage.Height - cut, cut, cut, GraphicsUnit.Pixel);
            //下边
            g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y + rect.Height - cut, rect.Width - cut * 2, cut), cut, backgroundImage.Height - cut, backgroundImage.Width - cut * 2, cut, GraphicsUnit.Pixel);
            //平铺中间
            g.DrawImage(backgroundImage, new Rectangle(rect.X + cut, rect.Y + cut, rect.Width - cut * 2, rect.Height - cut * 2), cut, cut, backgroundImage.Width - cut * 2, backgroundImage.Height - cut * 2, GraphicsUnit.Pixel);
        }
        #region IWindowTarget 成员
        void IWindowTarget.OnHandleChange(IntPtr newHandle)
        {
            Target.OnHandleChange(newHandle);
        }
        private BufferedGraphicsContext BufferContext
        {
            get
            {
                return BufferedGraphicsManager.Current;
            }
        }
        internal static IntPtr SetUpPalette(IntPtr dc, bool force, bool realizePalette)
        {
            IntPtr halftonePalette = Graphics.GetHalftonePalette();
            IntPtr ptr2 = SafeNativeMethods.SelectPalette(new HandleRef(null, dc), new HandleRef(null, halftonePalette), force ? 0 : 1);
            if ((ptr2 != IntPtr.Zero) && realizePalette)
            {
                SafeNativeMethods.RealizePalette(new HandleRef(null, dc));
            }
            return ptr2;
        }
        private void WmPaint(ref  Message m)
        {
            bool flag = true;
            IntPtr zero = IntPtr.Zero;
            NativeMethods.PAINTSTRUCT lpPaint = new NativeMethods.PAINTSTRUCT();
            bool flag2 = false;
            try
            {
                IntPtr wParam;
                Rectangle clientRectangle;
                if (m.WParam == IntPtr.Zero)
                {
                    zero = Control.Handle;
                    wParam = UnsafeNativeMethods.BeginPaint(new HandleRef(this, zero), ref lpPaint);
                    flag2 = true;
                    clientRectangle = new Rectangle(lpPaint.rcPaint_left, lpPaint.rcPaint_top, lpPaint.rcPaint_right - lpPaint.rcPaint_left, lpPaint.rcPaint_bottom - lpPaint.rcPaint_top);
                }
                else
                {
                    wParam = m.WParam;
                    clientRectangle = Control.ClientRectangle;
                }
                if (!flag || ((clientRectangle.Width > 0) && (clientRectangle.Height > 0)))
                {
                    IntPtr handle = IntPtr.Zero;
                    BufferedGraphics graphics = null;
                    DotNet.Windows.Forms.Internal.PaintEventArgs e = null;
                    System.Drawing.Drawing2D.GraphicsState gstate = null;
                    try
                    {
                        if (flag || (m.WParam == IntPtr.Zero))
                        {
                            handle = SetUpPalette(wParam, false, false);
                        }
                        if (flag)
                        {
                            try
                            {
                                graphics = this.BufferContext.Allocate(wParam, Control.ClientRectangle);
                            }
                            catch (Exception exception)
                            {
                                if (ClientUtils.IsCriticalException(exception))
                                {
                                    throw;
                                }
                                flag = false;
                            }
                        }
                        if (graphics != null)
                        {
                            graphics.Graphics.SetClip(clientRectangle);
                            e = new DotNet.Windows.Forms.Internal.PaintEventArgs(graphics.Graphics, clientRectangle);
                            gstate = e.Graphics.Save();
                        }
                        else
                        {
                            e = new DotNet.Windows.Forms.Internal.PaintEventArgs(wParam, clientRectangle);
                        }
                        using (e)
                        {
                            try
                            {
                                if (((m.WParam == IntPtr.Zero) && true) || flag)
                                {
                                    this.PaintWithErrorHandling(e, 1);
                                }
                            }
                            finally
                            {
                                if (gstate != null)
                                {
                                    e.Graphics.Restore(gstate);
                                }
                                else
                                {
                                    e.ResetGraphics();
                                }
                            }
                            this.PaintWithErrorHandling(e, 2);
                            if (graphics != null)
                            {
                                graphics.Render();
                            }
                        }
                    }
                    finally
                    {
                        if (handle != IntPtr.Zero)
                        {
                            SafeNativeMethods.SelectPalette(new HandleRef(null, wParam), new HandleRef(null, handle), 0);
                        }
                        if (graphics != null)
                        {
                            graphics.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (flag2)
                {
                    UnsafeNativeMethods.EndPaint(new HandleRef(this, zero), ref lpPaint);
                }
            }
        }
        protected virtual void OnPaintBackground(DotNet.Windows.Forms.Internal.PaintEventArgs pevent)
        {
            NativeMethods.RECT rect = new NativeMethods.RECT();
            UnsafeNativeMethods.GetClientRect(new HandleRef(this.window, Control.Handle), ref rect);
            this.PaintBackground(pevent, new Rectangle(rect.left, rect.top, rect.right, rect.bottom));
        }
        internal void PaintBackground(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle)
        {
            this.PaintBackground(e, rectangle, Control.BackColor, Point.Empty);
        }

        internal void PaintBackground(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle, System.Drawing.Color backColor)
        {
            this.PaintBackground(e, rectangle, backColor, Point.Empty);
        }
        private bool RenderColorTransparent(System.Drawing.Color c)
        {
            return ((c.A < 0xff));
        }
        internal void PaintTransparentBackground(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle)
        {
            this.PaintTransparentBackground(e, rectangle, null);
        }
        internal static bool IsImageTransparent(Image backgroundImage)
        {
            return ((backgroundImage != null) && ((backgroundImage.Flags & 2) > 0));
        }
        internal static Rectangle CalculateBackgroundImageRectangle(Rectangle bounds, Image backgroundImage, ImageLayout imageLayout)
        {
            Rectangle rectangle = bounds;
            if (backgroundImage != null)
            {
                switch (imageLayout)
                {
                    case ImageLayout.None:
                        rectangle.Size = backgroundImage.Size;
                        return rectangle;

                    case ImageLayout.Tile:
                        return rectangle;

                    case ImageLayout.Center:
                        {
                            rectangle.Size = backgroundImage.Size;
                            Size size = bounds.Size;
                            if (size.Width > rectangle.Width)
                            {
                                rectangle.X = (size.Width - rectangle.Width) / 2;
                            }
                            if (size.Height > rectangle.Height)
                            {
                                rectangle.Y = (size.Height - rectangle.Height) / 2;
                            }
                            return rectangle;
                        }
                    case ImageLayout.Stretch:
                        rectangle.Size = bounds.Size;
                        return rectangle;

                    case ImageLayout.Zoom:
                        {
                            Size size2 = backgroundImage.Size;
                            float num = ((float)bounds.Width) / ((float)size2.Width);
                            float num2 = ((float)bounds.Height) / ((float)size2.Height);
                            if (num >= num2)
                            {
                                rectangle.Height = bounds.Height;
                                rectangle.Width = (int)((size2.Width * num2) + 0.5);
                                if (bounds.X >= 0)
                                {
                                    rectangle.X = (bounds.Width - rectangle.Width) / 2;
                                }
                                return rectangle;
                            }
                            rectangle.Width = bounds.Width;
                            rectangle.Height = (int)((size2.Height * num) + 0.5);
                            if (bounds.Y >= 0)
                            {
                                rectangle.Y = (bounds.Height - rectangle.Height) / 2;
                            }
                            return rectangle;
                        }
                }
            }
            return rectangle;
        }
        internal static void DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (backgroundImageLayout == ImageLayout.Tile)
            {
                using (TextureBrush brush = new TextureBrush(backgroundImage, WrapMode.Tile))
                {
                    if (scrollOffset != Point.Empty)
                    {
                        Matrix transform = brush.Transform;
                        transform.Translate((float)scrollOffset.X, (float)scrollOffset.Y);
                        brush.Transform = transform;
                    }
                    g.FillRectangle(brush, clipRect);
                    return;
                }
            }
            Rectangle rect = CalculateBackgroundImageRectangle(bounds, backgroundImage, backgroundImageLayout);
            if ((rightToLeft == RightToLeft.Yes) && (backgroundImageLayout == ImageLayout.None))
            {
                rect.X += clipRect.Width - rect.Width;
            }
            using (SolidBrush brush2 = new SolidBrush(backColor))
            {
                g.FillRectangle(brush2, clipRect);
            }
            if (!clipRect.Contains(rect))
            {
                if ((backgroundImageLayout == ImageLayout.Stretch) || (backgroundImageLayout == ImageLayout.Zoom))
                {
                    rect.Intersect(clipRect);
                    g.DrawImage(backgroundImage, rect);
                }
                else if (backgroundImageLayout == ImageLayout.None)
                {
                    rect.Offset(clipRect.Location);
                    Rectangle destRect = rect;
                    destRect.Intersect(clipRect);
                    Rectangle rectangle3 = new Rectangle(Point.Empty, destRect.Size);
                    g.DrawImage(backgroundImage, destRect, rectangle3.X, rectangle3.Y, rectangle3.Width, rectangle3.Height, GraphicsUnit.Pixel);
                }
                else
                {
                    Rectangle rectangle4 = rect;
                    rectangle4.Intersect(clipRect);
                    Rectangle rectangle5 = new Rectangle(new Point(rectangle4.X - rect.X, rectangle4.Y - rect.Y), rectangle4.Size);
                    g.DrawImage(backgroundImage, rectangle4, rectangle5.X, rectangle5.Y, rectangle5.Width, rectangle5.Height, GraphicsUnit.Pixel);
                }
            }
            else
            {
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetWrapMode(WrapMode.TileFlipXY);
                g.DrawImage(backgroundImage, rect, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, imageAttr);
                imageAttr.Dispose();
            }
        }
        internal void PaintTransparentBackground(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle, System.Drawing.Region transparentRegion)
        {
            Graphics g = e.Graphics;
            Control parentInternal = Control.Parent;
            if (parentInternal != null)
            {
                if (Application.RenderWithVisualStyles) // parentInternal.RenderTransparencyWithVisualStyles)
                {
                    System.Drawing.Drawing2D.GraphicsState gstate = null;
                    if (transparentRegion != null)
                    {
                        gstate = g.Save();
                    }
                    try
                    {
                        if (transparentRegion != null)
                        {
                            g.Clip = transparentRegion;
                        }
                        ButtonRenderer.DrawParentBackground(g, rectangle, Control);
                        return;
                    }
                    finally
                    {
                        if (gstate != null)
                        {
                            g.Restore(gstate);
                        }
                    }
                }
                Rectangle rectangle2 = new Rectangle(-Control.Left, -Control.Top, parentInternal.Width, parentInternal.Height);
                Rectangle clipRect = new Rectangle(rectangle.Left + Control.Left, rectangle.Top + Control.Top, rectangle.Width, rectangle.Height);
                using (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(g))
                {
                    graphics2.DeviceContext.TranslateTransform(-Control.Left, -Control.Top);
                    using (DotNet.Windows.Forms.Internal.PaintEventArgs args = new DotNet.Windows.Forms.Internal.PaintEventArgs(graphics2.GetHdc(), clipRect))
                    {
                        if (transparentRegion != null)
                        {
                            args.Graphics.Clip = transparentRegion;
                            args.Graphics.TranslateClip(-rectangle2.X, -rectangle2.Y);
                        }
                        try
                        {
                            //this.InvokePaintBackground(parentInternal, args);
                            //this.InvokePaint(parentInternal, args);
                        }
                        finally
                        {
                            if (transparentRegion != null)
                            {
                                args.Graphics.TranslateClip(rectangle2.X, rectangle2.Y);
                            }
                        }
                    }
                    return;
                }
            }
            g.FillRectangle(SystemBrushes.Control, rectangle);
        }
        internal void PaintBackground(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle, System.Drawing.Color backColor, Point scrollOffset)
        {
            backColor = Color.Transparent;
            //ControlStyles.SupportsTransparentBackColor;
            this.PaintTransparentBackground(e, rectangle);
            //if (this.RenderColorTransparent(backColor))
            //{
            //    this.PaintTransparentBackground(e, rectangle);
            //}
            bool flag = ((Control is FormBase) || (this is MdiClient)) && Control.IsMirrored;
            if (((Control.BackgroundImage != null) && !SystemInformation.HighContrast) && !flag)
            {
                if ((Control.BackgroundImageLayout == ImageLayout.Tile) && IsImageTransparent(Control.BackgroundImage))
                {
                    PaintTransparentBackground(e, rectangle);
                }
                Point autoScrollPosition = scrollOffset;
                if ((Control is ScrollableControl) && (autoScrollPosition != Point.Empty))
                {
                    autoScrollPosition = ((ScrollableControl)(Control)Control).AutoScrollPosition;
                }
                if (IsImageTransparent(Control.BackgroundImage))
                {
                    PaintBackColor(e, rectangle, backColor);
                }
                DrawBackgroundImage(e.Graphics, Control.BackgroundImage, backColor, Control.BackgroundImageLayout, Control.ClientRectangle, rectangle, autoScrollPosition, Control.RightToLeft);
            }
            else
            {
                PaintBackColor(e, rectangle, backColor);
            }
        }
        private static void PaintBackColor(DotNet.Windows.Forms.Internal.PaintEventArgs e, Rectangle rectangle, System.Drawing.Color backColor)
        {
            System.Drawing.Color nearestColor = backColor;
            if (nearestColor.A == 0xff)
            {
                using (WindowsGraphics graphics = ((e.HDC != IntPtr.Zero) && ((short)Screen.PrimaryScreen.BitsPerPixel > 8)) ? WindowsGraphics.FromHdc(e.HDC) : WindowsGraphics.FromGraphics(e.Graphics))
                {
                    nearestColor = graphics.GetNearestColor(nearestColor);
                    using (WindowsBrush brush = new WindowsSolidBrush(graphics.DeviceContext, nearestColor))
                    {
                        graphics.FillRectangle(brush, rectangle);
                    }
                    return;
                }
            }
            if (nearestColor.A > 0)
            {
                using (Brush brush2 = new SolidBrush(nearestColor))
                {
                    e.Graphics.FillRectangle(brush2, rectangle);
                }
            }
        }
        private void PaintWithErrorHandling(DotNet.Windows.Forms.Internal.PaintEventArgs e, short layer)
        {
            try
            {
                this.CacheTextInternal = true;
                bool flag = true;
                try
                {
                    switch (layer)
                    {
                        case 1:
                            this.OnPaintBackground(e);
                            break;
                        case 2:
                            this.OnPaint(e);
                            break;
                    }
                    flag = false;
                }
                finally
                {
                    if (flag)
                    {
                        this.SetState(0x400000, true);
                        Control.Invalidate();
                    }
                }
            }
            finally
            {
                this.CacheTextInternal = false;
            }
        }
        void IWindowTarget.OnMessage(ref Message m)
        {
            if (m.Msg == 15)
            {
                WmPaint(ref m);
                return;
            }
            Target.OnMessage(ref m);
        }

        #endregion
    }
}

在PreFilterMessage中接到消息时,能够采纳Control.FromHandle获得窗口对应的控件对象,原型如下:

View Code

?

 

1
2
3
4
5
6
7
8
9
10
//Declaring Type: System.Windows.Forms.Control //Assembly: System.Windows.Forms, Version=2.0.0.0 public static Control FromHandle(IntPtr handle);通过这种方式可以监测各消息的信息来自哪个控件。
public bool PreFilterMessage(ref Message msg)
{
   Control c = Control.FromHandle(msg.HWnd);
   if (c == null)
      System.Diagnostics.Debug.WriteLine("Filter:NULL" +"-" + msg.ToString());
   else
      System.Diagnostics.Debug.WriteLine("Filter:" +c.Name+"-"+ msg.ToString());
   return false;
}

 

从Visual Studio的出口窗口监视到的调和输出如下图所示:

 

澳门1495娱乐 4

http://files.cnblogs.com/dotnet-org-cn/DotNet.Framework.rar

 

最后打个小广告:中夏族民共和国.NET组织(http://www.dotnet.org.cn
Tencent企鹅群:45132984澳门1495娱乐 5
天涯论坛地址:http://http://www.cnblogs.com/dotnet-org-cn
     国内独一一个以非盈利的.NET协会,致力营造国内具有权威性、价值性的.NET组织。