Example #1
0
        public ApplicationsControl()
        {
            InitializeComponent();

            this.DataContext  = this;
            this.Applications = User32Helper.GetDesktopWindows();
        }
Example #2
0
 /// <summary>
 /// Pretplata na događaj koji je pozvan nakon što se kontrola učita
 /// Svaki put kad promijenimo tab, ažuriramo jednostavno listu.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
 {
     this.Applications.Clear(); // Očisti trenutnu listu i dodaj novu.
     foreach (var application in User32Helper.GetDesktopWindows())
     {
         this.Applications.Add(application);
     }
 }
Example #3
0
 /// <summary>
 /// Callback function called by the operating system when a keyboard message is processed.
 /// This function adds data relative to the keyboard message to the <see cref="m_HookQueue"/> queue.
 /// </summary>
 /// <param name="nCode">A code the hook procedure uses to determine how to process the message.
 /// If nCode is less than zero, the hook procedure must pass the message to the <see cref="CallNextHookEx(IntPtr, int, int, IntPtr)"/>
 /// function without further processing and should return the value returned by <see cref="CallNextHookEx(IntPtr, int, int, IntPtr)"/>.</param>
 /// <param name="wParam">The identifier of the keyboard message.</param>
 /// <param name="lParam">A pointer to a <see cref="KBDLLHOOKSTRUCT"/> structure.</param>
 /// <returns>If returns <see cref="IntPtr.Zero"/> other message hooks in the queue are not processed,
 /// otherwise processing continues normally.</returns>
 private IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode >= 0)
     {
         KeyData data = new KeyData((User32Helper.WM)wParam.ToInt32(), lParam);
         m_HookQueue.Add(data);
     }
     return(User32Helper.CallNextHookEx(m_HookID, nCode, wParam, lParam));
 }
Example #4
0
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            foreach (char c in txtOutput.Text.ToLower())
            {
                User32Helper.PostMessage(wbAxHandle, User32Helper.WM_KEYDOWN, User32Helper.VkKeyScan(c), 0);
                User32Helper.PostMessage(wbAxHandle, User32Helper.WM_KEYUP, User32Helper.VkKeyScan(c), 0);
            }

            User32Helper.PostMessage(wbAxHandle, User32Helper.WM_KEYDOWN, User32Helper.VK_RETURN, 0);
            User32Helper.PostMessage(wbAxHandle, User32Helper.WM_KEYUP, User32Helper.VK_RETURN, 0);
        }
        private ImageSource CapturePixel()
        {
            Point point = System.Windows.Forms.Cursor.Position;

            Console.WriteLine("X: " + point.X + "\t Y: " + point.Y);
            point.X -= _pixelWidth / 2;
            point.Y -= _pixelHeight / 2;

            var image = ScreenCaptureHelper.CaptureWindow(User32Helper.GetDesktopWindow(), point.X, point.Y, _pixelWidth, _pixelHeight);

            return(ScreenCaptureHelper.ConvertImageToImageSource(image));
        }
Example #6
0
        private void MoveWindow()
        {
            var cursorPosition = Cursor.Position;
            var allScreens     = Screen.AllScreens;

            var targets = _screenConfigurationLocator.GetTargets(allScreens);

            var target = targets.FirstOrDefault(t => t.Rectangle.IsPointInside(cursorPosition));

            if (target == null)
            {
                return;
            }

            var browser = _configuration.Browsers[_configuration.TargetBrowser];
            var handle  = User32Helper.FindWindow(browser.WindowClassName, browser.WindowTitle);

            if (handle == IntPtr.Zero)
            {
                _logger.LogWarning("Failed to find window handle");
                return;
            }

            if (browser.HideWindow)
            {
                User32Helper.HideWindow(handle);
            }

            var popupPosition = User32Helper.GetWindowPosition(handle);

            if (_configuration.AllowOverlap)
            {
                var userHasMouseOverPopup = popupPosition.IsPointInside(cursorPosition);
                if (userHasMouseOverPopup)
                {
                    return;
                }
            }

            var targetDimensions = new Size(target.Size.Width, target.Size.Height);

            var size = GeometryHelper.IsApproximatelyTheSame(targetDimensions, popupPosition)
                ? popupPosition.Size
                : targetDimensions;

            var newPosition = GetTargetPosition(size, target.Position, target.Screen, target.Size.Margin);

            if (GeometryHelper.IsDifferent(newPosition, popupPosition))
            {
                User32Helper.MoveWindow(handle, newPosition);
            }
        }
