/// <summary> /// Returns a TEXTMETRIC structure for the font selected in the device context /// represented by this object, in units of pixels. /// </summary> public Gdi32.TEXTMETRICW GetTextMetrics() { // Set the mapping mode to MM_TEXT so we deal with units of pixels. Gdi32.MM mapMode = DeviceContext.MapMode; bool setupDC = mapMode != Gdi32.MM.TEXT; if (setupDC) { // Changing the MapMode will affect viewport and window extent and origin, we save the dc // state so all those properties can be properly restored once done. DeviceContext.SaveHdc(); } try { if (setupDC) { mapMode = DeviceContext.SetMapMode(Gdi32.MM.TEXT); } var tm = new Gdi32.TEXTMETRICW(); Gdi32.GetTextMetricsW(DeviceContext, ref tm); return(tm); } finally { if (setupDC) { DeviceContext.RestoreHdc(); } } }
/// <summary> /// Returns a TEXTMETRIC structure for the font selected in the device context /// represented by this object, in units of pixels. /// </summary> public IntNativeMethods.TEXTMETRIC GetTextMetrics() { IntNativeMethods.TEXTMETRIC tm = new IntNativeMethods.TEXTMETRIC(); HandleRef hdc = new HandleRef(DeviceContext, DeviceContext.Hdc); // Set the mapping mode to MM_TEXT so we deal with units of pixels. DeviceContextMapMode mapMode = DeviceContext.MapMode; bool setupDC = mapMode != DeviceContextMapMode.Text; if (setupDC) { // Changing the MapMode will affect viewport and window extent and origin, we save the dc // state so all those properties can be properly restored once done. DeviceContext.SaveHdc(); } try { if (setupDC) { mapMode = DeviceContext.SetMapMode(DeviceContextMapMode.Text); } IntUnsafeNativeMethods.GetTextMetrics(hdc, ref tm); } finally { if (setupDC) { DeviceContext.RestoreHdc(); } } return(tm); }
internal void Dispose(bool disposing) { if (dc != null) { DbgUtil.AssertFinalization(this, disposing); try { // Restore original dc. dc.RestoreHdc(); if (disposeDc) { dc.Dispose(disposing); } if (graphics != null) // if created from a Graphics object... { graphics.ReleaseHdcInternal(dc.Hdc); graphics = null; } } catch (Exception ex) { if (ClientUtils.IsSecurityOrCriticalException(ex)) { throw; // rethrow the original exception. } Debug.Fail("Exception thrown during disposing: \r\n" + ex.ToString()); } finally { dc = null; } } }