private WindowsFont(IntNativeMethods.LOGFONT lf, bool createHandle) { this.fontSize = -1f; this.logFont = lf; if (this.logFont.lfFaceName == null) { this.logFont.lfFaceName = "Microsoft Sans Serif"; } this.style = FontStyle.Regular; if (lf.lfWeight == 700) { this.style |= FontStyle.Bold; } if (lf.lfItalic == 1) { this.style |= FontStyle.Italic; } if (lf.lfUnderline == 1) { this.style |= FontStyle.Underline; } if (lf.lfStrikeOut == 1) { this.style |= FontStyle.Strikeout; } if (createHandle) { this.CreateFont(); } }
public static int DrawText(HandleRef hDC, string text, ref IntNativeMethods.RECT lpRect, int nFormat) { if (Marshal.SystemDefaultCharSize == 1) { lpRect.top = Math.Min(0x7fff, lpRect.top); lpRect.left = Math.Min(0x7fff, lpRect.left); lpRect.right = Math.Min(0x7fff, lpRect.right); lpRect.bottom = Math.Min(0x7fff, lpRect.bottom); int num2 = WideCharToMultiByte(0, 0, text, text.Length, null, 0, IntPtr.Zero, IntPtr.Zero); byte[] pOutBytes = new byte[num2]; WideCharToMultiByte(0, 0, text, text.Length, pOutBytes, pOutBytes.Length, IntPtr.Zero, IntPtr.Zero); num2 = Math.Min(num2, 0x2000); return DrawTextA(hDC, pOutBytes, num2, ref lpRect, nFormat); } return DrawTextW(hDC, text, text.Length, ref lpRect, nFormat); }
public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { if (((((flags & IntTextFormatFlags.Bottom) == IntTextFormatFlags.Default) && ((flags & IntTextFormatFlags.VerticalCenter) == IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.SingleLine) != IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.CalculateRectangle) != IntTextFormatFlags.Default)) { return bounds; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); flags |= IntTextFormatFlags.CalculateRectangle; int num = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int) flags, dtparams); if (num > bounds.Height) { return bounds; } Rectangle rectangle = bounds; if ((flags & IntTextFormatFlags.VerticalCenter) != IntTextFormatFlags.Default) { rectangle.Y = (rectangle.Top + (rectangle.Height / 2)) - (num / 2); return rectangle; } rectangle.Y = rectangle.Bottom - num; return rectangle; }
public static extern int DrawTextA(HandleRef hDC, byte[] lpszString, int byteCount, ref IntNativeMethods.RECT lpRect, int nFormat);
public static int GetObject(HandleRef hFont, IntNativeMethods.LOGFONT lp) { int retVal = IntGetObject(hFont, System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntNativeMethods.LOGFONT)), lp); DbgUtil.AssertWin32(retVal != 0, "GetObject(hObj=[0x{0:X8}], [LOGFONT]) failed.", unchecked((int) hFont.Handle) ); return retVal; }
public static extern int DrawTextW(HandleRef hDC, string lpszString, int nCount, ref IntNativeMethods.RECT lpRect, int nFormat);
public static IntPtr ExtCreatePen(int fnStyle, int dwWidth, IntNativeMethods.LOGBRUSH lplb, int dwStyleCount, int[] lpStyle) { IntPtr hPen = System.Internal.HandleCollector.Add(IntExtCreatePen(fnStyle, dwWidth, lplb, dwStyleCount, lpStyle), IntSafeNativeMethods.CommonHandles.GDI); DbgUtil.AssertWin32(hPen != IntPtr.Zero, "IntExtCreatePen(style={0}, width={1}, brush={2}, styleCount={3}, styles={4}) failed.", fnStyle, dwWidth, lplb, dwStyleCount, lpStyle); return hPen; }
public static int GetObject(HandleRef hBrush, IntNativeMethods.LOGBRUSH lb) { int retVal = IntGetObject(hBrush, System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntNativeMethods.LOGBRUSH)), lb); DbgUtil.AssertWin32(retVal != 0, "GetObject(hObj=[0x{0:X8}], [LOGBRUSH]) failed.", unchecked((int) hBrush.Handle) ); return retVal; }
public static int GetObject(HandleRef hBrush, IntNativeMethods.LOGBRUSH lb) { return IntGetObject(hBrush, Marshal.SizeOf(typeof(IntNativeMethods.LOGBRUSH)), lb); }
private static extern IntPtr IntExtCreatePen(int fnStyle, int dwWidth, IntNativeMethods.LOGBRUSH lplb, int dwStyleCount, [MarshalAs(UnmanagedType.LPArray)] int[] lpStyle);
internal static extern bool ExtTextOut(HandleRef hdc, int x, int y, int options, ref IntNativeMethods.RECT rect, string str, int length, int[] spacing);
public static bool MoveToEx(HandleRef hdc, int x, int y, IntNativeMethods.POINT pt) { bool retVal = IntMoveToEx(hdc, x, y, pt); DbgUtil.AssertWin32(retVal, "MoveToEx(hdc=[0x{0:X8}], x=[{1}], y=[{2}], pt=[{3}] failed.", unchecked((int) hdc.Handle), x, y, pt ); return retVal; }
private WindowsFont( IntNativeMethods.LOGFONT lf, bool createHandle ) { Debug.Assert( lf != null, "lf is null" ); this.logFont = lf; if (this.logFont.lfFaceName == null) { this.logFont.lfFaceName = defaultFaceName; } this.style = FontStyle.Regular; if (lf.lfWeight == IntNativeMethods.FW_BOLD) { this.style |= FontStyle.Bold; } if (lf.lfItalic == 1) { this.style |= FontStyle.Italic; } if (lf.lfUnderline == 1) { this.style |= FontStyle.Underline; } if (lf.lfStrikeOut == 1) { this.style |= FontStyle.Strikeout; } if( createHandle ) { CreateFont(); } }
public static extern int DrawTextExA(HandleRef hDC, byte[] lpszString, int byteCount, ref IntNativeMethods.RECT lpRect, int nFormat, [In, Out] IntNativeMethods.DRAWTEXTPARAMS lpDTParams);
public static IntPtr ExtCreatePen(int fnStyle, int dwWidth, IntNativeMethods.LOGBRUSH lplb, int dwStyleCount, int[] lpStyle) { return System.Internal.HandleCollector.Add(IntExtCreatePen(fnStyle, dwWidth, lplb, dwStyleCount, lpStyle), CommonHandles.GDI); }
public static WindowsBrush FromLogBrush( DeviceContext dc, IntNativeMethods.LOGBRUSH logBrush ) { Debug.Assert( logBrush != null, "logBrush is null" ); switch( logBrush.lbStyle ) { // currently supported brushes: case IntNativeMethods.BS_HATCHED: return new WindowsHatchBrush(dc, (WindowsHatchStyle) logBrush.lbHatch ); case IntNativeMethods.BS_SOLID: return new WindowsSolidBrush( dc, Color.FromArgb(logBrush.lbColor) ); default: Debug.Fail( "Don't know how to create WindowsBrush from specified logBrush" ); return null; } }
public static int GetObject(HandleRef hFont, IntNativeMethods.LOGFONT lp) { return IntGetObject(hFont, Marshal.SizeOf(typeof(IntNativeMethods.LOGFONT)), lp); }
public static bool MoveToEx(HandleRef hdc, int x, int y, IntNativeMethods.POINT pt) { return IntMoveToEx(hdc, x, y, pt); }
public static int GetTextMetrics(HandleRef hDC, ref IntNativeMethods.TEXTMETRIC lptm) { if (Marshal.SystemDefaultCharSize == 1) { IntNativeMethods.TEXTMETRICA textmetrica = new IntNativeMethods.TEXTMETRICA(); int textMetricsA = GetTextMetricsA(hDC, ref textmetrica); lptm.tmHeight = textmetrica.tmHeight; lptm.tmAscent = textmetrica.tmAscent; lptm.tmDescent = textmetrica.tmDescent; lptm.tmInternalLeading = textmetrica.tmInternalLeading; lptm.tmExternalLeading = textmetrica.tmExternalLeading; lptm.tmAveCharWidth = textmetrica.tmAveCharWidth; lptm.tmMaxCharWidth = textmetrica.tmMaxCharWidth; lptm.tmWeight = textmetrica.tmWeight; lptm.tmOverhang = textmetrica.tmOverhang; lptm.tmDigitizedAspectX = textmetrica.tmDigitizedAspectX; lptm.tmDigitizedAspectY = textmetrica.tmDigitizedAspectY; lptm.tmFirstChar = (char) textmetrica.tmFirstChar; lptm.tmLastChar = (char) textmetrica.tmLastChar; lptm.tmDefaultChar = (char) textmetrica.tmDefaultChar; lptm.tmBreakChar = (char) textmetrica.tmBreakChar; lptm.tmItalic = textmetrica.tmItalic; lptm.tmUnderlined = textmetrica.tmUnderlined; lptm.tmStruckOut = textmetrica.tmStruckOut; lptm.tmPitchAndFamily = textmetrica.tmPitchAndFamily; lptm.tmCharSet = textmetrica.tmCharSet; return textMetricsA; } return GetTextMetricsW(hDC, ref lptm); }
public static int DrawTextEx(HandleRef hDC, string text, ref IntNativeMethods.RECT lpRect, int nFormat, [In, Out] IntNativeMethods.DRAWTEXTPARAMS lpDTParams) { int retVal; if( Marshal.SystemDefaultCharSize == 1) { // CapRectangleForWin9x(ref lpRect); // we have to do this because if we pass too big of a value (<= 2^31) to win9x // it fails and returns negative values as the rect in which it painted the text lpRect.top = Math.Min(Int16.MaxValue, lpRect.top); lpRect.left = Math.Min(Int16.MaxValue, lpRect.left); lpRect.right = Math.Min(Int16.MaxValue, lpRect.right); lpRect.bottom = Math.Min(Int16.MaxValue, lpRect.bottom); // Convert Unicode string to ANSI. int byteCount = IntUnsafeNativeMethods.WideCharToMultiByte(IntNativeMethods.CP_ACP, 0, text, text.Length, null, 0, IntPtr.Zero, IntPtr.Zero); byte[] textBytes = new byte[byteCount]; IntUnsafeNativeMethods.WideCharToMultiByte(IntNativeMethods.CP_ACP, 0, text, text.Length, textBytes, textBytes.Length, IntPtr.Zero, IntPtr.Zero); // Security: Windows 95/98/Me: This value may not exceed 8192. byteCount = Math.Min( byteCount, IntNativeMethods.MaxTextLengthInWin9x); retVal = DrawTextExA( hDC, textBytes, byteCount, ref lpRect, nFormat, lpDTParams ); } else { 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.", unchecked((int)hDC.Handle), text, lpRect, nFormat ); return retVal; }
/// <devdoc> /// <para> /// The GDI DrawText does not do multiline alignment when IntTextFormatFlags.SingleLine is not set. This /// adjustment is to workaround that limitation. We don't want to duplicate SelectObject calls here, /// so put your Font in the dc before calling this. /// /// AdjustForVerticalAlignment is only used when the text is multiline and it fits inside the bounds passed in. /// In that case we want the horizontal center of the multiline text to be at the horizontal center of the bounds. /// /// If the text is multiline and it does not fit inside the bounds passed in, then return the bounds that were passed in. /// This way we paint the top of the text at the top of the bounds passed in. /// </para> /// </devdoc> public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { Debug.Assert( ((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!" ); // Ok if any Top (Cannot test IntTextFormatFlags.Top because it is 0), single line text or measuring text. bool isTop = (flags & IntTextFormatFlags.Bottom) == 0 && (flags & IntTextFormatFlags.VerticalCenter) == 0; if( isTop ||((flags & IntTextFormatFlags.SingleLine) != 0) || ((flags & IntTextFormatFlags.CalculateRectangle) != 0) ) { return bounds; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); // Get the text bounds. flags |= IntTextFormatFlags.CalculateRectangle; int textHeight = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int) flags, dtparams); // if the text does not fit inside the bounds then return the bounds that were passed in if (textHeight > bounds.Height) { return bounds; } Rectangle adjustedBounds = bounds; if( (flags & IntTextFormatFlags.VerticalCenter) != 0 ) // Middle { adjustedBounds.Y = adjustedBounds.Top + adjustedBounds.Height / 2 - textHeight / 2; } else // Bottom. { adjustedBounds.Y = adjustedBounds.Bottom - textHeight; } return adjustedBounds; }
public static extern bool IntMoveToEx(HandleRef hdc, int x, int y, IntNativeMethods.POINT pt);
public static _DeviceContext FromCompatibleDC(IntPtr hdc) // = IntPtr.Zero) { IntPtr compatibleDc = IntNativeMethods.CreateCompatibleDC(new HandleRef(null, hdc)); return(new _DeviceContext(compatibleDc, DeviceContextType.Memory)); }
public static int GetTextMetrics(HandleRef hDC, ref IntNativeMethods.TEXTMETRIC lptm) { int retVal; if (Marshal.SystemDefaultCharSize == 1) { // ANSI IntNativeMethods.TEXTMETRICA lptmA = new IntNativeMethods.TEXTMETRICA(); retVal = IntUnsafeNativeMethods.GetTextMetricsA(hDC, ref lptmA); lptm.tmHeight = lptmA.tmHeight; lptm.tmAscent = lptmA.tmAscent; lptm.tmDescent = lptmA.tmDescent; lptm.tmInternalLeading = lptmA.tmInternalLeading; lptm.tmExternalLeading = lptmA.tmExternalLeading; lptm.tmAveCharWidth = lptmA.tmAveCharWidth; lptm.tmMaxCharWidth = lptmA.tmMaxCharWidth; lptm.tmWeight = lptmA.tmWeight; lptm.tmOverhang = lptmA.tmOverhang; lptm.tmDigitizedAspectX = lptmA.tmDigitizedAspectX; lptm.tmDigitizedAspectY = lptmA.tmDigitizedAspectY; lptm.tmFirstChar = (char) lptmA.tmFirstChar; lptm.tmLastChar = (char) lptmA.tmLastChar; lptm.tmDefaultChar = (char) lptmA.tmDefaultChar; lptm.tmBreakChar = (char) lptmA.tmBreakChar; lptm.tmItalic = lptmA.tmItalic; lptm.tmUnderlined = lptmA.tmUnderlined; lptm.tmStruckOut = lptmA.tmStruckOut; lptm.tmPitchAndFamily = lptmA.tmPitchAndFamily; lptm.tmCharSet = lptmA.tmCharSet; } else { // Unicode retVal = IntUnsafeNativeMethods.GetTextMetricsW(hDC, ref lptm); } DbgUtil.AssertWin32(retVal != 0, "GetTextMetrics(hdc=[0x{0:X8}], [out TEXTMETRIC] failed.", unchecked((int)hDC.Handle) ); return retVal; }
public void Dispose() { IntNativeMethods.DeleteDC(new HandleRef(this, this.hDC)); }