static bool TryGetErrorMessage(uint error, StringBuilder sb, out string errorMsg) { errorMsg = ""; var flags = FormatMessageFlags.IGNORE_INSERTS | FormatMessageFlags.ARGUMENT_ARRAY | FormatMessageFlags.FROM_SYSTEM; uint result = WinMethods.FormatMessage(flags, IntPtr.Zero, error, 0, sb, (uint)sb.Capacity + 1, null); if (result != 0) { //remove trailing junk int i = sb.Length; while (i > 0) { char ch = sb[i - 1]; if (ch > 32 && ch != '.') { break; } i--; } errorMsg = sb.ToString(0, i); } else if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) { return(false); } else { errorMsg = "Unknown error (0x" + Convert.ToString(error, 16) + ")"; } return(true); }
internal Screen(IntPtr monitor, IntPtr hdc) { IntPtr screenDC = hdc; if (!multiMonitorSupport || monitor == (IntPtr)PRIMARY_MONITOR) { // Single monitor system bounds = new Rectangle(new Point(0, 0), PrimaryMonitorSize); primary = true; deviceName = "DISPLAY"; } else { var info = new MonitorInfoEx(); info.Init(); WinMethods.GetMonitorInfo(ToHR(monitor), ref info); bounds = info.Monitor; //implicit conversion primary = ((info.Flags & MONITORINFOF_PRIMARY) != 0); deviceName = new string(info.DeviceName); deviceName = deviceName.TrimEnd((char)0); if (hdc == IntPtr.Zero) { screenDC = WinMethods.CreateDC(deviceName, null, null, IntPtr.Zero); } } hmonitor = monitor; this.bitDepth = WinMethods.GetDeviceCaps(ToHR(screenDC), DeviceCap.BITSPIXEL); this.bitDepth *= WinMethods.GetDeviceCaps(ToHR(screenDC), DeviceCap.PLANES); this.vRfresh = WinMethods.GetDeviceCaps(ToHR(screenDC), DeviceCap.VREFRESH); this.pWidthMm = WinMethods.GetDeviceCaps(ToHR(screenDC), DeviceCap.HORZSIZE); this.pHeightMm = WinMethods.GetDeviceCaps(ToHR(screenDC), DeviceCap.VERTSIZE); if (hdc != screenDC) { WinMethods.DeleteDC(ToHR(screenDC)); } }
static void InitScreens() { if (multiMonitorSupport) { MonitorEnumCallback closure = new MonitorEnumCallback(); WinMethods.EnumMonitorsDelegate proc = new WinMethods.EnumMonitorsDelegate(closure.Callback); WinMethods.EnumDisplayMonitors(NullHandleRef, IntPtr.Zero, proc, IntPtr.Zero); if (closure.ScreenList.Count > 0) { Screen[] temp = new Screen[closure.ScreenList.Count]; closure.ScreenList.CopyTo(temp, 0); screens = temp; } else { screens = new Screen[] { new Screen((IntPtr)PRIMARY_MONITOR) }; } } else { screens = new Screen[] { new Screen((IntPtr)PRIMARY_MONITOR, IntPtr.Zero) }; } }