Ejemplo n.º 1
0
    private ShowDesktop() {
      // create a reference window to detect show desktop
      referenceWindow = new NativeWindow();
      CreateParams cp = new CreateParams();
      cp.ExStyle = GadgetWindow.WS_EX_TOOLWINDOW;
      cp.Caption = referenceWindowCaption;
      referenceWindow.CreateHandle(cp);
      NativeMethods.SetWindowPos(referenceWindow.Handle, 
        GadgetWindow.HWND_BOTTOM, 0, 0, 0, 0, GadgetWindow.SWP_NOMOVE | 
        GadgetWindow.SWP_NOSIZE | GadgetWindow.SWP_NOACTIVATE | 
        GadgetWindow.SWP_NOSENDCHANGING);

      // start a repeated timer to detect "Show Desktop" events 
      timer = new System.Threading.Timer(OnTimer, null,
        System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
    }
Ejemplo n.º 2
0
        // will ensure that the toolTip window was created
        public void CreateToolTipHandle()
        {
            if (tipWindow == null || tipWindow.Handle == IntPtr.Zero)
            {
                NativeMethods.INITCOMMONCONTROLSEX icc = new NativeMethods.INITCOMMONCONTROLSEX();
                icc.dwICC  = NativeMethods.ICC_TAB_CLASSES;
                icc.dwSize = Marshal.SizeOf(icc);
                SafeNativeMethods.InitCommonControlsEx(icc);
                CreateParams cparams = new CreateParams();
                cparams.Parent    = dataGrid.Handle;
                cparams.ClassName = NativeMethods.TOOLTIPS_CLASS;
                cparams.Style     = NativeMethods.TTS_ALWAYSTIP;
                tipWindow         = new NativeWindow();
                tipWindow.CreateHandle(cparams);

                UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETMAXTIPWIDTH, 0, SystemInformation.MaxWindowTrackSize.Width);
                SafeNativeMethods.SetWindowPos(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.HWND_NOTOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE);
                UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETDELAYTIME, NativeMethods.TTDT_INITIAL, 0);
            }
        }
        // will ensure that the toolTip window was created
        public void CreateToolTipHandle()
        {
            if (tipWindow == null || tipWindow.Handle == IntPtr.Zero)
            {
                NativeMethods.INITCOMMONCONTROLSEX icc = new NativeMethods.INITCOMMONCONTROLSEX();
                icc.dwICC = NativeMethods.ICC_TAB_CLASSES;
                icc.dwSize = Marshal.SizeOf(icc);
                SafeNativeMethods.InitCommonControlsEx(icc);
                CreateParams cparams = new CreateParams();
                cparams.Parent = dataGrid.Handle;
                cparams.ClassName = NativeMethods.TOOLTIPS_CLASS;
                cparams.Style = NativeMethods.TTS_ALWAYSTIP;
                tipWindow = new NativeWindow();
                tipWindow.CreateHandle(cparams);

                UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETMAXTIPWIDTH, 0, SystemInformation.MaxWindowTrackSize.Width);
                SafeNativeMethods.SetWindowPos(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.HWND_NOTOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE);
                UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETDELAYTIME, NativeMethods.TTDT_INITIAL, 0);
            }
        }
Ejemplo n.º 4
0
        //==============================================================
        /// <summary>
        /// 通过向指定的按键发送KeyUp来恢复状态
        /// </summary>
        /// <param name="c"></param>
        /// <param name="allKeys"></param>
        public static void TryResetKeys(params IEnumerable<VirtualKeyCode>[] allKeys)
        {
            var dummyWnd = new NativeWindow();

            try
            {
                //如果被系统或者杀毒软件阻止, 则发送给自己的一个窗口,这样避免被在此拦截。
                dummyWnd.CreateHandle(new CreateParams(){ExStyle = (int) (User32.WS_EX.WS_EX_LAYERED | User32.WS_EX.WS_EX_TOOLWINDOW)});
                User32.ShowWindow(dummyWnd.Handle, User32.SW.SW_SHOWNORMAL);

                var sim = new InputSimulator();
                sim.Keyboard.Sleep(10);

                foreach (var keys in allKeys)
                {
                     foreach (var k in keys)
                    {
                        User32.SetForegroundWindow(dummyWnd.Handle);
                        sim.Keyboard.KeyUp(k);
                    }
                }

            }
            catch (Exception)
            {
                Debug.WriteLine("恢复键盘状态失败");
            #if TEST
                throw;
            #endif
            }
            finally
            {
                dummyWnd.DestroyHandle();
            }
        }
