private void spawnNearWindow() { Win32.GUITHREADINFO gui = new Win32.GUITHREADINFO(); gui.cbSize = Marshal.SizeOf(gui); bool success = Win32.GetGUIThreadInfo(0, ref gui); if (success) { if (gui.hwndActive != IntPtr.Zero) { Win32.RECT windowRect = new Win32.RECT(); bool success2 = Win32.GetWindowRect(gui.hwndActive, ref windowRect); if (success2) { var windowPlacement = ((App)Application.Current).Preferences.windowPlacement; var insideOutsidePlacement = ((App)Application.Current).Preferences.insideOutsidePlacement; WindowUtils.PutWindowNear(this, windowRect.asRect(), windowPlacement, insideOutsidePlacement); } } else { // No active window -- spawn somewhere in the active monitor instead spawnInMonitor(); } } else { // TODO: log these, don't crash throw new Win32Exception(); } }
private bool putNearTextCaret() { Win32.GUITHREADINFO gui = new Win32.GUITHREADINFO(); gui.cbSize = Marshal.SizeOf(gui); bool success = Win32.GetGUIThreadInfo(0, ref gui); if (success) { if (gui.hwndCaret == IntPtr.Zero) { /* The focused application has no caret information, so * gracefully degrade to some alternative method. */ return(false); } else { /* The GUI's caret position is relative to its control, * so get the control's position and add the two. */ Win32.RECT windowRect = new Win32.RECT(); bool success2 = Win32.GetWindowRect(gui.hwndCaret, ref windowRect); if (success2) { Rect caretRect = gui.rcCaret.asRect(); caretRect.X += windowRect.left; caretRect.Y += windowRect.top; WindowUtils.PutWindowNear(this, caretRect, PlacementSide.BOTTOM_LEFT, PlacementInOut.OUTSIDE); return(true); } else { /* TODO: before production release, handle these * exceptions and always fall back to cursor pos */ throw new Win32Exception(); } } } else { // TODO: see above throw new Win32Exception(); } }