Example #7
0
 /// <summary>
 /// Create a new instance of the <see cref="KeyboardHook"/> and initialize ith internal structures.
 /// </summary>
 public KeyboardHook()
 {
     Filters        = new List <IKeyboardHookFilter>();
     m_HookQueue    = new BlockingCollection <KeyData>();
     m_WorkerThread = new Thread(Worker);
     m_WorkerThread.Start();
     using (Process curProc = Process.GetCurrentProcess())
     {
         using (ProcessModule curModule = curProc.MainModule)
         {
             m_LowLevelKeyboardCallback = new User32Helper.LowLevelKeyboardProc(HookCallBack);
             m_HookID = User32Helper.SetWindowsHookEx(User32Helper.WH.KEYBOARD_LL, m_LowLevelKeyboardCallback, Kernel32Helper.GetModuleHandle(curModule.ModuleName), 0);
         }
     }
 }
 private void RegisterGlobalHotkey()
 {
     // TODO: Make the global hotkeys configurable
     if (User32Helper.RegisterHotKey(IntPtr.Zero, MYACTION_HOTKEY_ID, MOD_CONTROL | MOD_SHIFT, Keys.PrintScreen) == 0)
     {
         // Is the error that the hotkey is registered?
         if (Marshal.GetLastWin32Error() == User32Helper.ERROR_HOTKEY_ALREADY_REGISTERED)
         {
             MessageBox.Show("Cannot register hotkey.");
         }
         else
         {
             throw new Win32Exception();
         }
     }
 }
        public static IAppInfo CreateForProcess(int processId, bool trackProcess = false)
        {
            if (processId == 0)
            {
                return(new Internal.SystemSoundsAppInfo());
            }

            if (User32Helper.IsImmersiveProcess(processId))
            {
                return(new Internal.ModernAppInfo(processId, trackProcess));
            }
            else
            {
                return(new Internal.DesktopAppInfo(processId, trackProcess));
            }
        }
        public static IntPtr AxHandle(this WebBrowser browser)
        {
            var childWindows = new List <string>
            {
                "Shell DocObject View",
                "Internet Explorer_Server",
                "MacromediaFlashPlayerActiveX"
            };

            var handle = User32Helper.FindWindowEx(browser.Handle, IntPtr.Zero, "Shell Embedding", null);

            childWindows.ForEach(
                (window) =>
            {
                if (handle != null)
                {
                    handle = User32Helper.FindWindowEx(handle, IntPtr.Zero, window, null);
                }
            });

            return(handle);
        }
Example #11
0
        public IntPtr GetFlashHandle(WebBrowser browser)
        {
            var windows = new string[]
            {
                "Shell DocObject View",
                "Internet Explorer_Server",
                "MacromediaFlashPlayerActiveX"
            };

            IntPtr hwnd = User32Helper.FindWindowEx(browser.Handle, IntPtr.Zero, "Shell Embedding", null);

            foreach (string window in windows)
            {
                if (hwnd != null)
                {
                    hwnd = User32Helper.FindWindowEx(hwnd, IntPtr.Zero, window, null);
                }
            }
            ;

            return(hwnd);
        }