Ejemplo n.º 5
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            System.Windows.Forms.IntSecurity.SafeSubWindows.Demand();
            if (!SystemInformation.UserInteractive)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive"));
            }
            NativeWindow window = null;
            IntPtr       zero   = IntPtr.Zero;
            DialogResult cancel = DialogResult.Cancel;

            try
            {
                if (owner != null)
                {
                    zero = Control.GetSafeHandle(owner);
                }
                if (zero == IntPtr.Zero)
                {
                    zero = System.Windows.Forms.UnsafeNativeMethods.GetActiveWindow();
                }
                if (zero == IntPtr.Zero)
                {
                    window = new NativeWindow();
                    window.CreateHandle(new CreateParams());
                    zero = window.Handle;
                }
                if (helpMsg == 0)
                {
                    helpMsg = System.Windows.Forms.SafeNativeMethods.RegisterWindowMessage("commdlg_help");
                }
                System.Windows.Forms.NativeMethods.WndProc d = new System.Windows.Forms.NativeMethods.WndProc(this.OwnerWndProc);
                this.hookedWndProc = Marshal.GetFunctionPointerForDelegate(d);
                IntPtr userCookie = IntPtr.Zero;
                try
                {
                    this.defOwnerWndProc = System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, d);
                    if (Application.UseVisualStyles)
                    {
                        userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();
                    }
                    Application.BeginModalMessageLoop();
                    try
                    {
                        cancel = this.RunDialog(zero) ? DialogResult.OK : DialogResult.Cancel;
                    }
                    finally
                    {
                        Application.EndModalMessageLoop();
                    }
                    return(cancel);
                }
                finally
                {
                    IntPtr windowLong = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, zero), -4);
                    if ((IntPtr.Zero != this.defOwnerWndProc) || (windowLong != this.hookedWndProc))
                    {
                        System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, new HandleRef(this, this.defOwnerWndProc));
                    }
                    System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
                    this.defOwnerWndProc = IntPtr.Zero;
                    this.hookedWndProc   = IntPtr.Zero;
                    GC.KeepAlive(d);
                }
            }
            finally
            {
                if (window != null)
                {
                    window.DestroyHandle();
                }
            }
            return(cancel);
        }
Ejemplo n.º 6
0
        public DialogResult ShowDialog( IWin32Window owner ) {

            IntSecurity.SafeSubWindows.Demand();

            if (!SystemInformation.UserInteractive) {
                throw new InvalidOperationException(SR.GetString(SR.CantShowModalOnNonInteractive));
            }

            NativeWindow native = null;//This will be used if there is no owner or active window (declared here so it can be kept alive)

            IntPtr hwndOwner = IntPtr.Zero;
            DialogResult result = DialogResult.Cancel;
            try {
                if (owner != null) {
                    hwndOwner = Control.GetSafeHandle(owner);
                }
    
                if (hwndOwner == IntPtr.Zero) {
                    hwndOwner = UnsafeNativeMethods.GetActiveWindow();
                }
    
                if (hwndOwner == IntPtr.Zero) {
                    //We will have to create our own Window
                    native = new NativeWindow();
                    native.CreateHandle(new CreateParams());
                    hwndOwner = native.Handle;
                }
                
                if (helpMsg == 0) {
                    helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help");
                }
    
                NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(this.OwnerWndProc);
                hookedWndProc = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(ownerProc);
                System.Diagnostics.Debug.Assert(IntPtr.Zero == defOwnerWndProc, "The previous subclass wasn't properly cleaned up");

                IntPtr userCookie = IntPtr.Zero;
                try {
                    //UnsafeNativeMethods.[Get|Set]WindowLong is smart enough to call SetWindowLongPtr on 64-bit OS
                    defOwnerWndProc = UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc);

                    if (Application.UseVisualStyles) {
                        userCookie = UnsafeNativeMethods.ThemingScope.Activate();
                    }
                    
                    Application.BeginModalMessageLoop();
                    try {
                        result = RunDialog(hwndOwner) ? DialogResult.OK : DialogResult.Cancel;
                    }
                    finally {
                        Application.EndModalMessageLoop();
                    }
                }
                finally {
                    IntPtr currentSubClass = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC);
                    if ( IntPtr.Zero != defOwnerWndProc || currentSubClass != hookedWndProc) {
                        UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(this, defOwnerWndProc));
                    }
                    UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);

                    defOwnerWndProc = IntPtr.Zero;
                    hookedWndProc = IntPtr.Zero;
                    //Ensure that the subclass delegate will not be GC collected until after it has been subclassed
                    GC.KeepAlive(ownerProc);
                }
            }
            finally {
                if (null != native) {
                    native.DestroyHandle();
                }
            }

            return result;
        }
