public static SafeDC CreateCompatibleDC(SafeDC hdc) { SafeDC safeDC = null; try { IntPtr hdc2 = IntPtr.Zero; if (hdc != null) { hdc2 = hdc.handle; } safeDC = SafeDC.NativeMethods.CreateCompatibleDC(hdc2); if (safeDC == null) { HRESULT.ThrowLastError(); } } finally { if (safeDC != null) { safeDC._created = true; } } if (safeDC.IsInvalid) { safeDC.Dispose(); throw new SystemException("Unable to create a device context from the specified device information."); } return(safeDC); }
public static void UpdateLayeredWindow(IntPtr hwnd, SafeDC hdcDst, ref POINT pptDst, ref SIZE psize, SafeDC hdcSrc, ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags) { if (!NativeMethods._UpdateLayeredWindow(hwnd, hdcDst, ref pptDst, ref psize, hdcSrc, ref pptSrc, crKey, ref pblend, dwFlags)) { HRESULT.ThrowLastError(); } }
private static int _GetBitDepth() { if (s_bitDepth == 0) { using (SafeDC dc = SafeDC.GetDesktop()) { s_bitDepth = NativeMethods.GetDeviceCaps(dc, DeviceCap.BITSPIXEL) * NativeMethods.GetDeviceCaps(dc, DeviceCap.PLANES); } } return(s_bitDepth); }
public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj) { IntPtr intPtr = NativeMethods._SelectObjectSafeHBITMAP(hdc, hgdiobj); if (intPtr == IntPtr.Zero) { HRESULT.ThrowLastError(); } return(intPtr); }
static DpiHelper() { using (var desktop = SafeDC.GetDesktop()) { var pixelsPerInchX = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX); var pixelsPerInchY = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY); _transformToDip = Matrix.Identity; _transformToDip.Scale(96d / pixelsPerInchX, 96d / pixelsPerInchY); _transformToDevice = Matrix.Identity; _transformToDevice.Scale(pixelsPerInchX / 96d, pixelsPerInchY / 96d); } }
static DpiHelper() { using (SafeDC desktop = SafeDC.GetDesktop()) { int deviceCaps = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX); int deviceCaps2 = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY); DpiHelper._transformToDip = Matrix.Identity; DpiHelper._transformToDip.Scale(96.0 / (double)deviceCaps, 96.0 / (double)deviceCaps2); DpiHelper._transformToDevice = Matrix.Identity; DpiHelper._transformToDevice.Scale((double)deviceCaps / 96.0, (double)deviceCaps2 / 96.0); } }
static DpiHelper() { using (SafeDC desktop = SafeDC.GetDesktop()) { // Can get these in the static constructor. They shouldn't vary window to window, // and changing the system DPI requires a restart. int pixelsPerInchX = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX); int pixelsPerInchY = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY); _transformToDip = Matrix.Identity; _transformToDip.Scale(96d / (double)pixelsPerInchX, 96d / (double)pixelsPerInchY); _transformToDevice = Matrix.Identity; _transformToDevice.Scale((double)pixelsPerInchX / 96d, (double)pixelsPerInchY / 96d); } }
public static SafeHBITMAP CreateDIBSection(SafeDC hdc, ref BITMAPINFO bitmapInfo, out IntPtr ppvBits, IntPtr hSection, int dwOffset) { SafeHBITMAP safeHBITMAP; if (hdc == null) { safeHBITMAP = NativeMethods._CreateDIBSectionIntPtr(IntPtr.Zero, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset); } else { safeHBITMAP = NativeMethods._CreateDIBSection(hdc, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset); } if (safeHBITMAP.IsInvalid) { HRESULT.ThrowLastError(); } return(safeHBITMAP); }
public static SafeDC GetDC(IntPtr hwnd) { SafeDC safeDC = null; try { safeDC = SafeDC.NativeMethods.GetDC(hwnd); } finally { if (safeDC != null) { safeDC.Hwnd = hwnd; } } if (safeDC.IsInvalid) { HRESULT.E_FAIL.ThrowIfFailed(); } return(safeDC); }
public static SafeDC CreateDC(string deviceName) { SafeDC safeDC = null; try { safeDC = SafeDC.NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero); } finally { if (safeDC != null) { safeDC._created = true; } } if (safeDC.IsInvalid) { safeDC.Dispose(); throw new SystemException("Unable to create a device context from the specified device information."); } return(safeDC); }
private static extern bool _UpdateLayeredWindow(IntPtr hwnd, SafeDC hdcDst, [In] ref POINT pptDst, [In] ref SIZE psize, SafeDC hdcSrc, [In] ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags);
public void Show() { _VerifyMutability(); Stream imageStream = null; try { // Try to use the filepath first. If it's not provided or not available, use the embedded resource. if (!string.IsNullOrEmpty(ImageFileName) && File.Exists(ImageFileName)) { try { imageStream = new FileStream(ImageFileName, FileMode.Open); } catch (IOException) { } } if (imageStream == null) { imageStream = _resourceManager.GetStream(ResourceName, CultureInfo.CurrentUICulture); if (imageStream == null) { throw new IOException("The resource could not be found."); } } Size bitmapSize; _hBitmap = _CreateHBITMAPFromImageStream(imageStream, out bitmapSize); Point location = new Point( (NativeMethods.GetSystemMetrics(SM.CXSCREEN) - bitmapSize.Width) / 2, (NativeMethods.GetSystemMetrics(SM.CYSCREEN) - bitmapSize.Height) / 2); // Pass a null WndProc. Let the MessageWindow use DefWindowProc. _hwndWrapper = new MessageWindow( CS.HREDRAW | CS.VREDRAW, WS.POPUP | WS.VISIBLE, WS_EX.WINDOWEDGE | WS_EX.TOOLWINDOW | WS_EX.LAYERED | (IsTopMost ? WS_EX.TOPMOST : 0), new Rect(location, bitmapSize), "Splash Screen", null); byte opacity = (byte)(FadeInDuration > TimeSpan.Zero ? 0 : 255); using (SafeDC hScreenDC = SafeDC.GetDesktop()) { using (SafeDC memDC = SafeDC.CreateCompatibleDC(hScreenDC)) { IntPtr hOldBitmap = NativeMethods.SelectObject(memDC, _hBitmap); RECT hwndRect = NativeMethods.GetWindowRect(_hwndWrapper.Handle); POINT hwndPos = hwndRect.Position; SIZE hwndSize = hwndRect.Size; POINT origin = new POINT(); BLENDFUNCTION bf = _BaseBlendFunction; bf.SourceConstantAlpha = opacity; NativeMethods.UpdateLayeredWindow(_hwndWrapper.Handle, hScreenDC, ref hwndPos, ref hwndSize, memDC, ref origin, 0, ref bf, ULW.ALPHA); NativeMethods.SelectObject(memDC, hOldBitmap); } } if (CloseOnMainWindowCreation) { Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Loaded, (DispatcherOperationCallback) delegate(object splashObj) { var splashScreen = (SplashScreen)splashObj; if (!splashScreen._isClosed) { splashScreen.Close(); } return(null); }, this); } _dispatcher = Dispatcher.CurrentDispatcher; if (FadeInDuration > TimeSpan.Zero) { _fadeInEnd = DateTime.UtcNow + FadeInDuration; _dt = new DispatcherTimer(FadeInDuration, DispatcherPriority.Normal, _FadeInTick, _dispatcher); _dt.Start(); } } finally { Utility.SafeDispose(ref imageStream); } }
private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
private static extern IntPtr _SelectObject(SafeDC hdc, IntPtr hgdiobj);
public static extern int GetDeviceCaps(SafeDC hdc, DeviceCap nIndex);
private static extern SafeHBITMAP _CreateDIBSection(SafeDC hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, out IntPtr ppvBits, IntPtr hSection, int dwOffset);
public static SafeDC GetDesktop() { return(SafeDC.GetDC(IntPtr.Zero)); }