/// <summary>return the size of the string according to the font</summary> /// <param name="font"></param> /// <param name="str"></param> /// <param name="control"></param> /// <returns></returns> public static Size GetTextExt(Font font, String str, Control control) { Size size = new Size(); if (control != null && str != null) { #if PocketPC Form form = (Form)((control is Form)? control : control.TopLevelControl); using (Graphics g = form.CreateGraphics()) { // Mobile does not support GetTextExtentPoint32 or similar functions SizeF sizef = g.MeasureString(str, font); size.Width = (int)Math.Ceiling(sizef.Width); size.Height = (int)Math.Ceiling(sizef.Height); } #else using (Graphics g = control.CreateGraphics()) { IntPtr hdc = g.GetHdc(); IntPtr hFont = FontHandlesCache.GetInstance().Get(font).FontHandle; IntPtr hFontOld = (IntPtr)NativeWindowCommon.SelectObject(hdc, hFont); NativeWindowCommon.SIZE nativeSize = new NativeWindowCommon.SIZE(); NativeWindowCommon.GetTextExtentPoint32(hdc, str, str.Length, out nativeSize); size.Width = nativeSize.cx; //(int)Math.Ceiling(size.cx); size.Height = nativeSize.cy; //(int)Math.Ceiling(size.cy); NativeWindowCommon.SelectObject(hdc, hFontOld); g.ReleaseHdc(hdc); } #endif } return(size); }
/// <summary> /// Get font text metrics points. /// </summary> /// <param name="hFont"></param> /// <param name="str"></param> /// <param name="control"></param> /// <returns></returns> public static Size GetTextExt(IntPtr hFont, string str, Control control) { Size size = new Size(); using (Graphics g = control.CreateGraphics()) { IntPtr hdc = g.GetHdc(); IntPtr hFontOld = (IntPtr)NativeWindowCommon.SelectObject(hdc, hFont); NativeWindowCommon.SIZE nativeSize = new NativeWindowCommon.SIZE(); NativeWindowCommon.GetTextExtentPoint32(hdc, str, str.Length, out nativeSize); size.Width = nativeSize.cx; size.Height = nativeSize.cy; NativeWindowCommon.SelectObject(hdc, hFontOld); g.ReleaseHdc(hdc); } return(size); }
/// <summary> /// code from gui_dc_calc_start_point /// </summary> /// <param name="hdc"></param> /// <param name="rectangle"></param> /// <param name="orientation"></param> /// <param name="str"></param> /// <param name="contentAlignment"></param> /// <returns></returns> public static Point CalculateStartCoordinates(IntPtr hdc, Rectangle rectangle, long orientation, string str, ContentAlignment contentAlignment) { int txt; Point Start = new Point(); HorizontalAlignment horizontalAlignment = Utils.ContentAlignment2HorizontalAlignment(contentAlignment); VerticalAlignment verticalAlignment = Utils.ContentAlignment2VerticalAlignment(contentAlignment); NativeWindowCommon.SIZE size = new NativeWindowCommon.SIZE(); NativeWindowCommon.GetTextExtentPoint32(hdc, str, str.Length, out size); switch (orientation) { case 450: txt = (int)Math.Sqrt((double)size.cx * size.cx / 2); if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom - size.cy / 2; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top - size.cy / 2 + txt) / 2); } else { Start.Y = rectangle.Top + txt; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right - txt - size.cy / 2; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left - size.cy / 2 - txt) / 2); } else { Start.X = rectangle.Left; } break; case 900: if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top + size.cx) / 2); } else { Start.Y = rectangle.Top + size.cx; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right - size.cy; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left - size.cy) / 2); } else { Start.X = rectangle.Left; } break; case 1350: txt = (int)Math.Sqrt((double)size.cx * size.cx / 2); if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top + size.cy / 2 + txt) / 2); } else { Start.Y = rectangle.Top + txt + size.cy / 2; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right - size.cy / 2; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left - size.cy / 2 + txt) / 2); } else { Start.X = rectangle.Left + txt; } break; case 1800: if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top + size.cy) / 2); } else { Start.Y = rectangle.Top + size.cy; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left + size.cx) / 2); } else { Start.X = rectangle.Left + size.cx; } break; case 2250: txt = (int)Math.Sqrt((double)size.cx * size.cx / 2); if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom - txt; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top + size.cy / 2 - txt) / 2); } else { Start.Y = rectangle.Top + size.cy / 2; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left + size.cy / 2 + txt) / 2); } else { Start.X = rectangle.Left + txt + size.cy / 2; } break; case 2700: if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom - size.cx; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top - size.cx) / 2); } else { Start.Y = rectangle.Top; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left + size.cy) / 2); } else { Start.X = rectangle.Left + size.cy; } break; case 3150: txt = (int)Math.Sqrt((double)size.cx * size.cx / 2); if (verticalAlignment == VerticalAlignment.Bottom) { Start.Y = rectangle.Bottom - txt - size.cy / 2; } else if (verticalAlignment == VerticalAlignment.Center) { Start.Y = rectangle.Top + ((rectangle.Bottom - rectangle.Top - size.cy / 2 - txt) / 2); } else { Start.Y = rectangle.Top; } if (horizontalAlignment == HorizontalAlignment.Right) { Start.X = rectangle.Right - txt; } else if (horizontalAlignment == HorizontalAlignment.Center) { Start.X = rectangle.Left + ((rectangle.Right - rectangle.Left + size.cy / 2 - txt) / 2); } else { Start.X = rectangle.Left + size.cy / 2; } break; default: break; } return(Start); }