/// <summary> /// Draw the text. /// </summary> /// <param name="hdc"></param> /// <param name="color"></param> /// <param name="rect"></param> /// <param name="topOffset"></param> /// <param name="orientation"></param> /// <param name="fontDescription"></param> /// <param name="text"></param> /// <param name="contentAlignment"></param> /// <param name="flags"></param> public static void DrawText(IntPtr hdc, Color color, Rectangle rect, int topOffset, int orientation, FontDescription fontDescription, String text, ContentAlignment contentAlignment, int flags, bool rightToLeft) { NativeWindowCommon.SetBkMode(hdc, NativeWindowCommon.TRANSPARENT); NativeWindowCommon.SetTextColor(hdc, ColorTranslator.ToWin32(color)); #if !PocketPC // QCR #439182 & 430913: the font is used for a control before even being initialized. // TODO: We need a better fix that avoids calling get_Font() for logical controls that are not initialized properly. if (fontDescription == null) { fontDescription = new FontDescription(Control.DefaultFont); } #endif NativeWindowCommon.RECT rc = new NativeWindowCommon.RECT(); rc.left = rect.Left; rc.right = rect.Right; rc.top = rect.Top + topOffset; rc.bottom = rect.Bottom; #if !PocketPC if (orientation != 0) { PrintRotatedText(hdc, fontDescription, orientation, text, rect, contentAlignment, rc, rightToLeft); } else #endif { IntPtr hFont = fontDescription.FontHandle; NativeWindowCommon.SelectObject(hdc, hFont); NativeWindowCommon.DrawText(hdc, text, text.Length, ref rc, flags); } }
/// <summary> /// calc text rect /// use ContentAlignment.MiddleRight for the calculation because if we use ContentAlignment.MiddleCenter /// then GuiUtils.CalcTextRect() returns wrong results. The problem is that the height returned by GuiUtils.CalcTextRect() /// will be of a single line always. /// /// </summary> /// <param name="g"></param> /// <param name="rect"></param> /// <param name="font"></param> /// <param name="text"></param> /// <param name="flags"></param> /// <param name="retCalcTextRect"></param> public static void CalcTextRect(Graphics g, Rectangle rect, FontDescription font, String text, int flags, out NativeWindowCommon.RECT retCalcTextRect) { IntPtr hdc1; IntPtr hFont1; hdc1 = g.GetHdc(); hFont1 = font.FontHandle; NativeWindowCommon.SelectObject(hdc1, hFont1); retCalcTextRect = new NativeWindowCommon.RECT(); retCalcTextRect.left = rect.Left; retCalcTextRect.right = rect.Right; retCalcTextRect.top = rect.Top; retCalcTextRect.bottom = rect.Bottom; int calcRectFlags = (int)(TextFormatFlags)flags | 0x400; // TextFormatFlags.CalcRect == 0x400 == DT_CALCRECT NativeWindowCommon.DrawText(hdc1, text, text.Length, ref retCalcTextRect, calcRectFlags); g.ReleaseHdc(hdc1); }
/// <summary> /// draw header section title /// </summary> /// <param name="hdc"></param> /// <param name="rc"></param> internal void DrawTitle(IntPtr hdc, NativeWindowCommon.RECT rc, int index, bool supportsMultilineText, bool addEndEllipsesFlag, bool rightToLeftLayout) { Header.HeaderRenderer.GetTitleTextRect(index, ref rc); using (Graphics gr = Graphics.FromHdc(hdc)) { //draw sort icon if (SortMark != HeaderSectionSortMarks.Non && !HasFilter) { int iconWindth = SortIconWidth(); DrawSortMark(hdc, rc, gr); rc.right -= iconWindth + Header.SORT_ICON_LEFT_RIGHT_MARGIN; } if (HasFilter) { rc.right -= HeaderSection.FILTER_WIDTH + Header.SORT_ICON_LEFT_RIGHT_MARGIN; } //draw ... in the end of text if width is too short int width = rc.right - rc.left; int format = NativeWindowCommon.DT_EDITCONTROL | NativeWindowCommon.DT_EXTERNALLEADING; if (supportsMultilineText) { format |= NativeWindowCommon.DT_WORDBREAK; } StringBuilder stringBuilder = new StringBuilder(Text); if (addEndEllipsesFlag) { //for windows CE && orientated fonts DT_END_ELLIPSIS style is not supported //http://support.microsoft.com/kb/249678 format |= NativeWindowCommon.DT_END_ELLIPSIS; } if (Environment.OSVersion.Platform == PlatformID.WinCE) { if (Text.IndexOf("\n") != -1) { SizeF size = gr.MeasureString(text, Font); int cur, len = cur = text.Length; while (size.Width > width && cur > 1) { cur = --len; stringBuilder.Length = len; while (cur > 1 && len - cur < 3) { stringBuilder[--cur] = '.'; } size = gr.MeasureString(stringBuilder.ToString(), Font); } } } NativeWindowCommon.SetBkMode(hdc, NativeWindowCommon.TRANSPARENT); NativeWindowCommon.SetTextColor(hdc, ColorTranslator.ToWin32(Color)); #if !PocketPC if (FontOrientation != 0) { Rectangle rectangle = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); ControlRenderer.PrintRotatedText(hdc, FontDescription, FontOrientation, stringBuilder.ToString(), rectangle, ContentAlignment, rc, rightToLeftLayout); } else #endif { //text flags are exactly the same as DrawText DT_*** flags format |= (int)Utils.ContentAlignment2TextFlags(ContentAlignment); if (isSingleLine()) { format |= NativeWindowCommon.DT_SINGLELINE; } IntPtr hFont = FontDescription.FontHandle; NativeWindowCommon.SelectObject(hdc, hFont); NativeWindowCommon.DrawText(hdc, stringBuilder.ToString(), stringBuilder.Length, ref rc, format); } #if !PocketPC //Set the rectangle back if (HasFilter) { rc.right += HeaderSection.FILTER_WIDTH + Header.SORT_ICON_LEFT_RIGHT_MARGIN; DrawFilter(hdc, rc); } #endif } }