// 작업 결과 값을 반환할 경우 CodeActivity<TResult>에서 파생되고 // Execute 메서드에서 값을 반환합니다. protected override void Execute(CodeActivityContext context) { // 텍스트 입력 인수의 런타임 값을 가져옵니다. string text = context.GetValue(this.Text); HWND hWnd = context.GetValue(this.WindowHandle); if (hWnd == IntPtr.Zero) { int nRetryCnt = 0; while (nRetryCnt < m_RetrySec) { Debug.WriteLine("FindWindow {0}, {1}", ProcessName, WindowTitle); hWnd = WindowList.FindWindowByTitle(ProcessName, WindowTitle, false); //"notepad++", "Notepad" if (hWnd != IntPtr.Zero) { break; } Thread.Sleep(1000); nRetryCnt++; } if (hWnd == IntPtr.Zero) { Debug.WriteLine("Not Found Window"); return; } } Debug.WriteLine("Maxmize Window"); WindowList.ShowMaxmizeWindow(hWnd); }
// 작업 결과 값을 반환할 경우 CodeActivity<TResult>에서 파생되고 // Execute 메서드에서 값을 반환합니다. protected override void Execute(CodeActivityContext context) { this.ResultBool.Set(context, false); // 텍스트 입력 인수의 런타임 값을 가져옵니다. //string text = context.GetValue(this.Text); List <int> piList = WindowList.GetProcessIdByWindowByTitle(ProcessName, WindowTitle, false); foreach (int pid in piList) { Debug.WriteLine("Found Pid {0}", pid.ToString()); try { Process proc = Process.GetProcessById(pid); proc.Kill(); } catch (ArgumentException ex) { // Process already exited. CommonException.PrintExceptionLog(ex); } } this.ResultBool.Set(context, true); }
public static List <int> GetProcessIdByWindowByTitle(string sProcessName, string sTitle, bool bWholeWord) { List <int> piList = new List <int>(); Process ps = null; if (sTitle == null) { sTitle = ""; } foreach (KeyValuePair <IntPtr, string> window in WindowList.GetOpenWindows()) { IntPtr handle = window.Key; string title = window.Value; if (sProcessName != "") //input에 processName이 있으면 process name 일치 여부를 먼저 체크 { uint pid = 0; GetWindowThreadProcessId(handle, out pid); ps = Process.GetProcessById((int)pid); if (ps.ProcessName != sProcessName) { continue; } } if (bWholeWord) { if (title == sTitle) { piList.Add(ps.Id); } } else { if (title.Contains(sTitle)) { piList.Add(ps.Id); } } } return(piList); }
public static HWND FindWindowByTitle(string sProcessName, string sTitle, bool bWholeWord) { foreach (KeyValuePair <IntPtr, string> window in WindowList.GetOpenWindows()) { IntPtr handle = window.Key; string title = window.Value; if (sProcessName != "") //input에 processName이 있으면 process name 일치 여부를 먼저 체크 { uint pid = 0; GetWindowThreadProcessId(handle, out pid); Process ps = Process.GetProcessById((int)pid); if (ps.ProcessName != sProcessName) { continue; } } if (bWholeWord) { if (title == sTitle) { return(handle); } } else { if (title.Contains(sTitle)) { return(handle); } } } return(IntPtr.Zero); }
// 작업 결과 값을 반환할 경우 CodeActivity<TResult>에서 파생되고 // Execute 메서드에서 값을 반환합니다. protected override void Execute(CodeActivityContext context) { // 텍스트 입력 인수의 런타임 값을 가져옵니다. string text = context.GetValue(this.Text); this.ResultBool.Set(context, false); HWND hWnd = context.GetValue(this.WindowHandle); if (hWnd == IntPtr.Zero) { int nRetryCnt = 0; while (nRetryCnt < m_RetrySec) { Debug.WriteLine("FindWindow {0}, {1}", ProcessName, WindowTitle); hWnd = WindowList.FindWindowByTitle(ProcessName, WindowTitle, false); //"notepad++", "Notepad" if (hWnd != IntPtr.Zero) { break; } Thread.Sleep(1000); nRetryCnt++; } if (hWnd == IntPtr.Zero) { Debug.WriteLine("Not Found Window"); return; } } m_FoundWindowHandle = hWnd; Debug.WriteLine("Found Window"); ControlType controlType; AutomationElement foundElement = null; if (hWnd != IntPtr.Zero) { System.Windows.Automation.ControlType.Button.ToString(); // ControlType class bug ㅜ.ㅜ, 첫번째 호출시 결과 return 안됨. tostring 호출하여 먼저 초기화 필요 int nRetryCnt = 0; while (nRetryCnt < m_RetrySec) { controlType = System.Windows.Automation.ControlType.LookupById(int.Parse(m_ControlTypeId)); Debug.WriteLine("controlTypeId : {0}, {1}", m_ControlTypeId, controlType); Debug.WriteLine("Find element {0}, {1}", ControlName, controlType.ToString()); foundElement = FindElement(hWnd, AutomationId, ControlName, controlType); if (foundElement != null) { break; } Thread.Sleep(1000); nRetryCnt++; } } if (foundElement == null) { Debug.WriteLine("Not Found Element"); return; } Debug.WriteLine("Found Element"); /* * try * { * foundElement.SetFocus(); * } * catch(Exception ex) * { * PrintExceptionLog(ex); * } */ int x = 0, y = 0; try { var clickablePoint = foundElement.GetClickablePoint(); x = (int)clickablePoint.X; y = (int)clickablePoint.Y; } catch (Exception ex) { System.Windows.Rect rect = foundElement.Current.BoundingRectangle; if (rect.X > 0 && rect.Y > 0) { x = (int)(rect.X + rect.Width) / 2; //center y = (int)(rect.Y + rect.Height) / 2; //center } } Debug.WriteLine("Point {0}, {1}", x, y); if (x > 0 && y > 0) { Cursor.Position = new System.Drawing.Point(x, y); //Cursor.Clip = new Rectangle(this.Location, this.Size); } if ((x <= 0 || y <= 0) && MouseClickAction != MouseClickType.None) { object objPattern; if (true == foundElement.TryGetCurrentPattern(InvokePattern.Pattern, out objPattern)) { InvokeControl(foundElement); } } else { if (MouseClickAction == MouseClickType.Click) { DoMouseClickEvent(MouseButton, x, y); } if (MouseClickAction == MouseClickType.DblClick) { DoMouseDblClickEvent(MouseButton, x, y); } } WaitForCompleted(); this.ResultBool.Set(context, true); }