/// <summary> /// Returns highlighted text (if any) /// </summary> /// <returns>highlighted text</returns> protected String GetSelectedText(IntPtr handleText) { var selectedText = String.Empty; if (handleText == IntPtr.Zero) { return(selectedText); } try { Int32 start = -1; Int32 end = -1; User32Interop.SendMessageRefRef(handleText, User32Interop.EM_GETSEL, ref start, ref end); var text = GetText(); if (end > text.Length) { end = text.Length - 1; } if (end > start) { selectedText = text.Substring(start, end - start); } } catch (Exception e) { Log.Exception(e); } return(selectedText); }
private void SettingsSaveBtn_Click(object sender, EventArgs e) { try { ini.IniWriteValue("Hotkeys", "Close", settingsCloseCmb.SelectedItem.ToString() + "+" + settingsCloseHKTxt.Text); ini.IniWriteValue("Hotkeys", "Stop", settingsStopCmb.SelectedItem.ToString() + "+" + settingsStopTxt.Text); ini.IniWriteValue("Hotkeys", "TopMost", settingsTopCmb.SelectedItem.ToString() + "+" + settingsTopTxt.Text); User32Interop.UnregisterHotKey(mainForm.Handle, KillProcess_HotkeyID); User32Interop.UnregisterHotKey(mainForm.Handle, TopMost_HotkeyID); User32Interop.UnregisterHotKey(mainForm.Handle, StopSession_HotkeyID); //RegHotkeys(KillProcess_HotkeyID, GetMod(settingsCloseCmb.SelectedItem.ToString()), (int)Enum.Parse(typeof(Keys), settingsCloseHKTxt.Text)); //RegHotkeys(TopMost_HotkeyID, GetMod(settingsTopCmb.SelectedItem.ToString()), (int)Enum.Parse(typeof(Keys), settingsTopTxt.Text)); //RegHotkeys(StopSession_HotkeyID, GetMod(settingsStopCmb.SelectedItem.ToString()), (int)Enum.Parse(typeof(Keys), settingsStopTxt.Text)); User32Interop.RegisterHotKey(mainForm.Handle, KillProcess_HotkeyID, GetMod(ini.IniReadValue("Hotkeys", "Close").Split('+')[0].ToString()), (int)Enum.Parse(typeof(Keys), ini.IniReadValue("Hotkeys", "Close").Split('+')[1].ToString())); User32Interop.RegisterHotKey(mainForm.Handle, TopMost_HotkeyID, GetMod(ini.IniReadValue("Hotkeys", "TopMost").Split('+')[0].ToString()), (int)Enum.Parse(typeof(Keys), ini.IniReadValue("Hotkeys", "TopMost").Split('+')[1].ToString())); User32Interop.RegisterHotKey(mainForm.Handle, StopSession_HotkeyID, GetMod(ini.IniReadValue("Hotkeys", "Stop").Split('+')[0].ToString()), (int)Enum.Parse(typeof(Keys), ini.IniReadValue("Hotkeys", "Stop").Split('+')[1].ToString())); for (int i = 0; i < controllerGuids.Length; i++) { if (!string.IsNullOrEmpty(controllerGuids[i].Text)) //&& !string.IsNullOrEmpty(controllerNicks[i].Text)) { ini.IniWriteValue("ControllerMapping", controllerGuids[i].Text, controllerNicks[i].Text); } } if (positionsControl != null) { positionsControl.Refresh(); } if (!string.IsNullOrEmpty(keyboardNick.Text)) { ini.IniWriteValue("ControllerMapping", "Keyboard", keyboardNick.Text); } ini.IniWriteValue("Misc", "UseNicksInGame", useNicksCheck.Checked.ToString()); ini.IniWriteValue("Misc", "DebugLog", debugLogCheck.Checked.ToString()); ini.IniWriteValue("Misc", "Network", cmb_Network.SelectedItem.ToString()); ini.IniWriteValue("Misc", "SteamLang", cmb_Lang.SelectedItem.ToString()); ini.IniWriteValue("Misc", "ShowStatus", statusCheck.Checked.ToString()); ini.IniWriteValue("Misc", "KeepAccounts", statusCheck.Checked.ToString()); //ini.IniWriteValue("CustomLayout", "Enabled", enableCustomCheckbox.Checked.ToString()); ini.IniWriteValue("CustomLayout", "HorizontalLines", numHorDiv.Value.ToString()); ini.IniWriteValue("CustomLayout", "VerticalLines", numVerDiv.Value.ToString()); ini.IniWriteValue("CustomLayout", "MaxPlayers", numMaxPlyrs.Value.ToString()); //ini.IniWriteValue("Misc", "VibrateOpen", check_Vibrate.Checked.ToString()); MessageBox.Show("Settings saved succesfully!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.None); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Notifies event subscribers of a keyboard event /// </summary> /// <param name="wParam">key info</param> /// <param name="hookStruct">keyboard info</param> /// <param name="handled">was the event handled? set to true if so</param> private void notifyEvent(KeyboardWParam wParam, KBDLLHOOKSTRUCT hookStruct, ref bool handled) { var args = new KeyEventArgs((Keys)hookStruct.vkCode) { Handled = false }; switch (wParam) { case KeyboardWParam.WM_SYSKEYDOWN: if (EvtKeyDown != null) { EvtKeyDown(this, args); } break; case KeyboardWParam.WM_KEYDOWN: if (EvtKeyDown != null) { EvtKeyDown(this, args); } if (EvtKeyPress != null) { bool isShiftDown = (User32Interop.GetKeyState(VK_SHIFT) & 0x80) == 0x80; bool isCapsLockDown = User32Interop.GetKeyState(VK_CAPITAL) != 0; var keyState = new byte[256]; var inBuffer = new byte[2]; User32Interop.GetKeyboardState(keyState); if (User32Interop.ToAscii(hookStruct.vkCode, hookStruct.scanCode, keyState, inBuffer, hookStruct.flags) == 1) { var key = (char)inBuffer[0]; if ((isCapsLockDown ^ isShiftDown) && Char.IsLetter(key)) { key = Char.ToUpper(key); } var e = new KeyPressEventArgs(key); EvtKeyPress(this, e); } } break; case KeyboardWParam.WM_SYSKEYUP: case KeyboardWParam.WM_KEYUP: if (EvtKeyUp != null) { EvtKeyUp(this, args); } break; } handled = args.Handled; }
/// <summary> /// Gets a list of active windows sorted on the /// indcated sort order /// </summary> /// <param name="order">sort order</param> /// <returns>sorted list</returns> private List <EnumWindows.WindowInfo> getWindowList(SortOrder order) { var processName = FilterByProcessName.Trim(); var windowList = EnumWindows.Enumerate(); if (!String.IsNullOrEmpty(processName)) { var filteredList = new List <EnumWindows.WindowInfo>(); for (int ii = 0; ii < windowList.Count; ii++) { var process = WindowActivityMonitor.GetProcessForWindow(windowList[ii].Handle); if (String.Compare(process.ProcessName, FilterByProcessName, true) == 0) { filteredList.Add(windowList[ii]); } } windowList = filteredList; } var sortedList = sortWindowList(windowList, order); if (String.IsNullOrEmpty(processName)) { IntPtr desktopHWnd = User32Interop.GetDesktopWindow(); if (desktopHWnd != null) { sortedList.Insert(0, new EnumWindows.WindowInfo(desktopHWnd, R.GetString("ShowDesktop"))); } } return(sortedList); }
/// <summary> /// Positions the dockee to the left of the parent window /// </summary> private void positionWindow() { User32Interop.RECT windowRect; User32Interop.GetWindowRect(_windowHandleDockTo, out windowRect); var rect = new Rectangle(0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top); var point = new Point(windowRect.left, windowRect.top); Log.Debug("parent location: " + point); Log.Debug("parent rectangle: " + rect); int screenWidth = Screen.FromControl(_form).Bounds.Width; int left = Math.Max(windowRect.left, 0); if (left + _form.Width > screenWidth) { left = screenWidth - _form.Width; } _form.Left = left; int top = Math.Max(windowRect.top - _form.Height, 0); _form.Top = top; }
private void FullScreenTimer_Tick(object sender, EventArgs e) { // This timer is for hiding controls and cursor // After every 3 seconds when no mouse movement if (IsFullScreen & Player.Media != null) { shouldHide = User32Interop.GetLastInput() > activityThreshold; if (cursorHidden != shouldHide) { if (shouldHide) { ShowCursor(0); PanelBackControls.Visible = false; tableLayoutPanel1.RowCount = 1; } else { ShowCursor(1); PanelBackControls.Visible = true; tableLayoutPanel1.RowCount = 2; } cursorHidden = shouldHide; } } else { Timer2.Stop(); } }
public static void HideBorder(IntPtr handle) { uint lStyle = User32Interop.GetWindowLong(handle, User32_WS.GWL_STYLE); lStyle &= ~(User32_WS.WS_CAPTION | User32_WS.WS_BORDER | User32_WS.WS_DLGFRAME | User32_WS.WS_SIZEBOX | User32_WS.WS_THICKFRAME); User32Interop.SetWindowLong(handle, User32_WS.GWL_STYLE, lStyle); }
/// <summary> /// Checks if any part of the specified window is obscured by /// another window. Enumerates all the windows, checks the bounding /// rectangle of each and ensures that the rectangle doesn't intersect /// with our window /// </summary> /// <returns>true if it is</returns> private bool isObscuredWindow() { if (_window == null || _window.Handle == IntPtr.Zero || !Windows.GetVisible(_window)) { return(false); } IntPtr windowHandle = _window.Handle; // to keep track of whether we have already visited this window var cache = new HashSet <IntPtr> { windowHandle }; User32Interop.RECT windowRect; User32Interop.GetWindowRect(windowHandle, out windowRect); // now, step through all active windows and check for overlap while ((windowHandle = User32Interop.GetWindow(windowHandle, User32Interop.GW_HWNDPREV)) != IntPtr.Zero && !cache.Contains(windowHandle)) { User32Interop.RECT rect; User32Interop.RECT intersection; cache.Add(windowHandle); bool isScanner = false; if (!_force && User32Interop.IsWindowVisible(windowHandle)) { try { Control ctl = Form.FromHandle(windowHandle); isScanner = (ctl is Form) && (ctl is IScannerPanel); if (isScanner) { double opacity = Windows.GetOpacity(ctl as Form); isScanner = (opacity == 1.0f); } } catch (Exception ex) { isScanner = false; Log.Debug("isScanner logic exception " + ex); } } if (User32Interop.IsWindowVisible(windowHandle) && !Windows.IsMinimized(windowHandle) && !isScanner && User32Interop.GetWindowRect(windowHandle, out rect) && User32Interop.IntersectRect(out intersection, ref windowRect, ref rect)) { return(true); } } cache.Clear(); return(false); }
public void CenterCursor() { List <PlayerInfo> players = profile.PlayerData; if (players == null) { return; } for (int i = 0; i < players.Count; i++) { PlayerInfo p = players[i]; if (p.IsKeyboardPlayer) { ProcessInfo data = p.ProcessData; if (data == null) { continue; } Rectangle r = p.MonitorBounds; //Cursor.Clip = r; if (data.HWnd != null) { User32Interop.SetForegroundWindow(data.HWnd.NativePtr); } } } }
/// <summary> /// Unselects any selection /// </summary> protected void UnselectText(IntPtr handleText) { if (handleText != IntPtr.Zero) { User32Interop.SendMessageIntInt(handleText, User32Interop.EM_SETSEL, -1, -1); } }
/// <summary> /// Handles docking of the form to the right of the parent window /// </summary> private void handleDockRight() { User32Interop.RECT windowRect; User32Interop.GetWindowRect(_windowHandleDockTo, out windowRect); int screenWidth = Screen.FromControl(_form).Bounds.Width; int parentWidth = windowRect.right - windowRect.left; int parentHeight = windowRect.bottom - windowRect.top; int spaceLeftHoriz = screenWidth - windowRect.right; if (spaceLeftHoriz < _form.Width) { var parentFormLeft = (screenWidth - _form.Width - parentWidth); User32Interop.MoveWindow(_windowHandleDockTo, parentFormLeft, windowRect.top, parentWidth, parentHeight, true); User32Interop.GetWindowRect(_windowHandleDockTo, out windowRect); } _form.Left = windowRect.right; switch (_dockPosition) { case Windows.WindowPosition.TopRight: _form.Top = windowRect.top; break; case Windows.WindowPosition.MiddleRight: _form.Top = windowRect.top + (parentHeight - _form.Height) / 2; break; case Windows.WindowPosition.BottomRight: _form.Top = windowRect.bottom - _form.Height; break; } }
/// <summary> /// Perform the operation - page through the list, /// activate a window etc /// </summary> /// <param name="itemTag">Meta data about seleted item</param> private void handleSelect(ItemTag itemTag) { switch (itemTag.DataType) { case ItemTag.ItemType.NextPage: gotoNextPage(); break; case ItemTag.ItemType.PreviousPage: gotoPreviousPage(); break; case ItemTag.ItemType.OrderBy: switchSortOrder(); break; case ItemTag.ItemType.Window: if (itemTag.WInfo != null) { if (!User32Interop.IsWindow(itemTag.WInfo.Handle) || !User32Interop.IsWindowVisible(itemTag.WInfo.Handle)) { DialogUtils.ShowTimedDialog(this, "Window does not exist"); } else if (DialogUtils.ConfirmScanner("Switch to " + itemTag.WInfo.Title + "?")) { if (EvtActivateWindow != null) { EvtActivateWindow.BeginInvoke(this, itemTag.WInfo, null, null); } } } break; } }
private static void Main() { // if already running, activate it if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1) { IntPtr handle = User32Interop.FindWindow(null, _title); if (handle != IntPtr.Zero) { User32Interop.ShowWindow(handle.ToInt32(), User32Interop.SW_SHOW); } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FileUtils.LogAssemblyInfo(); Splash splash = new Splash(1000); splash.Show(); splash.Close(); DashboardSettings.PreferencesFilePath = FileUtils.GetFullPathRelativeToApp(_settingsFileName); Settings = DashboardSettings.Load(); var form = new DashboardForm { Text = _title }; Application.Run(form); }
public static void ShowTaskBar() { IntPtr hwnd = User32Interop.FindWindow("Shell_TrayWnd", ""); User32Interop.ShowWindow(hwnd, WindowShowStyle.Show); IntPtr hwndOrb = User32Interop.FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, null); User32Interop.ShowWindow(hwndOrb, WindowShowStyle.Show); }
/// <summary> /// Indicates if text is highlighted in the window /// </summary> /// <returns></returns> public override bool IsTextSelected() { int start = -1; int end = -1; User32Interop.SendMessageRefRef(_handleTextWindow, User32Interop.EM_GETSEL, ref start, ref end); return(start >= 0 && end >= 0 && start != end); }
/// <summary> /// Clears text in window /// </summary> /// <returns></returns> protected void ClearText(IntPtr handleText) { if (handleText != IntPtr.Zero) { const uint WM_SETTEXT = 0x000C; User32Interop.SendMessageText(handleText, WM_SETTEXT, IntPtr.Zero, String.Empty); } }
/// <summary> /// Set focus to the specified window /// </summary> /// <param name="handle">handle to the window</param> private void activateWindow(IntPtr handle) { if (handle != IntPtr.Zero) { User32Interop.SetFocus(handle); Thread.Sleep(1000); Windows.SetForegroundWindow(handle); } }
/// <summary> /// Create the contexual menu for the sys tray /// </summary> /// <param name="e">event args</param> protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); var hSysMenu = User32Interop.GetSystemMenu(this.Handle, false); User32Interop.AppendMenu(hSysMenu, MF_SEPARATOR, 0, string.Empty); User32Interop.AppendMenu(hSysMenu, MF_STRING, SYSMENU_EXITID, "E&xit…"); }
/// <summary> /// Sets the global mouse hook. /// </summary> /// <returns>true on success</returns> public bool SetHook() { _hookProc = HookProc; _hookHandle = User32Interop.SetWindowsHookEx( (int)HookType.WH_MOUSE_LL, _hookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0); return(_hookHandle != IntPtr.Zero); }
/// <summary> /// Set keyboard hook to start capturing keyboard events globally /// </summary> /// <returns>true on success</returns> public bool SetHook() { _hookProc = HookProc; _hookHandle = User32Interop.SetWindowsHookEx( (int)HookType.WH_KEYBOARD_LL, _hookProc, System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress, 0); return(_hookHandle != IntPtr.Zero); }
public static Control GetFocusControl() { Control focusControl = null; IntPtr focusHandle = User32Interop.GetFocus(); if (focusHandle != IntPtr.Zero) { focusControl = Control.FromHandle(focusHandle); } return(focusControl); }
private void cancel() { if (_selectedTask != null) { User32Interop.SetForegroundWindow(_selectedTask.Handle); User32Interop.ShowWindow(_selectedTask.Handle.ToInt32(), User32Interop.SW_SHOW); User32Interop.BringWindowToTop(_selectedTask.Handle); } Windows.CloseForm(this); }
/// <summary> /// Closes the talk window. Checks if lecture manager /// is active, if so, shows it. Else, launches a /// new instance /// </summary> /// <param name="form">scanner form</param> private void showLectureManager(Form form) { IntPtr handle = User32Interop.FindWindow(null, R.GetString("LectureManager")); if (handle != IntPtr.Zero) { User32Interop.SetForegroundWindow(handle); } else { launchLectureManager(form); } }
/// <summary> /// Removes a previously installed mouse hook /// </summary> /// <returns></returns> public bool RemoveHook() { if (_hookHandle != IntPtr.Zero) { if (!User32Interop.UnhookWindowsHookEx(_hookHandle)) { return(false); } _hookHandle = IntPtr.Zero; } return(true); }
/// <summary> /// Called when the window is shown /// </summary> /// <param name="e">event arg</param> protected override void OnShown(EventArgs e) { Log.Debug(); base.OnShown(e); User32Interop.SetForegroundWindow(this.Handle); if (_talkWindowTextBox != null) { ActiveControl = _talkWindowTextBox; _talkWindowTextBox.Select(); } }
/// <summary> /// User selected a window to switch to. Trigger an event /// to indicate this. /// </summary> /// <param name="wInfo">Window info of the window selected</param> private void handleWindowSelect(EnumWindows.WindowInfo wInfo) { if (!User32Interop.IsWindow(wInfo.Handle) || !User32Interop.IsWindowVisible(wInfo.Handle)) { DialogUtils.ShowTimedDialog(this, Resources.WindowDoesNotExist); } else if (DialogUtils.ConfirmScanner(string.Format(Resources.SwitchTo, wInfo.Title))) { if (EvtActivateWindow != null) { EvtActivateWindow.BeginInvoke(this, wInfo, null, null); } } }
/// <summary> /// User selected a window to switch to. Trigger an event /// to indicate this. /// </summary> /// <param name="wInfo">Window info of the window selected</param> private void handleWindowSelect(EnumWindows.WindowInfo wInfo) { if (!User32Interop.IsWindow(wInfo.Handle) || !User32Interop.IsWindowVisible(wInfo.Handle)) { DialogUtils.ShowTimedDialog(this, R.GetString("WindowNotFound")); } else if (DialogUtils.ConfirmScanner(String.Format(R.GetString("ConfirmSwitchToWindow"), wInfo.Title))) { if (EvtActivateWindow != null) { EvtActivateWindow.BeginInvoke(this, wInfo, null, null); } } }
/// <summary> /// Indicates whether a shift needs to be pressed for the /// specified character. /// </summary> /// <param name="c">the character</param> /// <returns>true if needed</returns> private bool shiftNeeded(char c) { short virtualKey = User32Interop.VkKeyScanEx(c, _keyboardLayout); Log.Debug("virtualKey for [" + c + "] is " + virtualKey); if ((virtualKey & 0x100) == 0x100) { Log.Debug("Shift needs to be pressed for [" + c + "]"); return(true); } Log.Debug("Shift does not need to be pressed for [" + c + "]"); return(false); }
/// <summary> /// Closes the talk window. Checks if lecture manager /// is active, if so, shows it. Else, launches a /// new instance /// </summary> /// <param name="form">scanner form</param> private void showLectureManager(Form form) { Context.AppTalkWindowManager.CloseTalkWindow(); IntPtr handle = User32Interop.FindWindow(null, "Lecture Manager"); if (handle != IntPtr.Zero) { User32Interop.SetForegroundWindow(handle); } else { launchLectureManager(form); } }
/// <summary> /// Low level hook func that is invoked by windows whenever there is a /// global keyboard event /// </summary> /// <param name="nCode">the code</param> /// <param name="wParam">wParam</param> /// <param name="lParam">lParam</param> /// <returns>-1 if handled, otherwise if not handled</returns> public int HookProc(int nCode, IntPtr wParam, IntPtr lParam) { bool handled = false; if (nCode >= 0) { var keyboardLLHookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); var wmKeyboard = (KeyboardWParam)wParam; notifyEvent(wmKeyboard, keyboardLLHookStruct, ref handled); } return(handled ? -1 : User32Interop.CallNextHookEx(_hookHandle, nCode, wParam, lParam)); }