使用C#实现Windows下无焦点窗体

        #region 无焦点窗体

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private extern static IntPtr SetActiveWindow(IntPtr handle);
        private const int WM_ACTIVATE = 0x006;
        private const int WM_ACTIVATEAPP = 0x01C;
        private const int WM_NCACTIVATE = 0x086;
        private const int WA_INACTIVE = 0;
        private const int WM_MOUSEACTIVATE = 0x21;
        private const int MA_NOACTIVATE = 3;
        protected override void WndProc(ref Message m)
        {
            if(m.Msg == WM_MOUSEACTIVATE)
            {
                m.Result = new IntPtr(MA_NOACTIVATE);
                return;
            }
            else if(m.Msg == WM_NCACTIVATE)
            {
                if(((int)m.WParam & 0xFFFF) != WA_INACTIVE)
                {
                    if(m.LParam != IntPtr.Zero)
                    {
                        SetActiveWindow(m.LParam);
                    }
                    else
                    {
                        SetActiveWindow(IntPtr.Zero);
                    }
                }
            }
            base.WndProc(ref m);
        }

        #endregion
后面一段else if WM_NCACTIVATE是不必要的,如果窗口一开始是用ShowWindow且NOACTIVATE来启动的话。

转:http://blog.csdn.net/thirdapple/archive/2008/08/01/2755684.aspx

posted @ 2010-10-19 08:24  Treenew Lyn  阅读(2433)  评论(0编辑  收藏  举报