Esempio n. 1
0
        private static bool TryToActivateAnotherInstance()
        {
            try
            {
                var currentProcess = Process.GetCurrentProcess();
                var prc            = Process.GetProcessesByName(currentProcess.ProcessName);
                if (prc.Length > 0)
                {
                    foreach (var process in prc)
                    {
                        if (process.Id != currentProcess.Id && process.MainWindowHandle != IntPtr.Zero)
                        {
                            WinApiDllImports.ShowWindow(process.MainWindowHandle, 1);
                            WinApiDllImports.SetForegroundWindow(process.MainWindowHandle);
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error while trying to activate another isntance: {0}.", ex.Message);
                return(false);
            }
        }
Esempio n. 2
0
 public void UnregisterHotKeys()
 {
     // unregister all the registered hot keys.
     for (int i = _currentId; i > 0; i--)
     {
         WinApiDllImports.UnregisterHotKey(_window.Handle, i);
     }
 }
Esempio n. 3
0
        private static void LaunchApplication()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                WinApiDllImports.SetProcessDPIAware();
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
Esempio n. 4
0
        /// <summary>
        /// Registers a hot key in the system.
        /// </summary>
        /// <param name="modifier">The modifiers that are associated with the hot key.</param>
        /// <param name="key">The key itself that is associated with the hot key.</param>
        public void RegisterHotKey(ModifierKeys modifier, Keys key)
        {
            // increment the counter.
            _currentId = _currentId + 1;

            // register the hot key.
            if (!WinApiDllImports.RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
            {
                throw new InvalidOperationException("Couldn’t register the hot key.");
            }
        }
Esempio n. 5
0
 private string GetCaptionOfWindow(IntPtr hwnd)
 {
     try
     {
         int max_length = WinApiDllImports.GetWindowTextLength(hwnd);
         var windowText = new StringBuilder("", max_length + 5);
         WinApiDllImports.GetWindowText(hwnd, windowText, max_length + 2);
         return(windowText.ToString());
     }
     catch (Exception ex)
     {
         LogManager.GetCurrentClassLogger().Error(ex, "Error getting window caption: {0}.", ex.Message);
         throw ex;
     }
 }
Esempio n. 6
0
        // https://code.msdn.microsoft.com/windowsapps/C-Getting-the-Windows-da1bd524

        public GuiWindowInfo GetWindowUnderMouse()
        {
            IntPtr windowHandle = this.GetTopmostWindowHandleUnderTheMouse();

            if (windowHandle == IntPtr.Zero)
            {
                return(null);
            }

            uint processId = 0;

            WinApiDllImports.GetWindowThreadProcessId(windowHandle, out processId);
            Process process     = Process.GetProcessById((int)processId);
            string  windowTitle = this.GetCaptionOfWindow(windowHandle);

            return(new GuiWindowInfo(windowHandle, windowTitle, process));
        }
Esempio n. 7
0
        private IntPtr GetTopmostWindowHandleUnderTheMouse()
        {
            WinApiDllImports.POINT p = new WinApiDllImports.POINT();
            bool retVal = WinApiDllImports.GetCursorPos(ref p);

            if (retVal)
            {
                IntPtr hwnd = WinApiDllImports.WindowFromPoint(p);
                if (hwnd.ToInt64() > 0)
                {
                    var result = hwnd;
                    while (WinApiDllImports.GetParent(hwnd).ToInt64() > 0)
                    {
                        hwnd = WinApiDllImports.GetParent(hwnd);
                    }

                    Debug.Assert(hwnd != IntPtr.Zero && hwnd.ToInt64() > 0, "Invalid window handle");
                    return(hwnd);
                }
            }
            return(IntPtr.Zero);
        }
Esempio n. 8
0
 public static void SetTitleByMainWindowHandle(IntPtr mainWindowHandle, string newTitle)
 {
     WinApiDllImports.SetWindowText(mainWindowHandle, newTitle);
 }