public static IntPtr SelectObject(HandleRef hdc, HandleRef obj) { IntPtr oldObj = IntSelectObject(hdc, obj); DbgUtil.AssertWin32(oldObj != IntPtr.Zero, "SelectObject(hdc=hObj=[0x{0:X8}], hObj=[0x{1:X8}]) failed.", hdc.Handle, obj.Handle); return(oldObj); }
public static bool Rectangle(HandleRef hdc, int left, int top, int right, int bottom) { bool retVal = IntRectangle(hdc, left, top, right, bottom); DbgUtil.AssertWin32(retVal, "Rectangle(hdc=[0x{0:X8}], left=[{1}], top=[{2}], right=[{3}], bottom=[{4}] failed.", hdc.Handle, left, top, right, bottom); return(retVal); }
public static string GetLastErrorStr() { int MAX_SIZE = 255; StringBuilder buffer = new StringBuilder(MAX_SIZE); string message; int err = 0; try { err = Marshal.GetLastWin32Error(); uint retVal = Kernel32.FormatMessage( Kernel32.FormatMessageOptions.IGNORE_INSERTS | Kernel32.FormatMessageOptions.FROM_SYSTEM, IntPtr.Zero, (uint)err, Kernel32.GetUserDefaultLCID(), buffer, MAX_SIZE, IntPtr.Zero); message = retVal != 0 ? buffer.ToString() : "<error returned>"; } catch (Exception ex) { if (DbgUtil.IsCriticalException(ex)) { throw; //rethrow critical exception. } message = ex.ToString(); } return(string.Format(CultureInfo.CurrentCulture, "0x{0:x8} - {1}", err, message)); }
public static IntPtr GetStockObject(int nIndex) { IntPtr hGdiObj = IntGetStockObject(nIndex); DbgUtil.AssertWin32(hGdiObj != IntPtr.Zero, "GetStockObject({0}) failed.", nIndex); return(hGdiObj); }
public static int GetMapMode(HandleRef hDC) { int mapMode = IntGetMapMode(hDC); DbgUtil.AssertWin32(mapMode != 0, "GetMapMode(hdc=[0x{0:X8}]", hDC.Handle); return(mapMode); }
public static bool LineTo(HandleRef hdc, int x, int y) { bool retVal = IntLineTo(hdc, x, y); DbgUtil.AssertWin32(retVal, "LineTo(hdc=[0x{0:X8}], x=[{1}], y=[{2}] failed.", hdc.Handle, x, y); return(retVal); }
/// <summary> /// Saves the current state of the device context by copying data describing selected objects and graphic /// modes (such as the bitmap, brush, palette, font, pen, region, drawing mode, and mapping mode) to a /// context stack. /// The SaveDC function can be used any number of times to save any number of instances of the DC state. /// A saved state can be restored by using the RestoreHdc method. /// See MSDN for more details. /// </summary> public int SaveHdc() { HandleRef hdc = new HandleRef(this, Hdc); int state = Interop.Gdi32.SaveDC(hdc); if (contextStack == null) { contextStack = new Stack(); } GraphicsState g = new GraphicsState(); g.hBitmap = hCurrentBmp; g.hBrush = hCurrentBrush; g.hPen = hCurrentPen; g.hFont = hCurrentFont; g.font = new WeakReference(selectedFont); contextStack.Push(g); #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("state[0]=DC.SaveHdc(hDc=0x{1:x8})", state, unchecked ((int)this.hDC)))); #endif return(state); }
public static bool Ellipse(HandleRef hDc, int x1, int y1, int x2, int y2) { bool retVal = IntEllipse(hDc, x1, y1, x2, y2); DbgUtil.AssertWin32(retVal, "Ellipse(hdc=[0x{0:X8}], x1=[{1}], y1=[{2}], x2=[{3}], y2=[{4}]) failed.", hDc.Handle, x1, y1, x2, y2); return(retVal); }
public static int GetClipRgn(HandleRef hDC, HandleRef hRgn) { int retVal = IntGetClipRgn(hDC, hRgn); DbgUtil.AssertWin32(retVal != -1, "IntGetClipRgn([hdc=0x{0:X8}], [hRgn]) failed.", hDC.Handle); return(retVal); }
public static bool StrokePath(HandleRef hDC) { bool retVal = IntStrokePath(hDC); DbgUtil.AssertWin32(retVal, "StrokePath(hdc=[0x{0:X8}]failed.", hDC.Handle); return(retVal); }
public static bool AngleArc(HandleRef hDC, int x, int y, int radius, float startAngle, float endAngle) { bool retVal = IntAngleArc(hDC, x, y, radius, startAngle, endAngle); DbgUtil.AssertWin32(retVal, "AngleArc(hdc=[0x{0:X8}], ...) failed.", hDC.Handle); return(retVal); }
public static bool FillRect(HandleRef hDC, [In] ref IntNativeMethods.RECT rect, HandleRef hbrush) { bool retVal = IntFillRect(hDC, ref rect, hbrush); DbgUtil.AssertWin32(retVal, "FillRect(hdc=[0x{0:X8}], rect=[{1}], hbrush=[{2}]", hDC.Handle, rect, hbrush.Handle); return(retVal); }
public static int GetTextMetrics(HandleRef hDC, ref IntNativeMethods.TEXTMETRIC lptm) { int retVal = IntUnsafeNativeMethods.GetTextMetricsW(hDC, ref lptm); DbgUtil.AssertWin32(retVal != 0, "GetTextMetrics(hdc=[0x{0:X8}], [out TEXTMETRIC] failed.", hDC.Handle); return(retVal); }
public static int DrawTextEx(HandleRef hDC, string text, ref IntNativeMethods.RECT lpRect, int nFormat, [In, Out] IntNativeMethods.DRAWTEXTPARAMS lpDTParams) { int retVal = DrawTextExW(hDC, text, text.Length, ref lpRect, nFormat, lpDTParams); DbgUtil.AssertWin32(retVal != 0, "DrawTextEx(hdc=[0x{0:X8}], text=[{1}], rect=[{2}], flags=[{3}] failed.", hDC.Handle, text, lpRect, nFormat); return(retVal); }
public static int GetBkMode(HandleRef hDC) { int mode = IntGetBkMode(hDC); DbgUtil.AssertWin32(mode != 0, "GetBkMode(hdc=[0x{0:X8}]) failed.", hDC.Handle); return(mode); }
public static int SetBkMode(HandleRef hDC, int nBkMode) { int oldMode = IntSetBkMode(hDC, nBkMode); DbgUtil.AssertWin32(oldMode != 0, "SetBkMode(hdc=[0x{0:X8}], Mode=[{1}]) failed.", hDC.Handle, nBkMode); return(oldMode); }
internal void Dispose(bool disposing) { bool deletedHandle = false; if (_ownHandle) { if (!_ownedByCacheManager || !disposing) { // If we were ever owned by the CacheManger and we're being disposed // we can be sure that we're not in use by any DC's (otherwise Dispose() wouldn't have been called) // skip the check IsFontInUse check in this case. // Also skip the check if disposing == false, because the cache is thread-static // and that means we're being called from the finalizer. if (_everOwnedByCacheManager || !disposing || !DeviceContexts.IsFontInUse(this)) { Debug.Assert(Hfont != IntPtr.Zero, "Unexpected null hFont."); DbgUtil.AssertFinalization(this, disposing); Interop.Gdi32.DeleteObject(Hfont); Hfont = IntPtr.Zero; _ownHandle = false; deletedHandle = true; } } } if (disposing && (deletedHandle || !_ownHandle)) { GC.SuppressFinalize(this); } }
public static int SetMapMode(HandleRef hDC, int nMapMode) { int oldMapMode = IntSetMapMode(hDC, nMapMode); DbgUtil.AssertWin32(oldMapMode != 0, "SetMapMode(hdc=[0x{0:X8}], MapMode=[{1}]", hDC.Handle, nMapMode); return(oldMapMode); }
public static IntPtr GetCurrentObject(HandleRef hDC, int uObjectType) { IntPtr hGdiObj = IntGetCurrentObject(hDC, uObjectType); // If the selected object is a region the return value is HGI_ERROR on failure. DbgUtil.AssertWin32(hGdiObj != IntPtr.Zero, "GetObject(hdc=[0x{0:X8}], type=[{1}]) failed.", hDC, uObjectType); return(hGdiObj); }
public static void AssertWin32(bool expression, string message) { #if DEBUG if (!expression) { Debug.Fail(message + "\r\nError: " + DbgUtil.GetLastErrorStr()); } #endif }
internal void Dispose(bool disposing) { if (disposed) { return; } Disposing?.Invoke(this, EventArgs.Empty); disposed = true; DisposeFont(disposing); switch (dcType) { case DeviceContextType.Display: Debug.Assert(disposing, "WARNING: Finalizing a Display DeviceContext.\r\nReleaseDC may fail when not called from the same thread GetDC was called from."); ((IDeviceContext)this).ReleaseHdc(); break; case DeviceContextType.Information: case DeviceContextType.NamedDevice: // CreateDC and CreateIC add an HDC handle to the HandleCollector; to remove it properly we need // to call DeleteHDC. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteHDC(hdc=0x{0:x8})", unchecked ((int)this.hDC)))); #endif UnsafeNativeMethods.DeleteDC(new HandleRef(this, hDC)); hDC = IntPtr.Zero; break; case DeviceContextType.Memory: // CreatCompatibleDC adds a GDI handle to HandleCollector, to remove it properly we need to call // DeleteDC. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteDC(hdc=0x{0:x8})", unchecked ((int)this.hDC)))); #endif UnsafeNativeMethods.DeleteDC(new HandleRef(this, hDC)); hDC = IntPtr.Zero; break; case DeviceContextType.Unknown: default: return; // do nothing, the hdc is not owned by this object. // in this case it is ok if disposed throught finalization. } DbgUtil.AssertFinalization(this, disposing); }
[Conditional("DEBUG")] // This code will be compiled into the assembly anyways, it is up to the compiler to ignore the call. private static void AssertWin32Impl(bool expression, string format, object[] args) { #if DEBUG if (!expression) { string message = string.Format(CultureInfo.CurrentCulture, format, args); Debug.Fail(message + "\r\nError: " + DbgUtil.GetLastErrorStr()); } #endif }
protected override void CreateBrush() { IntPtr nativeHandle = SafeNativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(Color)); if (nativeHandle == IntPtr.Zero) // Don't use Debug.Assert, DbgUtil.GetLastErrorStr would always be evaluated. { Debug.Fail("CreateSolidBrush failed : " + DbgUtil.GetLastErrorStr()); } NativeHandle = nativeHandle; // sets the handle value in the base class. }
protected override void CreateBrush() { IntPtr nativeHandle = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(Color)); // Don't use Debug.Assert, DbgUtil.GetLastErrorStr would always be evaluated. if (nativeHandle == IntPtr.Zero) { Debug.Fail("CreateSolidBrush failed : " + DbgUtil.GetLastErrorStr()); } HBrush = nativeHandle; }
/// <summary> /// Constructor to contruct a DeviceContext object from an window handle. /// </summary> private DeviceContext(IntPtr hWnd) { _hWnd = hWnd; DeviceContextType = DeviceContextType.Display; DeviceContexts.AddDeviceContext(this); // the hDc will be created on demand. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DeviceContext( hWnd=0x{0:x8} )", unchecked ((int)hWnd)))); #endif }
/// <summary> /// If the object was created from a DC, this object doesn't 'own' the dc so we just ignore /// this call. /// </summary> void IDeviceContext.ReleaseHdc() { if (_hDC != IntPtr.Zero && DeviceContextType == DeviceContextType.Display) { #if TRACK_HDC int retVal = #endif User32.ReleaseDC(new HandleRef(this, _hWnd), _hDC); // Note: retVal == 0 means it was not released but doesn't necessarily means an error; class or private DCs are never released. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("[ret={0}]=DC.ReleaseDC(hDc=0x{1:x8}, hWnd=0x{2:x8})", retVal, unchecked ((int)this.hDC), unchecked ((int)this.hWnd)))); #endif _hDC = IntPtr.Zero; } }
/// <summary> /// Explicit interface method implementation to hide them a bit for usability reasons so the object is seen /// as a wrapper around an hdc that is always available, and for performance reasons since it caches the hdc /// if used in this way. /// </summary> IntPtr IDeviceContext.GetHdc() { if (_hDC == IntPtr.Zero) { Debug.Assert(DeviceContextType == DeviceContextType.Display, "Calling GetDC from a non display/window device."); // Note: for common DCs, GetDC assigns default attributes to the DC each time it is retrieved. // For example, the default font is System. _hDC = User32.GetDC(new HandleRef(this, _hWnd)); #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("hdc[0x{0:x8}]=DC.GetHdc(hWnd=0x{1:x8})", unchecked ((int)this.hDC), unchecked ((int)this.hWnd)))); #endif } return(_hDC); }
/// <summary> /// Constructor to contruct a DeviceContext object from an existing Win32 device context handle. /// </summary> private DeviceContext(IntPtr hDC, DeviceContextType dcType) { _hDC = hDC; DeviceContextType = dcType; CacheInitialState(); DeviceContexts.AddDeviceContext(this); if (dcType == DeviceContextType.Display) { _hWnd = User32.WindowFromDC(new HandleRef(this, this._hDC)); } #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DeviceContext( hDC=0x{0:X8}, Type={1} )", unchecked ((int)hDC), dcType))); #endif }
protected virtual void Dispose(bool disposing) { if (dc != null && nativeHandle != IntPtr.Zero) { DbgUtil.AssertFinalization(this, disposing); dc.DeleteObject(nativeHandle, GdiObjectType.Brush); nativeHandle = IntPtr.Zero; } if (disposing) { GC.SuppressFinalize(this); } }
public static bool Arc( HandleRef hDC, int nLeftRect, // x-coord of rectangle's upper-left corner int nTopRect, // y-coord of rectangle's upper-left corner int nRightRect, // x-coord of rectangle's lower-right corner int nBottomRect, // y-coord of rectangle's lower-right corner int nXStartArc, // x-coord of first radial ending point int nYStartArc, // y-coord of first radial ending point int nXEndArc, // x-coord of second radial ending point int nYEndArc // y-coord of second radial ending point ) { bool retVal = IntArc(hDC, nLeftRect, nTopRect, nRightRect, nBottomRect, nXStartArc, nYStartArc, nXEndArc, nYEndArc); DbgUtil.AssertWin32(retVal, "Arc(hdc=[0x{0:X8}], ...) failed.", hDC.Handle); return(retVal); }