Ejemplo n.º 7
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            if (!SystemInformation.UserInteractive)
            {
                throw new InvalidOperationException(SR.CantShowModalOnNonInteractive);
            }

            NativeWindow native = null;//This will be used if there is no owner or active window (declared here so it can be kept alive)

            IntPtr       hwndOwner = IntPtr.Zero;
            DialogResult result    = DialogResult.Cancel;

            try {
                if (owner != null)
                {
                    hwndOwner = Control.GetSafeHandle(owner);
                }

                if (hwndOwner == IntPtr.Zero)
                {
                    hwndOwner = UnsafeNativeMethods.GetActiveWindow();
                }

                if (hwndOwner == IntPtr.Zero)
                {
                    //We will have to create our own Window
                    native = new NativeWindow();
                    native.CreateHandle(new CreateParams());
                    hwndOwner = native.Handle;
                }

                if (helpMsg == 0)
                {
                    helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help");
                }

                NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(this.OwnerWndProc);
                hookedWndProc = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(ownerProc);
                System.Diagnostics.Debug.Assert(IntPtr.Zero == defOwnerWndProc, "The previous subclass wasn't properly cleaned up");

                IntPtr userCookie = IntPtr.Zero;
                try {
                    //UnsafeNativeMethods.[Get|Set]WindowLong is smart enough to call SetWindowLongPtr on 64-bit OS
                    defOwnerWndProc = UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc);

                    if (Application.UseVisualStyles)
                    {
                        userCookie = UnsafeNativeMethods.ThemingScope.Activate();
                    }

                    Application.BeginModalMessageLoop();
                    try {
                        result = RunDialog(hwndOwner) ? DialogResult.OK : DialogResult.Cancel;
                    }
                    finally {
                        Application.EndModalMessageLoop();
                    }
                }
                finally {
                    IntPtr currentSubClass = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC);
                    if (IntPtr.Zero != defOwnerWndProc || currentSubClass != hookedWndProc)
                    {
                        UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(this, defOwnerWndProc));
                    }
                    UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);

                    defOwnerWndProc = IntPtr.Zero;
                    hookedWndProc   = IntPtr.Zero;
                    //Ensure that the subclass delegate will not be GC collected until after it has been subclassed
                    GC.KeepAlive(ownerProc);
                }
            }
            finally {
                if (null != native)
                {
                    native.DestroyHandle();
                }
            }

            return(result);
        }
