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); }
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 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); }
private void DrawTreeItem(string itemText, int imageIndex, IntPtr dc, System.Windows.Forms.NativeMethods.RECT rcIn, int state, int backColor, int textColor) { IntNativeMethods.SIZE size = new IntNativeMethods.SIZE(); IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(); IntNativeMethods.RECT rect = new IntNativeMethods.RECT(rcIn.left, rcIn.top, rcIn.right, rcIn.bottom); ImageList imageList = base.ImageList; IntPtr zero = IntPtr.Zero; if ((state & 2) != 0) { zero = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(base.Parent, base.Parent.FontHandle)); } if (((state & 1) != 0) && (this.hbrushDither != IntPtr.Zero)) { this.FillRectDither(dc, rcIn); System.Windows.Forms.SafeNativeMethods.SetBkMode(new HandleRef(null, dc), 1); } else { System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), backColor); IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 6, ref rect, null, 0, null); } IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(null, dc), itemText, size); lpRect.left = (rect.left + 0x10) + 8; lpRect.top = rect.top + (((rect.bottom - rect.top) - size.cy) >> 1); lpRect.bottom = lpRect.top + size.cy; lpRect.right = rect.right; System.Windows.Forms.SafeNativeMethods.SetTextColor(new HandleRef(null, dc), textColor); IntUnsafeNativeMethods.DrawText(new HandleRef(null, dc), itemText, ref lpRect, 0x8804); System.Windows.Forms.SafeNativeMethods.ImageList_Draw(new HandleRef(imageList, imageList.Handle), imageIndex, new HandleRef(null, dc), 4, rect.top + (((rect.bottom - rect.top) - 0x10) >> 1), 1); if ((state & 2) != 0) { int clr = System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlLightLight)); lpRect.left = rect.left; lpRect.top = rect.top; lpRect.bottom = rect.top + 1; lpRect.right = rect.right; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); lpRect.bottom = rect.bottom; lpRect.right = rect.left + 1; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlDark)); lpRect.left = rect.left; lpRect.right = rect.right; lpRect.top = rect.bottom - 1; lpRect.bottom = rect.bottom; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); lpRect.left = rect.right - 1; lpRect.top = rect.top; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), clr); } if (zero != IntPtr.Zero) { System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(null, zero)); } }
/// <devdoc> /// A rectangle representing the window region set with the SetWindowRgn function. /// </devdoc> public Rectangle ToRectangle() { if (this.IsInfinite) { return(new Rectangle(-int.MaxValue, -int.MaxValue, int.MaxValue, int.MaxValue)); } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(); IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref rect); return(new Rectangle(new Point(rect.left, rect.top), rect.Size)); }
public void FillRectangle(WindowsBrush brush, int x, int y, int width, int height) { Debug.Assert(brush != null, "brush == null"); HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); IntPtr hBrush = brush.HBrush; // We don't delete this handle since we didn't create it. IntNativeMethods.RECT rect = new IntNativeMethods.RECT(x, y, x + width, y + height); #if WINFORMS_PUBLIC_GRAPHICS_LIBRARY if (brush is WindowsHatchBrush) { int clr = ColorTranslator.ToWin32(((WindowsHatchBrush)brush).BackGroundColor); IntUnsafeNativeMethods.SetBkColor(hdc, clr); IntUnsafeNativeMethods.SetBkMode(hdc, (int)DeviceContextBackgroundMode.Transparent); } #endif IntUnsafeNativeMethods.FillRect(hdc, ref rect, new HandleRef(brush, hBrush)); }
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; }
/// <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 IntNativeMethods.RegionFlags GetRgnBox(HandleRef hRgn, [In, Out] ref IntNativeMethods.RECT clipRect) { IntNativeMethods.RegionFlags result = IntGetRgnBox(hRgn, ref clipRect); DbgUtil.AssertWin32(result != IntNativeMethods.RegionFlags.ERROR, "GetRgnBox([hRegion=0x{0:X8}], [out rect]) failed.", hRgn.Handle); return(result); }
public static extern IntNativeMethods.RegionFlags IntGetRgnBox(HandleRef hRgn, [In, Out] ref IntNativeMethods.RECT clipRect);
/// <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 int DrawTextExA(HandleRef hDC, byte[] lpszString, int byteCount, ref IntNativeMethods.RECT lpRect, int nFormat, [In, Out] IntNativeMethods.DRAWTEXTPARAMS lpDTParams);
internal static extern bool ExtTextOut(HandleRef hdc, int x, int y, int options, ref IntNativeMethods.RECT rect, string str, int length, int[] spacing);
public void FillRectangle(WindowsBrush brush, int x, int y, int width, int height) { HandleRef hDC = new HandleRef(this.dc, this.dc.Hdc); IntPtr hBrush = brush.HBrush; IntNativeMethods.RECT rect = new IntNativeMethods.RECT(x, y, x + width, y + height); IntUnsafeNativeMethods.FillRect(hDC, ref rect, new HandleRef(brush, hBrush)); }
private void DrawTreeItem(string itemText, int imageIndex, IntPtr dc, System.Windows.Forms.NativeMethods.RECT rcIn, int state, int backColor, int textColor) { IntNativeMethods.SIZE size = new IntNativeMethods.SIZE(); IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(); IntNativeMethods.RECT rect = new IntNativeMethods.RECT(rcIn.left, rcIn.top, rcIn.right, rcIn.bottom); ImageList imageList = base.ImageList; IntPtr zero = IntPtr.Zero; if ((state & 2) != 0) { zero = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(base.Parent, base.Parent.FontHandle)); } if (((state & 1) != 0) && (this.hbrushDither != IntPtr.Zero)) { this.FillRectDither(dc, rcIn); System.Windows.Forms.SafeNativeMethods.SetBkMode(new HandleRef(null, dc), 1); } else { System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), backColor); IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 6, ref rect, null, 0, null); } IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(null, dc), itemText, size); lpRect.left = (rect.left + 0x10) + 8; lpRect.top = rect.top + (((rect.bottom - rect.top) - size.cy) >> 1); lpRect.bottom = lpRect.top + size.cy; lpRect.right = rect.right; System.Windows.Forms.SafeNativeMethods.SetTextColor(new HandleRef(null, dc), textColor); IntUnsafeNativeMethods.DrawText(new HandleRef(null, dc), itemText, ref lpRect, 0x8804); System.Windows.Forms.SafeNativeMethods.ImageList_Draw(new HandleRef(imageList, imageList.Handle), imageIndex, new HandleRef(null, dc), 4, rect.top + (((rect.bottom - rect.top) - 0x10) >> 1), 1); if ((state & 2) != 0) { int clr = System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlLightLight)); lpRect.left = rect.left; lpRect.top = rect.top; lpRect.bottom = rect.top + 1; lpRect.right = rect.right; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); lpRect.bottom = rect.bottom; lpRect.right = rect.left + 1; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlDark)); lpRect.left = rect.left; lpRect.right = rect.right; lpRect.top = rect.bottom - 1; lpRect.bottom = rect.bottom; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); lpRect.left = rect.right - 1; lpRect.top = rect.top; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, 2, ref lpRect, null, 0, null); System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, dc), clr); } if (zero != IntPtr.Zero) { System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(null, zero)); } }
public Rectangle ToRectangle() { if (this.IsInfinite) { return new Rectangle(-2147483647, -2147483647, 0x7fffffff, 0x7fffffff); } IntNativeMethods.RECT clipRect = new IntNativeMethods.RECT(); IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref clipRect); return new Rectangle(new Point(clipRect.left, clipRect.top), clipRect.Size); }
private void DrawTreeItem(string itemText, int imageIndex, IntPtr dc, NativeMethods.RECT rcIn, int state, int backColor, int textColor) { IntNativeMethods.SIZE size = new IntNativeMethods.SIZE(); IntNativeMethods.RECT rc2 = new IntNativeMethods.RECT(); IntNativeMethods.RECT rc = new IntNativeMethods.RECT(rcIn.left, rcIn.top, rcIn.right, rcIn.bottom); ImageList imagelist = this.ImageList; IntPtr hfontOld = IntPtr.Zero; // Select the font of the dialog, so we don't get the underlined font // when the item is being tracked if ((state & STATE_HOT) != 0) hfontOld = SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(Parent, ((Control)Parent).FontHandle)); // Fill the background if (((state & STATE_SELECTED) != 0) && (hbrushDither != IntPtr.Zero)) { FillRectDither(dc, rcIn); SafeNativeMethods.SetBkMode(new HandleRef(null, dc), NativeMethods.TRANSPARENT); } else { SafeNativeMethods.SetBkColor(new HandleRef(null, dc), backColor); IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_CLIPPED | NativeMethods.ETO_OPAQUE, ref rc, null, 0, null); } // Get the height of the font IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(null, dc), itemText, size); // Draw the caption rc2.left = rc.left + SIZE_ICON_X + 2 * PADDING_HORZ; rc2.top = rc.top + (((rc.bottom - rc.top) - size.cy) >> 1); rc2.bottom = rc2.top + size.cy; rc2.right = rc.right; SafeNativeMethods.SetTextColor(new HandleRef(null, dc), textColor); IntUnsafeNativeMethods.DrawText(new HandleRef(null, dc), itemText, ref rc2, IntNativeMethods.DT_LEFT | IntNativeMethods.DT_VCENTER | IntNativeMethods.DT_END_ELLIPSIS | IntNativeMethods.DT_NOPREFIX); SafeNativeMethods.ImageList_Draw(new HandleRef(imagelist, imagelist.Handle), imageIndex, new HandleRef(null, dc), PADDING_HORZ, rc.top + (((rc.bottom - rc.top) - SIZE_ICON_Y) >> 1), NativeMethods.ILD_TRANSPARENT); // Draw the hot-tracking border if needed if ((state & STATE_HOT) != 0) { int savedColor; // top left savedColor = SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlLightLight)); rc2.left = rc.left; rc2.top = rc.top; rc2.bottom = rc.top + 1; rc2.right = rc.right; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); rc2.bottom = rc.bottom; rc2.right = rc.left + 1; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); // bottom right SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlDark)); rc2.left = rc.left; rc2.right = rc.right; rc2.top = rc.bottom - 1; rc2.bottom = rc.bottom; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); rc2.left = rc.right - 1; rc2.top = rc.top; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); SafeNativeMethods.SetBkColor(new HandleRef(null, dc), savedColor); } if (hfontOld != IntPtr.Zero) SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(null, hfontOld)); }
/// <include file='doc\WindowsGraphics.uex' path='docs/doc[@for="WindowsGraphics.FillRectangle3"]/*' /> public void FillRectangle(WindowsBrush brush, int x, int y, int width, int height) { Debug.Assert( brush != null, "brush == null" ); HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); IntPtr hBrush = brush.HBrush; // We don't delete this handle since we didn't create it. IntNativeMethods.RECT rect = new IntNativeMethods.RECT(x, y, x + width, y + height ); #if WINFORMS_PUBLIC_GRAPHICS_LIBRARY if (brush is WindowsHatchBrush) { int clr = ColorTranslator.ToWin32(((WindowsHatchBrush)brush).BackGroundColor); IntUnsafeNativeMethods.SetBkColor(hdc, clr ); IntUnsafeNativeMethods.SetBkMode(hdc, (int)DeviceContextBackgroundMode.Transparent); } #endif IntUnsafeNativeMethods.FillRect(hdc, ref rect, new HandleRef(brush, hBrush)); }
public static extern int DrawTextW(HandleRef hDC, string lpszString, int nCount, ref IntNativeMethods.RECT lpRect, int nFormat);
public static extern int DrawTextA(HandleRef hDC, byte[] lpszString, int byteCount, ref IntNativeMethods.RECT lpRect, int nFormat);
public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (!string.IsNullOrEmpty(text) && (foreColor != Color.Transparent)) { HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); if (this.dc.TextAlignment != DeviceContextTextAlignment.Top) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Top); } if (!foreColor.IsEmpty && (foreColor != this.dc.TextColor)) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newMode = (backColor.IsEmpty || (backColor == Color.Transparent)) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newMode) { this.dc.SetBackgroundMode(newMode); } if ((newMode != DeviceContextBackgroundMode.Transparent) && (backColor != this.dc.BackgroundColor)) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS textMargins = this.GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, textMargins); if (bounds.Width == MaxSize.Width) { bounds.Width -= bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height -= bounds.Y; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int) flags, textMargins); } }
/// <devdoc> /// A rectangle representing the window region set with the SetWindowRgn function. /// </devdoc> public Rectangle ToRectangle() { if( this.IsInfinite ) { return new Rectangle( -Int32.MaxValue, -Int32.MaxValue, Int32.MaxValue, Int32.MaxValue ); } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(); IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref rect); return new Rectangle(new Point(rect.left, rect.top), rect.Size); }
/// <devdoc> /// Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified /// TextFormatFlags flags. /// If font is null, the font currently selected in the hdc is used. /// If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent) { return; } Debug.Assert( ((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!" ); Debug.Assert( (flags & IntTextFormatFlags.CalculateRectangle) == 0, "CalculateRectangle flag is set, text won't be drawn" ); HandleRef hdc = new HandleRef( this.dc, this.dc.Hdc); // DrawText requires default text alignment. if( this.dc.TextAlignment != DeviceContextTextAlignment.Default ) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Default ); } // color empty means use the one currently selected in the dc. if( !foreColor.IsEmpty && foreColor != this.dc.TextColor) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if( this.dc.BackgroundMode != newBackGndMode ) { this.dc.SetBackgroundMode( newBackGndMode ); } if( newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != this.dc.BackgroundColor ) { this.dc.SetBackgroundColor( backColor ); } IntNativeMethods.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if( bounds.Width == MaxSize.Width ) { bounds.Width = bounds.Width - bounds.X; } if( bounds.Height == MaxSize.Height ) { bounds.Height = bounds.Height - bounds.Y; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int) flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * if (hOldFont != IntPtr.Zero) { IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef( null, hOldFont)); } if( foreColor != textColor ) { this.dc.SetTextColor(textColor); } if( backColor != bkColor ) { this.dc.SetBackgroundColor(bkColor); } if( bckMode != newMode ) { this.dc.SetBackgroundMode(bckMode); } if( align != DeviceContextTextAlignment.Default ) { // Default text alignment required by DrewText. this.dc.SetTextAlignment(align); } */ }
public static extern bool IntFillRect(HandleRef hdc, [In] ref IntNativeMethods.RECT rect, HandleRef hbrush);
/// <devdoc> /// Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified /// TextFormatFlags flags. /// If font is null, the font currently selected in the hdc is used. /// If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent) { return; } Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); Debug.Assert((flags & IntTextFormatFlags.CalculateRectangle) == 0, "CalculateRectangle flag is set, text won't be drawn"); HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); // DrawText requires default text alignment. if (this.dc.TextAlignment != DeviceContextTextAlignment.Default) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Default); } // color empty means use the one currently selected in the dc. if (!foreColor.IsEmpty && foreColor != this.dc.TextColor) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newBackGndMode) { this.dc.SetBackgroundMode(newBackGndMode); } if (newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != this.dc.BackgroundColor) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if (bounds.Width == MaxSize.Width) { bounds.Width = bounds.Width - bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height = bounds.Height - bounds.Y; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * * if (hOldFont != IntPtr.Zero) * { * IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef( null, hOldFont)); * } * * if( foreColor != textColor ) * { * this.dc.SetTextColor(textColor); * } * * if( backColor != bkColor ) * { * this.dc.SetBackgroundColor(bkColor); * } * * if( bckMode != newMode ) * { * this.dc.SetBackgroundMode(bckMode); * } * * if( align != DeviceContextTextAlignment.Default ) * { * // Default text alignment required by DrewText. * this.dc.SetTextAlignment(align); * } */ }
private void DrawTreeItem(string itemText, int imageIndex, IntPtr dc, NativeMethods.RECT rcIn, int state, int backColor, int textColor) { IntNativeMethods.SIZE size = new IntNativeMethods.SIZE(); IntNativeMethods.RECT rc2 = new IntNativeMethods.RECT(); IntNativeMethods.RECT rc = new IntNativeMethods.RECT(rcIn.left, rcIn.top, rcIn.right, rcIn.bottom); ImageList imagelist = this.ImageList; IntPtr hfontOld = IntPtr.Zero; // Select the font of the dialog, so we don't get the underlined font // when the item is being tracked if ((state & STATE_HOT) != 0) { hfontOld = SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(Parent, ((Control)Parent).FontHandle)); } // Fill the background if (((state & STATE_SELECTED) != 0) && (hbrushDither != IntPtr.Zero)) { FillRectDither(dc, rcIn); SafeNativeMethods.SetBkMode(new HandleRef(null, dc), NativeMethods.TRANSPARENT); } else { SafeNativeMethods.SetBkColor(new HandleRef(null, dc), backColor); IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_CLIPPED | NativeMethods.ETO_OPAQUE, ref rc, null, 0, null); } // Get the height of the font IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(null, dc), itemText, size); // Draw the caption rc2.left = rc.left + SIZE_ICON_X + 2 * PADDING_HORZ; rc2.top = rc.top + (((rc.bottom - rc.top) - size.cy) >> 1); rc2.bottom = rc2.top + size.cy; rc2.right = rc.right; SafeNativeMethods.SetTextColor(new HandleRef(null, dc), textColor); IntUnsafeNativeMethods.DrawText(new HandleRef(null, dc), itemText, ref rc2, IntNativeMethods.DT_LEFT | IntNativeMethods.DT_VCENTER | IntNativeMethods.DT_END_ELLIPSIS | IntNativeMethods.DT_NOPREFIX); SafeNativeMethods.ImageList_Draw(new HandleRef(imagelist, imagelist.Handle), imageIndex, new HandleRef(null, dc), PADDING_HORZ, rc.top + (((rc.bottom - rc.top) - SIZE_ICON_Y) >> 1), NativeMethods.ILD_TRANSPARENT); // Draw the hot-tracking border if needed if ((state & STATE_HOT) != 0) { int savedColor; // top left savedColor = SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlLightLight)); rc2.left = rc.left; rc2.top = rc.top; rc2.bottom = rc.top + 1; rc2.right = rc.right; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); rc2.bottom = rc.bottom; rc2.right = rc.left + 1; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); // bottom right SafeNativeMethods.SetBkColor(new HandleRef(null, dc), ColorTranslator.ToWin32(SystemColors.ControlDark)); rc2.left = rc.left; rc2.right = rc.right; rc2.top = rc.bottom - 1; rc2.bottom = rc.bottom; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); rc2.left = rc.right - 1; rc2.top = rc.top; IntUnsafeNativeMethods.ExtTextOut(new HandleRef(null, dc), 0, 0, NativeMethods.ETO_OPAQUE, ref rc2, null, 0, null); SafeNativeMethods.SetBkColor(new HandleRef(null, dc), savedColor); } if (hfontOld != IntPtr.Zero) { SafeNativeMethods.SelectObject(new HandleRef(null, dc), new HandleRef(null, hfontOld)); } }
/// <devdoc> /// Returns the Size in logical units of the given text using the given Font, and according to the formatting flags. /// The proposed size is used to create a bounding rectangle as follows: /// - If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by /// the lpRect parameter and extends the base of the rectangle to bound the last line of text. /// - If the largest word is wider than the rectangle, the width is expanded. /// - If the text is less than the width of the rectangle, the width is reduced. /// - If there is only one line of text, DrawText modifies the right side of the rectangle so that /// it bounds the last character in the line. /// If the font is null, the hdc's current font will be used. /// /// Note for vertical fonts (if ever supported): DrawTextEx uses GetTextExtentPoint32 for measuring the text and this /// function has the following limitation (from MSDN): /// - This function assumes that the text is horizontal, that is, that the escapement is always 0. This is true for both /// the horizontal and vertical measurements of the text. The application must convert it explicitly. /// </devdoc> public Size MeasureText(string text, WindowsFont font, Size proposedSize, IntTextFormatFlags flags) { Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); if (string.IsNullOrEmpty(text)) { return(Size.Empty); } // // DrawText returns a rectangle useful for aligning, but not guaranteed to encompass all // pixels (its not a FitBlackBox, if the text is italicized, it will overhang on the right.) // So we need to account for this. // IntNativeMethods.DRAWTEXTPARAMS dtparams = null; #if OPTIMIZED_MEASUREMENTDC // use the cache if we've got it if (MeasurementDCInfo.IsMeasurementDC(this.DeviceContext)) { dtparams = MeasurementDCInfo.GetTextMargins(this, font); } #endif if (dtparams == null) { dtparams = GetTextMargins(font); } // // If Width / Height are < 0, we need to make them larger or DrawText will return // an unbounded measurement when we actually trying to make it very narrow. // int minWidth = 1 + dtparams.iLeftMargin + dtparams.iRightMargin; if (proposedSize.Width <= minWidth) { proposedSize.Width = minWidth; } if (proposedSize.Height <= 0) { proposedSize.Height = 1; } IntNativeMethods.RECT rect = IntNativeMethods.RECT.FromXYWH(0, 0, proposedSize.Width, proposedSize.Height); HandleRef hdc = new HandleRef(null, this.dc.Hdc); if (font != null) { this.dc.SelectFont(font); } // If proposedSize.Height >= MaxSize.Height it is assumed bounds needed. If flags contain SingleLine and // VerticalCenter or Bottom options, DrawTextEx does not bind the rectangle to the actual text height since // it assumes the text is to be vertically aligned; we need to clear the VerticalCenter and Bottom flags to // get the actual text bounds. if (proposedSize.Height >= MaxSize.Height && (flags & IntTextFormatFlags.SingleLine) != 0) { // Clear vertical-alignment flags. flags &= ~(IntTextFormatFlags.Bottom | IntTextFormatFlags.VerticalCenter); } if (proposedSize.Width == MaxSize.Width) { // PERF: No constraining width means no word break. // in this case, we dont care about word wrapping - there should be enough room to fit it all flags &= ~(IntTextFormatFlags.WordBreak); } flags |= IntTextFormatFlags.CalculateRectangle; IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * * if( hOldFont != IntPtr.Zero ) * { * this.dc.SelectObject(hOldFont); * } */ return(rect.Size); }