public static System.Windows.Point GetAbsolutePosition(this Window w) { if (w.WindowState != WindowState.Maximized) { return(new System.Windows.Point(w.Left, w.Top)); } Int32Rect r; bool multimonSupported = User32.GetSystemMetrics(User32.SM_CMONITORS) != 0; if (!multimonSupported) { User32.RECT rc = new User32.RECT(); User32.SystemParametersInfo(48, 0, ref rc, 0); r = new Int32Rect(rc.Left, rc.Top, rc.Width, rc.Height); } else { WindowInteropHelper helper = new WindowInteropHelper(w); IntPtr hmonitor = User32.MonitorFromWindow(new HandleRef((object)null, helper.EnsureHandle()), 2); User32.MONITORINFOEX info = new User32.MONITORINFOEX(); User32.GetMonitorInfo(new HandleRef((object)null, hmonitor), info); r = new Int32Rect(info.rcWork.Left, info.rcWork.Top, info.rcWork.Width, info.rcWork.Height); } return(new System.Windows.Point(r.X, r.Y)); }
private static bool EnumMonitorCallback(IntPtr hMonitor, IntPtr hdc, ref NativeStructs.RECT rect, IntPtr dwData) { User32.MONITORINFOEX monitorInfo = new User32.MONITORINFOEX(); monitorInfo.cbSize = Marshal.SizeOf(monitorInfo); if (!User32.GetMonitorInfo(hMonitor, ref monitorInfo)) { throw new Win32Exception(); } _monitorInfo.Add(monitorInfo.szDevice, monitorInfo); _monitorHandle.Add(monitorInfo.szDevice, hMonitor); return(true); }
public static Rectangle GetScaledBounds(this Screen screen) { var hMonitor = screen.GetHMonitor(); var mi = new User32.MONITORINFOEX() { Size = Marshal.SizeOf(typeof(User32.MONITORINFOEX)) }; if (!User32.GetMonitorInfo(hMonitor, ref mi)) { return(Rectangle.Empty); } var pt = new Point(mi.Monitor.Left, mi.Monitor.Top); var size = getScaledScreenSize(User32.WindowFromPoint(pt)); return(new Rectangle(pt, size)); }
private void DrawMonitor(KeyValuePair <Pair <VirtualDesktop, HMONITOR>, ObservableCollection <DesktopWindow> > desktopMonitor, out float ScreenScalingFactorVert, out int mX, out int mY, out IEnumerable <Rectangle> gridGenerator) { HMONITOR m = desktopMonitor.Key.Item2; int windowsCount = desktopMonitor.Value.Count(); User32.MONITORINFOEX info = new User32.MONITORINFOEX(); info.cbSize = (uint)Marshal.SizeOf(info); User32.GetMonitorInfo(m, ref info); Gdi32.SafeHDC hdc = Gdi32.CreateDC(info.szDevice); int LogicalScreenHeight = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.VERTRES); int PhysicalScreenHeight = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.DESKTOPVERTRES); int LogicalScreenWidth = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.HORZRES); int PhysicalScreenWidth = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.DESKTOPHORZRES); hdc.Close(); float ScreenScalingFactorHoriz = (float)PhysicalScreenWidth / (float)LogicalScreenWidth; ScreenScalingFactorVert = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; mX = info.rcWork.X + MarginLeft; mY = info.rcWork.Y + MarginTop; int mWidth = info.rcWork.Width - MarginLeft - MarginRight; int mHeight = info.rcWork.Height - MarginTop - MarginBottom; Layout mCurrentLayout; try { mCurrentLayout = Layouts[desktopMonitor.Key]; } catch { Layouts.Add(desktopMonitor.Key, Layout.Tall); mCurrentLayout = Layouts[desktopMonitor.Key]; } if (!Factors.ContainsKey(desktopMonitor.Key)) { Factors[desktopMonitor.Key] = 0; } gridGenerator = GridGenerator(mWidth, mHeight, windowsCount, Factors[desktopMonitor.Key], mCurrentLayout, LayoutPadding); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var list = value as List <KeyValuePair <Pair <VirtualDesktop, HMONITOR>, Layout> >; writer.WriteStartArray(); foreach (KeyValuePair <Pair <VirtualDesktop, HMONITOR>, Layout> pair in list) { User32.MONITORINFOEX info = new User32.MONITORINFOEX(); info.cbSize = (uint)Marshal.SizeOf(info); User32.GetMonitorInfo(pair.Key.Item2, ref info); writer.WriteStartObject(); writer.WritePropertyName("Desktop"); writer.WriteValue(VirtualDesktop.DesktopNameFromDesktop(pair.Key.Item1)); writer.WritePropertyName("MonitorX"); writer.WriteValue(info.rcMonitor.X); writer.WritePropertyName("MonitorY"); writer.WriteValue(info.rcMonitor.Y); writer.WritePropertyName("Layout"); writer.WriteValue((int)pair.Value); writer.WriteEndObject(); } writer.WriteEndArray(); }
public MonitorInfoWithHandle(IntPtr monitorHandle, User32.MONITORINFOEX monitorInfo) { MonitorHandle = monitorHandle; MonitorInfo = monitorInfo; }