Пример #1
0
        private void SetFirstMenuItem(ToolStripItemCollection passMenu)
        {
            if (m_Passwords.Count == 0)
            {
                return;
            }

            var lastWindow    = User32Windows.GetLastActiveWindow(hwndExcept: this.Handle, exceptNames: m_ExceptPasswordWindows);
            var maxMenuLength = 40;
            var title         = lastWindow.Title.Length >= maxMenuLength
                    ? lastWindow.Title.Substring(0, maxMenuLength - 3) + "..."
                    : lastWindow.Title;

            if (passMenu.Count == 0)
            {
                passMenu.Add(title);
            }
            else
            {
                ((ToolStripMenuItem)passMenu[0]).Text = title;
            }

            if (lastWindow.Icon != null)
            {
                ((ToolStripMenuItem)passMenu[0]).Image = lastWindow.Icon.ToBitmap();
            }
            ((ToolStripMenuItem)passMenu[0]).ForeColor = Color.FromArgb(112, 112, 112);
        }
Пример #2
0
        private void ActivatePreviousWindow(IntPtr hwnd = default(IntPtr))
        {
            if (hwnd == IntPtr.Zero)
            {
                hwnd = User32Windows.GetLastActiveWindow(hwndExcept: this.Handle, exceptNames: m_ExceptPasswordWindows).Handle;
            }

            User32Windows.SetForegroundWindow(hwnd);
        }
Пример #3
0
        private void ProcessClick()
        {
            if (!m_IsRunning)
            {
                return;
            }

            if (!User32Windows.IsWindow(m_HostWindowHwnd))
            {
                MessageBox.Show("Hosting window not found. Close the tool or set its hosting window.",
                                this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (this.m_SendCommandType == SendCommandType.ActivateWindow)
            {
                int handleTmp;
                if (int.TryParse(m_Commands, out handleTmp))
                {
                    var handle = (IntPtr)handleTmp;
                    if (User32Windows.IsIconic(handle))
                    {
                        User32Windows.ShowWindow(handle, User32Windows.SW_RESTORE);
                    }
                    User32Windows.SetForegroundWindow(handle);
                }
                else
                {
                    MessageBox.Show(String.Format("Window {0} not found.", m_Commands),
                                    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return;
            }
            else if (this.m_SendCommandType == SendCommandType.ActivateLastN)
            {
                int lastN;
                if (int.TryParse(m_Commands, out lastN))
                {
                    var handle = IntPtr.Zero;

                    var windows = User32Windows.GetDesktopWindows(getIcons: false);

                    int currentProcId;
                    User32Windows.GetWindowThreadProcessId(this.Handle, out currentProcId);

                    if (lastN < windows.Count)
                    {
                        var i = 0;
                        for ( ; i < lastN; i++)
                        {
                            int procId;
                            User32Windows.GetWindowThreadProcessId(windows[i].Handle, out procId);
                            if (procId == currentProcId)
                            {
                                lastN++;
                                if (lastN == windows.Count)
                                {
                                    return;
                                }
                            }
                        }
                        handle = windows[i - 1].Handle;
                    }
                    else
                    {
                        return;
                    }

                    if (User32Windows.IsIconic(handle))
                    {
                        User32Windows.ShowWindow(handle, User32Windows.SW_RESTORE);
                    }
                    User32Windows.SetForegroundWindow(handle);
                }
                else
                {
                    MessageBox.Show(String.Format("Window {0} not found.", m_Commands),
                                    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return;
            }

            User32Windows.SetForegroundWindow(m_HostWindowHwnd);

            string backup = null;

            if (this.m_SendCommandType == SendCommandType.Clipboard)
            {
                backup     = m_Commands;
                m_Commands = m_Commands == String.Empty ?
                             Clipboard.GetText() :
                             m_Commands.Replace("{clipboard}", Clipboard.GetText());
            }

            if (m_Commands == String.Empty)
            {
                return;
            }

            if (this.m_Sleep)
            {
                Thread.Sleep(m_SleepTimeout);
            }

            //
            // Working code (using SendKeys class).
            //
            var commands = m_Commands.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            for (int i = 0; i < commands.Length; i++)
            {
                if (this.m_Sleep)
                {
                    Thread.Sleep(m_SleepTimeout);
                }

                SendKeys.SendWait(commands[i]);
            }

            if (this.m_SendCommandType == SendCommandType.Clipboard)
            {
                m_Commands = backup;
            }

            var lastWindow = User32Windows.GetLastActiveWindow(hwndExcept: this.Handle);

            User32Windows.SetForegroundWindow(lastWindow.Handle);

            //
            // Working code (using InputSimulator http://inputsimulator.codeplex.com/).
            //
            // var simulator = new InputSimulator();
            // simulator.Keyboard.TextEntry(m_Commands);
        }