/// <summary> /// Gets the dpi of the monitor. /// </summary> /// <param name="pt">A point specifying the monitor that should be used.</param> /// <param name="dpiType">A value of MonitorDpiType indicating the dpi type to be returned.</param> /// <returns>The dpi of the monitor.</returns> public static int GetMonitorDpiFromPoint(WinPoint pt, MonitorDpiType dpiType) { IntPtr hmonitor = MonitorFromPoint(pt, MonitorOptions.MONITOR_DEFUALTTONEAREST); GetDpiForMonitor(hmonitor, dpiType, out int dpiX, out int dpiY); return(dpiX); }
/// <summary> /// Maps a point in screen coordinates to coordinates relative to the active window. /// </summary> /// <param name="point">A point in screen coordinates.</param> /// <returns>The given point in coordinates relative to the active window.</returns> public static WinPoint ScreenPointToClient(WinPoint point) { IntPtr hWnd = GetForegroundWindow(); ScreenToClient(hWnd, ref point); return(point); }
/// <summary> /// Returns the color of the pixel at the mouse's position. /// </summary> public static Color GetPixelColor(WinPoint point) { Bitmap bmp = new Bitmap(1, 1); using (Graphics g = Graphics.FromImage(bmp)) { try { g.CopyFromScreen(point.X, point.Y, 0, 0, new Size(1, 1)); } catch { } } Color color = bmp.GetPixel(0, 0); bmp.Dispose(); return(Color.FromArgb(color.R, color.G, color.B)); }
/// <summary> /// Creates a PositionData instance from a given point. /// </summary> public PositionData(Point position) { WinPoint winPos = (WinPoint)position; this.PhysicalPosition = position; // Determine dpi information this.Dpi = WinApi.GetMonitorDpiFromPoint(winPos, MonitorDpiType.EFFECTIVE_DPI); this.DpiScaling = Math.Round(Dpi / (double)96, 2); // Determine scaled position. this.ScaledPosition = new Point((int)(position.X / DpiScaling), (int)(position.Y / DpiScaling)); // Determine relative position. this.RelativePosition = (Point)WinApi.ScreenPointToClient(winPos); // Determine raw dpi. this.RawDpi = WinApi.GetMonitorDpiFromPoint(winPos, MonitorDpiType.RAW_DPI); this.DpiRawRatio = Math.Round(Dpi / (double)RawDpi, 2); // Determine screen bounds this.ScreenResolution = Screen.FromPoint(position).Bounds.Size; // Determine color at mouse position. this.PixelColor = GetPixelColor(winPos); }
private static extern IntPtr MonitorFromPoint(WinPoint pt, MonitorOptions dwFlags);
private static extern bool ScreenToClient(IntPtr hWnd, ref WinPoint lpPoint);