private static WindowInteractionState UiaGetWindowState(ProdWindow baseControl) { WindowInteractionState state = WindowPatternHelper.GetInteractionState(baseControl.UIAElement); LogController.ReceiveLogMessage(new LogMessage(state.ToString())); return(state); }
static void Main() { Application.ThreadExit += new EventHandler(ThreadOnExit); string ApplicationMutex = "BcFFcd23-3456-6543-Fc44abcd1234"; string ApplicationNameText = "[MyApplicationName]"; mutex = new Mutex(true, ApplicationMutex); bool SingleInstance = mutex.WaitOne(0, false); if (!SingleInstance) { MessageBox.Show("Application already running"); string AppProductName = Process.GetCurrentProcess().MainModule.FileVersionInfo.ProductName; Process[] WindowedProcesses = Process.GetProcesses() .Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray(); foreach (Process process in WindowedProcesses.Where(p => p.MainModule.FileVersionInfo.ProductName == AppProductName)) { if (process.Id != Process.GetCurrentProcess().Id) { AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); if (!element.Current.IsOffscreen) { WindowPattern WPattern = element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern; WindowInteractionState state = WPattern.Current.WindowInteractionState; WPattern.SetWindowVisualState(WindowVisualState.Normal); break; } } } } if (SingleInstance) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, (UIElm, evt) => { AutomationElement element = UIElm as AutomationElement; if (element != null) { Console.WriteLine(element.Current.Name); Form form = Application.OpenForms.Cast <Form>() .Where(f => f.Text == ApplicationNameText) .FirstOrDefault(); if (form != null) { form.Visible = true; form.WindowState = FormWindowState.Normal; form.Show(); } } }); Application.Run(new MyAppMainForm()); } else { Application.ExitThread(); } }
public static void AssertReadyForUserInteraction(AutomationElement element) { WindowPattern currentPattern = AutomationPatternHelper.GetWindowPattern(element); WindowInteractionState windowInteractionState = currentPattern.Current.WindowInteractionState; if (windowInteractionState != WindowInteractionState.ReadyForUserInteraction) { throw new Exception(string.Format("Window is not ready for user interaction. State is {0}. ({1})", windowInteractionState.ToString(), element.ToString())); } }
public void CloseTest() { using (Form f = new Form()) { IRawElementProviderFragment provider = (IRawElementProviderFragment)ProviderFactory.GetProvider(f); IWindowProvider pattern = (IWindowProvider)provider.GetPatternProvider(WindowPatternIdentifiers.Pattern.Id); f.Show(); Thread.Sleep(500); Assert.AreEqual(WindowInteractionState.ReadyForUserInteraction, pattern.InteractionState, "Interaction state while running normally"); var dialog = new Form(); WindowInteractionState interactionState = (WindowInteractionState)(-1); dialog.Activated += delegate { interactionState = pattern.InteractionState; Thread.Sleep(1000); dialog.Close(); }; dialog.ShowDialog(); Assert.AreEqual(WindowInteractionState.BlockedByModalWindow, interactionState, "Interaction state while blocked by modal dialog"); bool formClosed = false; bool formClosingChecked = false; f.Closed += delegate(Object sender, EventArgs e) { formClosed = true; }; f.Closing += delegate(Object sender, CancelEventArgs e) { Assert.AreEqual(WindowInteractionState.Closing, pattern.InteractionState, "Interaction state while closing"); formClosingChecked = true; }; bridge.ResetEventLists(); pattern.Close(); Assert.IsTrue(formClosed, "Form closed event didn't fire."); Assert.IsTrue(formClosingChecked, "Interaction state while closing never confirmed."); Assert.AreEqual(1, bridge.StructureChangedEvents.Count, "event count"); Assert.AreSame(provider, bridge.StructureChangedEvents [0].provider, "event provider"); Assert.AreEqual(StructureChangeType.ChildRemoved, bridge.StructureChangedEvents [0].e.StructureChangeType, "event change type"); Application.DoEvents(); } }
//------------------------------------------------------ // // Private Methods // //------------------------------------------------------ #region Private Methods private void OnStateChange(IntPtr hwnd, int idObject, int idChild) { NativeMethods.HWND nativeHwnd = NativeMethods.HWND.Cast(hwnd); // Ignore windows that have been destroyed if (!SafeNativeMethods.IsWindow(nativeHwnd)) { return; } AutomationElement rawEl = AutomationElement.FromHandle(hwnd); try { rawEl.GetCurrentPattern(WindowPattern.Pattern); } catch (InvalidOperationException) { // Only raise this event for elements with the WindowPattern. return; } Object windowInteractionState = rawEl.GetPatternPropertyValue(WindowPattern.WindowInteractionStateProperty, false); // if has no state value just return if (!(windowInteractionState is WindowInteractionState)) { return; } WindowInteractionState state = (WindowInteractionState)windowInteractionState; // Filter... avoid duplicate events if (hwnd == _lastHwnd && state == _lastState) { return; } AutomationPropertyChangedEventArgs e = new AutomationPropertyChangedEventArgs( WindowPattern.WindowInteractionStateProperty, hwnd == _lastHwnd ? _lastState : WindowInteractionState.Running, state); ClientEventManager.RaiseEventInThisClientOnly(AutomationElement.AutomationPropertyChangedEvent, rawEl, e); // save the last hwnd/rect for filtering out duplicates _lastHwnd = hwnd; _lastState = state; }
static void Main() { Application.ThreadExit += ThreadOnExit; string applicationMutex = @"Global\BcFFcd23-3456-6543-Fc44abcd1234"; mutex = new Mutex(true, applicationMutex); bool singleInstance = mutex.WaitOne(0, false); if (!singleInstance) { string appProductName = Process.GetCurrentProcess().MainModule.FileVersionInfo.ProductName; Process[] windowedProcesses = Process.GetProcesses().Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray(); foreach (Process process in windowedProcesses.Where(p => p.MainModule.FileVersionInfo.ProductName == appProductName)) { if (process.Id != Process.GetCurrentProcess().Id) { AutomationElement wElement = AutomationElement.FromHandle(process.MainWindowHandle); if (wElement.Current.IsOffscreen) { WindowPattern wPattern = wElement.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern; #if DEBUG WindowInteractionState state = wPattern.Current.WindowInteractionState; Debug.Assert(!(state == WindowInteractionState.NotResponding), "The application is not responding"); Debug.Assert(!(state == WindowInteractionState.BlockedByModalWindow), "Main Window blocked by a Modal Window"); #endif wPattern.SetWindowVisualState(WindowVisualState.Normal); break; } } } Thread.Sleep(200); MessageBox.Show("Application already running", "MyApplicationName", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); } if (SingleInstance) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MyAppMainForm()); } else { Application.ExitThread(); } }
public UIControl(string WinName, string ElemAutoID, int Tries) { WindowName = WinName; WinElemName = ElemAutoID; GetUIWindow(WinName, Tries); if (_Window != null) { GetWinElem(_Window, ElemAutoID, Tries); if (_WinElem == null) { GetWinElemByName(_Window, ElemAutoID, Tries); } } if (_WinElem != null) { Value = true; _WinState = GetPattern.GetWindowPattern(_Window).Current.WindowInteractionState; } }
static void Main() { Application.ThreadExit += new EventHandler(ThreadOnExit); string ApplicationMutex = "BcFFcd23-3456-6543-Fc44abcd1234"; mutex = new Mutex(true, ApplicationMutex); bool SingleInstance = mutex.WaitOne(0, false); if (!SingleInstance) { MessageBox.Show("Application already running", "[MyApplicationMainFormText]", MessageBoxButtons.OK, MessageBoxIcon.Information); string AppProductName = Process.GetCurrentProcess().MainModule.FileVersionInfo.ProductName; Process[] WindowedProcesses = Process.GetProcesses() .Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray(); foreach (Process process in WindowedProcesses.Where(p => p.MainModule.FileVersionInfo.ProductName == AppProductName)) { if (process.Id != Process.GetCurrentProcess().Id) { AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); if (!element.Current.IsOffscreen) { WindowPattern WPattern = element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern; WindowInteractionState state = WPattern.Current.WindowInteractionState; WPattern.SetWindowVisualState(WindowVisualState.Normal); break; } } } } if (SingleInstance) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MyAppMainForm()); } else { Application.ExitThread(); } }
/// <summary> /// Raises PropertyChangedEvent for WindowInteractionStateProperty. /// </summary> /// <param name="oldValue">Old WindowInteractionStateProperty.</param> /// <param name="newValue">New WindowInteractionStateProperty.</param> internal void RaiseInteractionStatePropertyChangedEvent(WindowInteractionState oldValue, WindowInteractionState newValue) { this.RaisePropertyChangedEvent(WindowPatternIdentifiers.WindowInteractionStateProperty, oldValue, newValue); this.RefreshIsTopMostProperty(); }