Example #12
0
        private void cmdCloseAll_Click(object sender, EventArgs e)
        {
            Process      lprocess;
            int          ltabs;
            DialogResult lDR;
            uint         exitCode;

            Shell32.Shell shell;
            String        lstrProcessServiceName;
            bool          lblnFoundServiceName;

            ServiceController[] scServices;
            var     ldesktopPrograms = new List <DesktopWindow>();
            uint    pid;
            uint    temp = 0;
            Process tempProcess;

            lDR = MessageBox.Show("You Are About To Close All Active Programs.\n Continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (lDR == System.Windows.Forms.DialogResult.No)
            {
                return; //get out, we don't want to continue
            }

            //shell = new Shell32.Shell();
            //scServices = ServiceController.GetServices();

            lprocess = Process.GetCurrentProcess();

            ldesktopPrograms = User32Helper.GetDesktopWindows();

            //go thru each window, if it's true and has a title then try to kill it
            for (int i = 0; i < ldesktopPrograms.Count; i++)
            {
                if (ldesktopPrograms[i].IsVisible && ldesktopPrograms[i].Title.Trim() != "")
                {
                    pid = GetWindowThreadProcessId(ldesktopPrograms[i].Handle, out temp);
                    try
                    {
                        tempProcess = Process.GetProcessById((int)temp);
                        if (tempProcess.Id != lprocess.Id && tempProcess.MainWindowHandle != IntPtr.Zero && tempProcess.ProcessName != "explorer")
                        {
                            try
                            {
                                tempProcess.CloseMainWindow();
                                System.Threading.Thread.Sleep(Globals.mintTimeout);
                            }

                            catch (InvalidOperationException err) { tempProcess.Kill(); }

                            catch (Win32Exception err) { tempProcess.Kill(); }

                            catch (Exception err)
                            {
                                tempProcess.Kill(); //no error reporting just kill it
                            }

                            //if we haven't thrown an error and the program still hasn't closed, kill it
                            if (!tempProcess.HasExited)
                            {
                                tempProcess.Kill();
                                System.Threading.Thread.Sleep(Globals.mintTimeout);
                            }
                        }
                    }
                    catch (System.ArgumentException err)
                    {
                        //MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //just skip this ID
                    }
                }
            }

            System.Threading.Thread.Sleep(Globals.mintTimeout);
            ExitWindowsEx(0, 0);
            this.Dispose();
            return;

            /*
             *
             * Dead Code Below
             * First attempt at gracefully exiting programs
             */

            foreach (Process p in Process.GetProcesses(System.Environment.MachineName))
            {
                if ((p.Id != lprocess.Id && p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer"))//make sure we aren't closing this program and that the current process has a GUI window
                {
                    try
                    {
                        p.CloseMainWindow();
                        System.Threading.Thread.Sleep(Globals.mintTimeout);
                    }

                    catch (InvalidOperationException err) { p.Kill(); }

                    catch (Win32Exception err) { p.Kill(); }

                    catch (Exception err)
                    {
                        //MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        p.Kill(); //screw error reporting just kill it
                    }

                    //}
                    if (!p.HasExited)
                    {
                        p.WaitForExit(Globals.mintTimeout * 2);

                        try
                        {
                            GetExitCodeProcess(p.Handle, out exitCode);
                            {
                                if (exitCode != 0)
                                {
                                    p.Kill();
                                }
                            }
                        }
                        catch (Win32Exception err) { p.Kill(); }

                        catch (InvalidOperationException err) { p.Kill(); }

                        catch (Exception err) { p.Kill(); }

                        System.Threading.Thread.Sleep(Globals.mintTimeout);
                        ltabs = 0;

                        /*while (!p.HasExited) //means that most likely we have a popup window asking if we are sure we want to exit
                         * {                    //try enter if control is on the yes button
                         *  //else tab a few times in case the control doesn't default to the yes button
                         *  TryTabbing(ltabs);
                         *  SendKeys.Send("{ENTER}");
                         *  System.Threading.Thread.Sleep(Globals.mintTimeout);
                         *
                         *  ltabs++;
                         *
                         *  if (ltabs > 3) //we've tried closing the program nicely a few times, just kill it at this point ¯\_(ツ)_/¯
                         *  {
                         *      try { p.Kill(); }
                         *      catch (Exception err) { MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                         *      System.Threading.Thread.Sleep(Globals.mintTimeout);
                         *      break;
                         *  }
                         *  try { p.CloseMainWindow(); }
                         *  catch (Exception err) { p.Kill(); }// MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                         *  System.Threading.Thread.Sleep(Globals.mintTimeout);
                         * }*/
                    }
                }
            }
            System.Threading.Thread.Sleep(Globals.mintTimeout);
            this.Dispose();
        }
Example #13
0
 /// <summary>
 /// Stops the worker thread and remove the keyboard messages hook.
 /// </summary>
 public void Dispose()
 {
     m_HookQueue.Add(null);
     User32Helper.UnhookWindowsHookEx(m_HookID);
 }
 public SystemSoundsAppInfo()
 {
     SmallLogoPath = Environment.ExpandEnvironmentVariables(User32Helper.Is64BitOperatingSystem() ?
                                                            @"%windir%\sysnative\audiosrv.dll,203" : @"%windir%\system32\audiosrv.dll,203");
 }
Example #15
0
 /// <summary>
 /// Create a new instance of <see cref="KeyData"/> and initialize its internal structures.
 /// </summary>
 /// <param name="wParam">wParam from <see cref="User32Helper.LowLevelKeyboardProc"/> delegate.</param>
 /// <param name="lParam">lParam from <see cref="User32Helper.LowLevelKeyboardProc"/> delegate.</param>
 public KeyData(User32Helper.WM wParam, IntPtr lParam)
 {
     m_wParam    = wParam;
     m_lParam    = (User32Helper.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(User32Helper.KBDLLHOOKSTRUCT));
     m_keyStatus = User32Helper.GetKeyState(m_lParam.vkCode);
 }