private static void DoYourThang() { //get the focused control IntPtr forgroundWindow = WinApi.GetForegroundWindow(); if (forgroundWindow != IntPtr.Zero) { string selText = null; uint myThreadId = WinApi.GetCurrentThreadId(); uint otherProcessId = 0; uint otherThreadId = WinApi.GetWindowThreadProcessId(forgroundWindow, out otherProcessId); //attach ourselves if (WinApi.AttachThreadInput(myThreadId, otherThreadId, true)) { //get the focused control IntPtr ptr = WinApi.GetFocus(); //what kinda control is it StringBuilder className = new StringBuilder(4096); WinApi.GetClassName(ptr, className, 4096); //get the text of the control HandleRef r = new HandleRef(_keyHook, ptr); StringBuilder sb = new StringBuilder(1048576); WinApi.SendMessageGetText(r, (uint)WinApi.WindowsMessages.WM_GETTEXT, 1048576, sb); string textContents = sb.ToString(); if (textContents.Length > 0) { //get the start and end of the control int start = 0; int end = 0; WinApi.SendMessageGetSel(r, (uint)WinApi.WindowsMessages.EM_GETSEL, ref start, ref end); if (end <= textContents.Length) { selText = textContents.Substring(start, end - start); if (selText != null && selText.Trim().Length == 0) { selText = null; } } } //detach WinApi.AttachThreadInput(myThreadId, otherThreadId, false); } else { int i = Marshal.GetLastWin32Error(); } if (selText != null) { //We got highlighted text... MethodInvoker i = delegate() { Process.Start("http://www.google.com/search?q=" + HttpUtility.UrlEncode(selText)); }; new Thread(new ThreadStart(i)).Start(); } } }
public void Run(int hWndMain, Automate automate, KConnection connection, KConnection destination, Form parent) { do { int hWnd = Automate.WinWait("KCMLMasterForm_32", "Enter host details", Automate.WinMatchMode.Start, 5); int ret; if (hWnd == 0) { break; } uint procID = WinApi.GetWindowThreadProcessId(hWnd, 0); uint currThread = WinApi.GetCurrentThreadId(); bool lret = WinApi.AttachThreadInput(currThread, procID, true); WinApi.SetForegroundWindow(hWnd); ret = Automate.SetFocus(hWnd); int hCtrlHost = Automate.ControlGetHandle(hWnd, "KCMLEdit32", 1); ret = Automate.SetFocus(hCtrlHost); Automate.SendString(hCtrlHost, destination.Host); Thread.Sleep(500); int hCtrlUserID = Automate.ControlGetHandle(hWnd, "KCMLEdit32", 2); ret = Automate.SetFocus(hCtrlUserID); Automate.SendString(hCtrlUserID, destination.User); Thread.Sleep(500); int hCtrlPassword = Automate.ControlGetHandle(hWnd, "KCMLEdit32", 3); ret = Automate.SetFocus(hCtrlPassword); Automate.SendString(hCtrlPassword, destination.Password); Thread.Sleep(500); int hCtrlHome = Automate.ControlGetHandle(hWnd, "KCMLEdit32", 4); ret = Automate.SetFocus(hCtrlHome); Automate.SendString(hCtrlHome, destination.Home); Thread.Sleep(500); } while (false); }