public IntNativeMethods.TEXTMETRIC GetTextMetrics() { IntNativeMethods.TEXTMETRIC lptm = new IntNativeMethods.TEXTMETRIC(); HandleRef hDC = new HandleRef(this.dc, this.dc.Hdc); bool flag = this.dc.MapMode != DeviceContextMapMode.Text; if (flag) { this.dc.SaveHdc(); } try { if (flag) { DeviceContextMapMode mode = this.dc.SetMapMode(DeviceContextMapMode.Text); } IntUnsafeNativeMethods.GetTextMetrics(hDC, ref lptm); } finally { if (flag) { this.dc.RestoreHdc(); } } return(lptm); }
/// <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(dc, dc.Hdc); // Set the mapping mode to MM_TEXT so we deal with units of pixels. DeviceContextMapMode mapMode = dc.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. dc.SaveHdc(); } try { if (setupDC) { mapMode = dc.SetMapMode(DeviceContextMapMode.Text); } IntUnsafeNativeMethods.GetTextMetrics(hdc, ref tm); } finally { if (setupDC) { dc.RestoreHdc(); } } return(tm); }