Ejemplo n.º 8
0
            /// <include file='doc\ErrorProvider.uex' path='docs/doc[@for="ErrorProvider.ErrorWindow.EnsureCreated"]/*' />
            /// <devdoc>
            ///     Make sure the error window is created, and the tooltip window is created.
            /// </devdoc>
            bool EnsureCreated() {
                if (Handle == IntPtr.Zero) {
                    if (!parent.IsHandleCreated)
                    {
                        return false;
                    }
                    CreateParams cparams = new CreateParams();
                    cparams.Caption = String.Empty;
                    cparams.Style = NativeMethods.WS_VISIBLE | NativeMethods.WS_CHILD;
                    cparams.ClassStyle = NativeMethods.CS_DBLCLKS;
                    cparams.X = 0;
                    cparams.Y = 0;
                    cparams.Width = 0;
                    cparams.Height = 0;
                    cparams.Parent = parent.Handle;

                    CreateHandle(cparams);

                    NativeMethods.INITCOMMONCONTROLSEX icc = new NativeMethods.INITCOMMONCONTROLSEX();
                    icc.dwICC = NativeMethods.ICC_TAB_CLASSES;
                    icc.dwSize = Marshal.SizeOf(icc);
                    SafeNativeMethods.InitCommonControlsEx(icc);
                    cparams = new CreateParams();
                    cparams.Parent = Handle;
                    cparams.ClassName = NativeMethods.TOOLTIPS_CLASS;
                    cparams.Style = NativeMethods.TTS_ALWAYSTIP;
                    tipWindow = new NativeWindow();
                    tipWindow.CreateHandle(cparams);

                    UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETMAXTIPWIDTH, 0, SystemInformation.MaxWindowTrackSize.Width);
                    SafeNativeMethods.SetWindowPos(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE);
                    UnsafeNativeMethods.SendMessage(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.TTM_SETDELAYTIME, NativeMethods.TTDT_INITIAL, 0);
                }
                return true;
            }
 public DialogResult ShowDialog(IWin32Window owner)
 {
     System.Windows.Forms.IntSecurity.SafeSubWindows.Demand();
     if (!SystemInformation.UserInteractive)
     {
         throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive"));
     }
     NativeWindow window = null;
     IntPtr zero = IntPtr.Zero;
     DialogResult cancel = DialogResult.Cancel;
     try
     {
         if (owner != null)
         {
             zero = Control.GetSafeHandle(owner);
         }
         if (zero == IntPtr.Zero)
         {
             zero = System.Windows.Forms.UnsafeNativeMethods.GetActiveWindow();
         }
         if (zero == IntPtr.Zero)
         {
             window = new NativeWindow();
             window.CreateHandle(new CreateParams());
             zero = window.Handle;
         }
         if (helpMsg == 0)
         {
             helpMsg = System.Windows.Forms.SafeNativeMethods.RegisterWindowMessage("commdlg_help");
         }
         System.Windows.Forms.NativeMethods.WndProc d = new System.Windows.Forms.NativeMethods.WndProc(this.OwnerWndProc);
         this.hookedWndProc = Marshal.GetFunctionPointerForDelegate(d);
         IntPtr userCookie = IntPtr.Zero;
         try
         {
             this.defOwnerWndProc = System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, d);
             if (Application.UseVisualStyles)
             {
                 userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();
             }
             Application.BeginModalMessageLoop();
             try
             {
                 cancel = this.RunDialog(zero) ? DialogResult.OK : DialogResult.Cancel;
             }
             finally
             {
                 Application.EndModalMessageLoop();
             }
             return cancel;
         }
         finally
         {
             IntPtr windowLong = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, zero), -4);
             if ((IntPtr.Zero != this.defOwnerWndProc) || (windowLong != this.hookedWndProc))
             {
                 System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, new HandleRef(this, this.defOwnerWndProc));
             }
             System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
             this.defOwnerWndProc = IntPtr.Zero;
             this.hookedWndProc = IntPtr.Zero;
             GC.KeepAlive(d);
         }
     }
     finally
     {
         if (window != null)
         {
             window.DestroyHandle();
         }
     }
     return cancel;
 }
Ejemplo n.º 10
0
        public static void InitUI()
        {
            #if !MONO
            HotkeyWindow = new NativeWindow();
            HotkeyWindow.CreateHandle(new CreateParams {
                Caption = "NDexer Hotkey Window",
                X = 0,
                Y = 0,
                Width = 0,
                Height = 0,
                Style = 0,
                ExStyle = 0x08000000,
                Parent = new IntPtr(-3)
            });

            Icon_Monitoring = Icon.FromHandle(Properties.Resources.database_monitoring.GetHicon());
            Icon_Working_1 = Icon.FromHandle(Properties.Resources.database_working_1.GetHicon());
            Icon_Working_2 = Icon.FromHandle(Properties.Resources.database_working_2.GetHicon());

            ContextMenu = new ContextMenuStrip();
            ContextMenu.Items.Add(
                "&Search", null,
                (e, s) => {
                    Scheduler.Start(ShowFullTextSearchTask(), TaskExecutionPolicy.RunAsBackgroundTask);
                }
            );
            ContextMenu.Items.Add("-");
            ContextMenu.Items.Add(
                "&Configure", null,
                (e, s) => {
                    using (var dialog = new ConfigurationDialog(Database))
                        if ((dialog.ShowDialog() == DialogResult.OK) && (dialog.NeedRestart))
                            Scheduler.Start(RestartTask(), TaskExecutionPolicy.RunAsBackgroundTask);
                }
            );
            ContextMenu.Items.Add(
                "&Rebuild Index", null,
                (e, s) => {
                    Scheduler.Start(ConfirmRebuildIndexTask(), TaskExecutionPolicy.RunAsBackgroundTask);
                }
            );
            ContextMenu.Items.Add("-");
            ContextMenu.Items.Add(
                "E&xit", null,
                (e, s) => {
                    Scheduler.Start(ExitTask(), TaskExecutionPolicy.RunAsBackgroundTask);
                }
            );

            NotifyIcon = new NotifyIcon();
            NotifyIcon.ContextMenuStrip = ContextMenu;
            NotifyIcon.DoubleClick += (EventHandler)((s, e) => {
                Scheduler.Start(ShowFullTextSearchTask(), TaskExecutionPolicy.RunAsBackgroundTask);
            });

            Scheduler.Start(
                RefreshTrayIcon(),
                TaskExecutionPolicy.RunAsBackgroundTask
            );
            #endif
        }