![]() Public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, Private static extern int GetSystemMetrics(int nIndex) Private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi) Private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData) Private const int BITBLT_CAPTURE = BITBLT_SRCCOPY | BITBLT_CAPTUREBLT ĭelegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData) Private const int BITBLT_CAPTUREBLT = 0x40000000 Private const int BITBLT_SRCCOPY = 0x00CC0020 Private const int MONITORINFOF_PRIMARY = 0x00000001 Private const string USER32 = const string GDI32 = const int PRIMARY_MONITOR = unchecked((int)0xBAADF00D) Var notifyInvalidated = MonitorInfoInvalidated _monitors = new Lazy>(GetMonitors, true) SystemEvents.UserPreferenceChanged -= OnUserPreferenceChanged SystemEvents.DisplaySettingsChanging -= OnDisplaySettingsChanging Private static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) Private static void OnDisplaySettingsChanging(object sender, EventArgs e) Monitors.Add(new MonitorInfo(hMonitor, hdcMonitor, deviceName, isPrimary, bounds, workArea)) Create info for this monitor and add it. Var workArea = Rectangle.FromLTRB(,, , ) Var isPrimary = (hMonitor = (IntPtr)PRIMARY_MONITOR) || ((info.Flags & MONITORINFOF_PRIMARY) != 0) Info.Size = Marshal.SizeOf(typeof(MONITORINFOEX)) Public bool Callback(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr lparam) SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged SystemEvents.DisplaySettingsChanging += OnDisplaySettingsChanging Register for events invalidating monitor info. Var result = BitBlt(destDC, 0, 0, m.Bounds.Width, m.Bounds.Height, monitorDC, 0, 0, unchecked((int)BITBLT_SRCCOPY)) ĮnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, cb.Callback, IntPtr.Zero) Var destDC = new HandleRef(null, destGraphics.GetHdc()) Var monitorDC = new HandleRef(null, hdc) Using (var destGraphics = Graphics.FromImage(screenBmp)) ScreenBmp = new Bitmap(m.Bounds.Width, m.Bounds.Height, PixelFormat.Format32bppArgb) ![]() Public static Bitmap CaptureScreen(MonitorInfo mi) Public static void CaptureScreen(MonitorInfo mi, string fileName) This.DeviceContextHandle = hDeviceContext Hdc = CreateDC(null, DeviceName, null, IntPtr.Zero) Public void WithMonitorHdc(Action action) Public readonly IntPtr DeviceContextHandle Public static event Action MonitorInfoInvalidated Private static Lazy> _monitors = new Lazy>(GetMonitors, true) Private static readonly bool _isSingleMonitor = GetSystemMetrics(SM_CMONITORS) = 0 / This is the version that is not dependent on Windows.Forms dll. If you don't want to take on a new dependency on the Windows.Forms dll, the ConnectedMonitors class below provides the same kind of functionality: /// Private static extern bool DeleteDC( IntPtr hdc) Static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData) Private const string GDI32 = EntryPoint = "CreateDC", CharSet = CharSet.Auto)] Hdc = CreateDC(null, screen.DeviceName, null, IntPtr.Zero) Public static void WithHdc(this Screen screen, Action action) Public static void ForAllScreens(Action actionWithHdc)įoreach (var screen in Screen.AllScreens) / This is an alternative that uses the Windows.Forms Screen class. The below code shows a wrapper around this class that helps with doing that: /// In this case you would register an eventhandler for the SystemEvents.DisplaySettingsChanged to trigger re-applying your get/set functions, and you would use interop calls to CreateDC and DeleteDC to get/release a device context handle (IntPtr) from the Screen.DeviceName property. ![]() If you already have a dependency on the Windows.Forms dll, or don't mind taking on this dependency, you can use its Screen class for this as pointed out in his answer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |