Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="m"></param>
        /// <returns>true if base winproc should be called</returns>
        public bool WndProcForFocus(ref Message m)
        {
            NativeMethods.WM wm = (NativeMethods.WM)m.Msg;

            if (wm != NativeMethods.WM.GETICON && wm != NativeMethods.WM.NCHITTEST &&
                wm != NativeMethods.WM.NCMOUSEMOVE && wm != NativeMethods.WM.NCMOUSELEAVE &&
                wm != NativeMethods.WM.GETTEXT && wm != NativeMethods.WM.GETTEXTLENGTH &&
                wm != NativeMethods.WM.MOUSEMOVE && wm != NativeMethods.WM.SETCURSOR &&
                wm != NativeMethods.WM.WINDOWPOSCHANGING && wm != NativeMethods.WM.WINDOWPOSCHANGED)
            {
                Log.DebugFormat("WndProcForFocus: shellhook={3}, wm={0}, wParam={1}, lParam={2}", wm, m.WParam, m.LParam, m.Msg == m_shellHookNotify);
            }
            switch (wm)
            {
            case NativeMethods.WM.ACTIVATE:
                // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646274(v=vs.85).aspx

                /*
                 * if (m.WParam.ToInt32() > 0)
                 * {
                 *  NativeMethods.BringWindowToTop(this.MainForm.Handle);
                 *  this.MainForm.FocusActiveDocument("ACTIVATE");
                 *  return true;
                 * }
                 * */
                break;

            case NativeMethods.WM.ACTIVATEAPP:
                // Never allow this window to display itself as inactive
                //NativeMethods.DefWindowProc(this.MainForm.Handle, m.Msg, (IntPtr)1, m.LParam);
                //m.Result = (IntPtr)1;
                //return false;
                //return true;
                break;

            case NativeMethods.WM.NCACTIVATE:
                // Never allow this window to display itself as inactive
                // http://msdn.microsoft.com/en-us/library/windows/desktop/ms632633(v=vs.85).aspx
                NativeMethods.DefWindowProc(this.MainForm.Handle, m.Msg, (IntPtr)1, m.LParam);
                m.Result = (IntPtr)1;
                return(false);

            case NativeMethods.WM.SYSCOMMAND:
                // Check for maximizing and restoring from maxed.
                // Removing the last 4 bits. This is necessary because
                // maximizing by double click gives you 0xF032, not 0xF030.
                switch ((int)m.WParam & 0xFFF0)
                {
                case NativeMethods.SC_MAXIMIZE:
                case NativeMethods.SC_RESTORE:
                    //Log.InfoFormat("SysCommand: {0}", m.WParam);
                    this.MainForm.BeginInvoke(new Action <string>(this.MainForm.FocusActiveDocument), "SYSCommand-Restore");
                    break;
                }
                break;

            default:

                if (m.Msg == m_shellHookNotify)
                {
                    //Log.InfoFormat("ShellHook:  param={0}", m.WParam.ToInt32());
                    switch (m.WParam.ToInt32())
                    {
                    case 4:
                    case 32772:
                        IntPtr current = NativeMethods.GetForegroundWindow();
                        if (current != this.MainForm.Handle && !this.ContainsChild(current))
                        {
                            m_externalWindow = true;
                        }
                        else if (m_externalWindow)
                        {
                            m_externalWindow = false;
                            NativeMethods.BringWindowToTop(this.MainForm.Handle);
                            this.MainForm.FocusActiveDocument("SHELLHOOK");
                            //return false;
                        }
                        else if (current == this.MainForm.Handle)
                        {
                            // round trip alt-tab or first alt-tab in 2x sequence...also when menus popup
                            //NativeMethods.SetForegroundWindow(this.MainForm.Handle);
                            //this.MainForm.FocusActiveDocument("SHELLHOOK2");
                        }
                        //{
                        //    //Log.Info("### hwd=" + this.MainForm.Handle + ", m.h=" + m.HWnd + ", blah=" + DesktopWindow.GetFirstDesktopWindow().Title);
                        //    foreach (DesktopWindow dw in DesktopWindow.GetDesktopWindows())
                        //    {
                        //        //Log.Info("blah=" + dw.Title);
                        //    }
                        //}
                        break;

                    default:
                        break;
                    }
                }
                break;
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Handles the WndProc events to enable drawing inside the title bar.
        /// </summary>
        protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            IntPtr result = IntPtr.Zero;

            NativeMethods.WM wm = (NativeMethods.WM)msg;
            if (Environment.OSVersion.Version.Major >= 6)
            {
                handled = NativeMethods.DwmDefWindowProc(hwnd, msg, wParam, lParam, out result);
            }
            if (!handled)
            {
                switch (wm)
                {
                case NativeMethods.WM.SIZE:
                    handled = false;
                    break;

                //TODO: This causes a uncontrolled moving of the window in windows 7 while dragging from  WindowState.Maximized.
                // don't paint the border and title:
                case NativeMethods.WM.NCCALCSIZE:
                    handled = true;
                    break;

                case NativeMethods.WM.SETICON:
                    handled = true;
                    return(IntPtr.Zero);

                case NativeMethods.WM.SETTEXT:
                    handled = true;
                    InvalidateArrange();
                    break;


                case NativeMethods.WM.NCACTIVATE:
                    IsWindowActive = wParam.ToInt32() == 1;
                    handled        = true;
                    result         = NativeMethods.DefWindowProc(hwnd, NativeMethods.WM.NCACTIVATE, wParam, new IntPtr(-1));
                    break;

                // determine if the titlebar, or any of the borders is under the cursor position coded in m.lParam:
                case NativeMethods.WM.NCHITTEST:
                    if (result == IntPtr.Zero)
                    {
                        WndProcHitTest(hwnd, lParam, ref handled, ref result);
                    }
                    break;


                //TODO: changing the DWMCOMPOSITION currently causes a "This freezable cannot be frozen" exception.
                case NativeMethods.WM.DWMCOMPOSITIONCHANGED:
                    SetIsGlassOnState();
                    AttachRegion();
                    InvalidateVisual();
                    UpdateLayout();
                    handled = true;
                    result  = IntPtr.Zero;
                    break;
                }
            }

            return(result);
        }