private static string GetText(IntPtr hWnd, TextReplaceMethod replaceMethod) //Взять текст { string selectedText = ""; switch (replaceMethod) { case TextReplaceMethod.TextBox: { int start = -1, next = -1; WinApi.SendMessage(hWnd, WinApi.EM_GETSEL, out start, out next); if (start != next) { // Возвращаемое значение длина текста в символах, не включая завершающий нулевой символ. int len = (int)WinApi.SendMessage(hWnd, WinApi.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero); StringBuilder sb = new StringBuilder(len + 1); int lenRead = (int)WinApi.SendMessage(hWnd, WinApi.WM_GETTEXT, (IntPtr)sb.Capacity, sb); if (lenRead > 0) { selectedText = sb.ToString().Substring(start, next - start); } } } break; case TextReplaceMethod.RichTextBox: { int start = -1, next = -1; WinApi.SendMessage(hWnd, WinApi.EM_GETSEL, out start, out next); if (start != next) { int len = next - start; StringBuilder sb = new StringBuilder(len + 1); int lenRead = (int)WinApi.SendMessage(hWnd, WinApi.EM_GETSELTEXT, IntPtr.Zero, sb); if (lenRead > 0) { selectedText = sb.ToString(); } } } break; case TextReplaceMethod.Scintilla: { int len = (int)WinApi.SendMessage(hWnd, WinApi.SCI_GETSELTEXT, IntPtr.Zero, IntPtr.Zero); if (len > 1) { StringBuilder sb = new StringBuilder(len); WinApi.SendMessage(hWnd, WinApi.SCI_GETSELTEXT, IntPtr.Zero, sb); //Не работает selectedText = sb.ToString(); } } break; case TextReplaceMethod.OtherTextControl: WinApi.SendMessage(hWnd, WinApi.WM_COPY, IntPtr.Zero, IntPtr.Zero); selectedText = Clipboard.GetText(); break; case TextReplaceMethod.Devenv: case TextReplaceMethod.Unknown: PressHotKey(new Keys[] { Keys.ControlKey, Keys.C }); selectedText = Clipboard.GetText(); break; } return(selectedText); }
public static void SetLayout(IntPtr hWnd, Language layout) { WinApi.PostMessage(hWnd, WinApi.WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, WinApi.LoadKeyboardLayout(string.Format("{0:X8}", (int)layout), WinApi.KLF_ACTIVATE)); }
public static void RunSwitcher(object o) { var swo = (SwitcherOptions)o; if ((swo & SwitcherOptions.SetLauot) != 0) { Thread.Sleep(50); } int pId; string pName; var hMainWnd = WinApi.GetForegroundWindow(); var threadId = WinApi.GetWindowThreadProcessId(hMainWnd, out pId); using (var p = Process.GetProcessById(pId)) pName = p.MainModule.ModuleName; Language layout = (Language)(short)WinApi.GetKeyboardLayout(threadId); //LOWORD var hWnd = Misc.GetFocusedHandle(threadId); if (hWnd == IntPtr.Zero) { hWnd = hMainWnd; } var cNameSb = new StringBuilder(32); WinApi.GetClassName(hWnd, cNameSb, cNameSb.Capacity); var classNames = cNameSb.ToString().Split(new char[] { ' ', '.' }, StringSplitOptions.RemoveEmptyEntries); if ((swo & SwitcherOptions.SwitchText) != 0 && (layout == Language.en_US || layout == Language.ru_RU)) { #region Filter var ma = TextReplaceMethod.OtherTextControl; switch (pName) { case "devenv.exe": ma = TextReplaceMethod.Devenv; break; } foreach (var className in classNames) { switch (className) { case "Static": case "STATIC": case "Button": case "BUTTON": case "SysListView32": case "SysTreeView32": case "ListBox": case "ScrollBar": case "ComboBox": case "COMBOBOX": case "msctls_hotkey32": case "ConsoleWindowClass": ma = TextReplaceMethod.Off; break; case "Edit": case "EDIT": ma = TextReplaceMethod.TextBox; break; case "RichEdit20W": ma = TextReplaceMethod.RichTextBox; break; case "Scintilla": //ma = TextReplaceMethod.Scintilla; //хз как починить break; } } #endregion if (ma != TextReplaceMethod.Off) { var success = Misc.SwitchText(hWnd, layout, ma); if (success && (swo & SwitcherOptions.SwitchLauotIsText) != 0) { swo |= SwitcherOptions.SwitchText; } } } if ((swo & SwitcherOptions.SwitchLauot) != 0) { #region Filter #endregion Misc.SwitchLayout(hWnd, layout); } else if ((swo & SwitcherOptions.SetLauot) != 0) { #region Filter #endregion Misc.SetLayout(hWnd, layout); } }
public static IntPtr GetFocusedHandle(uint threadId, WindowSelectorEventArgs e = null) //Получение хендла активного контрола { //@kurumpa http://stackoverflow.com/a/28409126 IntPtr hWnd = IntPtr.Zero; IntPtr focusedHandle = IntPtr.Zero; var info = new WinApi.GUITHREADINFO(); info.cbSize = Marshal.SizeOf(info); var success = WinApi.GetGUIThreadInfo(threadId, ref info); // target = hwndCaret || hwndFocus || (AttachThreadInput + GetFocus) || hwndActive var currentThreadId = WinApi.GetCurrentThreadId(); if (currentThreadId != threadId) { WinApi.AttachThreadInput(threadId, currentThreadId, true); } focusedHandle = WinApi.GetFocus(); if (e != null) { e.HGetActiveWindow = (int)WinApi.GetActiveWindow(); } if (currentThreadId != threadId) { WinApi.AttachThreadInput(threadId, currentThreadId, false); } if (e != null) { e.HGetFocus = (int)focusedHandle; if (success) { e.HCaret = (int)info.hwndCaret; e.HFocus = (int)info.hwndFocus; e.HActive = (int)info.hwndActive; e.HCapture = (int)info.hwndCapture; } } if (success) { if (info.hwndCaret != IntPtr.Zero) { hWnd = info.hwndCaret; } else if (info.hwndFocus != IntPtr.Zero) { hWnd = info.hwndFocus; } else if (focusedHandle != IntPtr.Zero) { hWnd = focusedHandle; } else if (info.hwndActive != IntPtr.Zero) { hWnd = info.hwndActive; } } else { hWnd = focusedHandle; } return(hWnd); }