private void txtHwnd_Leave(object sender, EventArgs e) { if (m_TrackingIsRunning) { return; } var hwnd = (IntPtr)int.Parse(txtHwnd.Text); if (User32Windows.IsWindow(hwnd)) { Hwnd = hwnd; btnStartStop.Enabled = true; btnStartStop.Text = "Start"; chkTrackModalWindow.Enabled = true; m_TrackingIsValid = true; } else { Hwnd = IntPtr.Zero; btnStartStop.Enabled = false; } DisplayTitle(); }
private void GetWindowInfo() { m_HostedWindowHwnd = (IntPtr)int.Parse(txtHwnd.Text); StringBuilder title = new StringBuilder(255); if (User32Windows.IsWindow(m_HostedWindowHwnd)) { User32Windows.GetWindowText(m_HostedWindowHwnd, title, title.Capacity + 1); } txtTitle.Text = title.ToString(); }
private void StartTracking() { if (Hwnd == IntPtr.Zero || !User32Windows.IsWindow(Hwnd)) { MessageBox.Show("No window is selected.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } m_StartTime = DateTime.Now; m_WindowIsHung = IsHungAppWindow(m_Hwnd) || m_TrackModalWindow; var msg = "• Start tracking" + " [" + Hwnd.ToString() + "] \"" + User32Windows.GetWindowText(Hwnd, 255) + "\"" + "\r\n" + FormatTime(m_StartTime) + " the program is " + (m_WindowIsHung ? "hung" : "accessible") + "."; Log(msg); WindowColors(); chkTrackModalWindow.Enabled = false; timer1.Start(); }
private void CheckToolDisplaying() { if (!m_IsRunning) { return; } IntPtr foreWindow = User32Windows.GetForegroundWindow(); var title = User32Windows.GetWindowText(foreWindow, 255); bool matchesTitlePattern = false; if (((foreWindow == m_HostWindowHwnd || foreWindow == this.Handle) && !User32Windows.IsIconic(m_HostWindowHwnd)) || (title == m_HostWindowTitle && m_RunOnAllWindowsWithSameTitle) || (m_TitlePattern != String.Empty && (matchesTitlePattern = m_TitleRegex.IsMatch(title)))) { // if ((foreWindow != m_HostWindowHwnd) && (foreWindow != this.Handle)) // { // int pidFore, pidThis; // User32Windows.GetWindowThreadProcessId(foreWindow, out pidFore); // User32Windows.GetWindowThreadProcessId(this.Handle, out pidThis); // if (pidFore == pidThis) // { // return; // } // // m_HostWindowHwnd = foreWindow; // m_HostWindowTitle = title; // if (!matchesTitlePattern) // { // CalculateCoordinates(); // } // } Rectangle rHost; User32Windows.GetWindowRect(m_HostWindowHwnd, out rHost); int newX = 0, newY = 0; if (m_AnchorH == AnchorHorizontal.Left) { newX = rHost.Left + m_HostWindowOffsetX; } else { newX = rHost.Width + m_HostWindowOffsetX; } if (m_AnchorV == AnchorVertical.Top) { newY = rHost.Top + m_HostWindowOffsetY; } else { newY = rHost.Height + m_HostWindowOffsetY; } if ((this.Location.X != newX) || (this.Location.Y != newY)) { this.Location = new Point(newX, newY); } if (!this.Visible) { Task.Delay(timer1.Interval).ContinueWith(t => this.Show()); } } else if ((!m_RunOnAllWindowsWithSameTitle) && (!User32Windows.IsWindow(m_HostWindowHwnd))) { if (this.Exit != null) { this.Exit.Invoke(this, new ToolEventArgs() { Title = m_HostWindowTitle }); } this.Close(); } else if (User32Windows.IsIconic(m_HostWindowHwnd)) { this.Hide(); } else { Rectangle rForeWindow; User32Windows.GetWindowRect(foreWindow, out rForeWindow); var r = this.RectangleToScreen(this.DisplayRectangle); var contains = RectangleContains(rForeWindow, r) || RectangleIntersects(rForeWindow, r); int processId, ptocessIdThis; User32Windows.GetWindowThreadProcessId(foreWindow, out processId); User32Windows.GetWindowThreadProcessId(this.Handle, out ptocessIdThis); if (this.Visible && m_AutoHide && contains && (processId != ptocessIdThis)) { this.Hide(); } } }
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); }
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e) { var settingsForm = new SendCommandToolPropertiesForm() { HostHwnd = this.m_HostWindowHwnd, ToolWidht = this.Size.Width, ToolHeight = this.Size.Height, AnchorH = m_AnchorH, AnchorV = m_AnchorV, Commands = m_Commands, CommandType = m_SendCommandType, Sleep = m_Sleep, SleepTimeout = m_SleepTimeout, RunOnAllWindowsWithSameTitle = m_RunOnAllWindowsWithSameTitle, ToolLeft = this.Location.X, ToolTop = this.Location.Y, BorderColor = this.m_PenNormal.Color, BorderHoverColor = this.m_PenHover.Color, TitlePattern = this.m_TitlePattern, ActivateOnHover = this.ActivateOnHover }; var result = settingsForm.ShowDialog(); if (result == DialogResult.OK) { this.m_HostWindowHwnd = settingsForm.HostHwnd; this.Size = new Size(settingsForm.ToolWidht, settingsForm.ToolHeight); this.m_AnchorH = settingsForm.AnchorH; this.m_AnchorV = settingsForm.AnchorV; this.m_DrawRectangle = new Rectangle(0, 0, this.Size.Width - 1, this.Size.Height - 1); this.m_Commands = settingsForm.Commands; this.m_SendCommandType = settingsForm.CommandType; if (this.m_SendCommandType == SendCommandType.ActivateWindow) { int handle; if (int.TryParse(m_Commands, out handle)) { if (User32Windows.IsWindow((IntPtr)handle)) { if (this.BackgroundImagePath != string.Empty) { var q = MessageBox.Show("The tool already has an background icon,\novervrite?", "Background icon", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (q == DialogResult.No) { return; } } var icon = User32Windows.GetIcon((IntPtr)handle); if (icon != null) { this.BackgroundImage = icon.ToBitmap(); } } } } this.m_Sleep = settingsForm.Sleep; this.m_SleepTimeout = settingsForm.SleepTimeout; this.m_RunOnAllWindowsWithSameTitle = settingsForm.RunOnAllWindowsWithSameTitle; this.m_TitlePattern = settingsForm.TitlePattern; this.Location = new Point(settingsForm.ToolLeft, settingsForm.ToolTop); this.m_PenNormal.Color = settingsForm.BorderColor; this.m_PenHover.Color = settingsForm.BorderHoverColor; if (m_TitlePattern != String.Empty) { m_TitleRegex = new Regex(m_TitlePattern); } else { m_TitleRegex = null; } this.ActivateOnHover = settingsForm.ActivateOnHover; } }
private void ProcessTracking() { if (!User32Windows.IsWindow(Hwnd)) { timer1.Stop(); m_TrackingIsRunning = false; btnStartStop.Text = "Start"; } string message = String.Empty; var now = DateTime.Now; var wasRunnind = now - m_StartTime; var running = FormatTime(wasRunnind); var runningClock = FormatTime(wasRunnind, false); if (!m_TrackingIsRunning) { message = FormatTime(now) + " the " + (m_TrackModalWindow ? "modal window" : "program") + " is closed now and was in the previous state: " + running + "."; m_WindowIsHung = false; this.m_TrackingIsValid = false; this.btnStartStop.Text = "Close"; lblLabelPlusClock.Text = "Hwnd:"; } else { lblLabelPlusClock.Text = runningClock; if (m_TrackModalWindow) { return; } else if (IsHungAppWindow(Hwnd) && !m_WindowIsHung) { m_WindowIsHung = true; // The Windows OS detects hung window after 5 seconds. var fiveSeconds = new TimeSpan(0, 0, 5); m_StartTime = now - fiveSeconds; wasRunnind -= fiveSeconds; message = FormatTime(now) + " the program is hung now and was in the previous state: " + running + "."; } else if (!IsHungAppWindow(Hwnd) && m_WindowIsHung) { m_WindowIsHung = false; m_StartTime = now; message = FormatTime(now) + " the program is accessible now and was in the previous state: " + FormatTime(wasRunnind) + "."; } } if (message != String.Empty) { Log(message); WindowColors(); if (!m_TrackingIsRunning) { Log("Please close this window."); } } }