/// <summary> /// Check if the font has a fixed width /// </summary> /// <param name="font">Font to check</param> /// <returns>true if the font has a fixed width</returns> public static bool IsFixedPitch(Font font) { bool result; IntPtr fnt = font.ToHfont(); using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { IntPtr hdc = g.GetHdc(); // store the current font and set the new one IntPtr fntOld = SelectObject(hdc, fnt); TEXTMETRIC metric = new TEXTMETRIC(); NativeMethods.GetTextMetrics(hdc, ref metric); result = (metric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0; // restore the old font SelectObject(hdc, fntOld); g.ReleaseHdc(hdc); } } DeleteObject(fnt); return(result); }
/// <summary> /// Check if the font has a fixed width /// </summary> /// <param name="font">Font to check</param> /// <returns>true if the font has a fixed width</returns> public static bool IsFixedPitch(Font font) { bool result; IntPtr fnt = font.ToHfont(); using (Bitmap bmp = new Bitmap(1, 1)) { using (Graphics g = Graphics.FromImage(bmp)) { IntPtr hdc = g.GetHdc(); // store the current font and set the new one IntPtr fntOld = SelectObject(hdc, fnt); TEXTMETRIC metric = new TEXTMETRIC(); NativeMethods.GetTextMetrics(hdc, ref metric); result = (metric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0; // restore the old font SelectObject(hdc, fntOld); g.ReleaseHdc(hdc); } } DeleteObject(fnt); return result; }
private TEXTMETRIC GenerateTextMetrics(Graphics graphics, Font font) { var hDC = IntPtr.Zero; TEXTMETRIC textMetric = new TEXTMETRIC(); var hFont = IntPtr.Zero; try { hDC = graphics.GetHdc(); hFont = font.ToHfont(); IntPtr hFontDefault = SelectObject(hDC, hFont); bool result = GetTextMetrics(hDC, ref textMetric); SelectObject(hDC, hFontDefault); } finally { if (hFont != IntPtr.Zero) { DeleteObject(hFont); } if (hDC != IntPtr.Zero) { graphics.ReleaseHdc(hDC); } } return(textMetric); }
public Font(string filename, int size, FontStyle style) { // Debug.Assert(false); string fontname = filename.Replace("/Application/", "./"); this.__size = size; this.__style = style; System.Drawing.FontStyle _style = System.Drawing.FontStyle.Regular; switch (style) { case FontStyle.Regular: _style = System.Drawing.FontStyle.Regular; break; case FontStyle.Bold: _style = System.Drawing.FontStyle.Bold; break; case FontStyle.Italic: _style = System.Drawing.FontStyle.Italic; break; } PrivateFontCollection privateFonts = new PrivateFontCollection(); privateFonts.AddFontFile(fontname); //Arial //Comic Sans MS //MS Gothic this.__font = new System.Drawing.Font(privateFonts.Families[0], (int)(__size * 0.75f), _style); //FIXME:???0.8 //this.__font = new System.Drawing.Font("Arial", __size, _style); //FIXME:???0.8 //this.__font = new System.Drawing.Font(/*"宋体"*/FontFamily.GenericSansSerif, __size, _style); this.__metrics = new FontMetrics(); IntPtr hdc = GetDC(IntPtr.Zero); IntPtr handle = __font.ToHfont(); try { IntPtr handle2 = SelectObject(hdc, handle); TEXTMETRIC tEXTMETRIC = new TEXTMETRIC(); GetTextMetrics(hdc, ref tEXTMETRIC); __metrics.Ascent = tEXTMETRIC.tmAscent; __metrics.Descent = tEXTMETRIC.tmDescent; __metrics.Leading = tEXTMETRIC.tmHeight - tEXTMETRIC.tmAscent - tEXTMETRIC.tmDescent; SelectObject(hdc, handle2); } finally { DeleteObject(handle); ReleaseDC(IntPtr.Zero, hdc); } }
// GetTextMetrics wrapper public static void GetTextMetrics(Graphics graphics, Font font, ref TEXTMETRIC tm) { IntPtr hDC = graphics.GetHdc(); IntPtr hFont = font.ToHfont(); IntPtr oldHFont = SelectObject(hDC, hFont); GetTextMetricsA(hDC, ref tm); SelectObject(hDC, oldHFont); DeleteObject(hFont); graphics.ReleaseHdc(hDC); }
public void Dispose() { HASH = 0; HFONT = (IntPtr)0; GLYPHINFO = null; METRIC = default(TEXTMETRIC); glDeleteLists( LIST, 256); LIST = 0; }
public Font(FontAlias alias, int size, FontStyle style) { this.__alias = alias; this.__size = size; this.__style = style; System.Drawing.FontStyle _style = System.Drawing.FontStyle.Regular; switch (style) { case FontStyle.Regular: _style = System.Drawing.FontStyle.Regular; break; case FontStyle.Bold: _style = System.Drawing.FontStyle.Bold; break; case FontStyle.Italic: _style = System.Drawing.FontStyle.Italic; break; } //Arial //Comic Sans MS //MS Gothic this.__font = new System.Drawing.Font("MS Gothic", (int)(__size * 0.75f), _style); //FIXME:???0.8 //this.__font = new System.Drawing.Font("Arial", __size, _style); //FIXME:???0.8 //this.__font = new System.Drawing.Font(/*"宋体"*/FontFamily.GenericSansSerif, __size, _style); this.__metrics = new FontMetrics(); IntPtr hdc = GetDC(IntPtr.Zero); IntPtr handle = __font.ToHfont(); try { IntPtr handle2 = SelectObject(hdc, handle); TEXTMETRIC tEXTMETRIC = new TEXTMETRIC(); GetTextMetrics(hdc, ref tEXTMETRIC); __metrics.Ascent = tEXTMETRIC.tmAscent; __metrics.Descent = tEXTMETRIC.tmDescent; __metrics.Leading = tEXTMETRIC.tmHeight - tEXTMETRIC.tmAscent - tEXTMETRIC.tmDescent; SelectObject(hdc, handle2); } finally { DeleteObject(handle); ReleaseDC(IntPtr.Zero, hdc); } }
public static int GetTextBaseline(Control ctrl, ContentAlignment alignment) { if (ctrl == null) { throw new ArgumentNullException("ctrl"); } Rectangle ctrlRect = ctrl.ClientRectangle; int ascent = 0; int height = 0; using (Graphics grfx = ctrl.CreateGraphics()) { IntPtr hDC = grfx.GetHdc(); IntPtr hFont = ctrl.Font.ToHfont(); try { IntPtr oldFont = Gdi32.SelectObject(hDC, hFont); TEXTMETRIC textMetric = new TEXTMETRIC(); Gdi32.GetTextMetrics(hDC, textMetric); ascent = textMetric.tmAscent + 1; height = textMetric.tmHeight; Gdi32.SelectObject(hDC, oldFont); } finally { Gdi32.DeleteObject(hFont); grfx.ReleaseHdc(hDC); } } if ((alignment & NuGenControlPaint.AnyTop) != 0) { return(ctrlRect.Top + ascent); } if ((alignment & NuGenControlPaint.AnyMiddle) != 0) { return(((ctrlRect.Top + (ctrlRect.Height / 2)) - (height / 2)) + ascent); } return((ctrlRect.Bottom - height) + ascent); }
public static extern System.Int32 GetTextMetrics(System.Int32 hDC,ref TEXTMETRIC tm);
private FontMetricsImpl(Graphics graphics, Font font) { this.metrics = this.GenerateTextMetrics(graphics, font); }
public void GetTextMetrics(ref TEXTMETRIC ATextMetric) { GDI.GetTextMetrics(FHandle, ref ATextMetric); }
internal static extern bool GetTextMetrics(IntPtr hdc, ref TEXTMETRIC lptm);
public static extern int GetTextMetrics( IntPtr hdc, ref TEXTMETRIC lptm );
[DllImport("gdi32.dll", CharSet = CharSet.Auto)] public static extern bool GetTextMetrics(IntPtr hdc, out TEXTMETRIC lptm);
private static extern bool GetTextMetrics(HandleRef hdc, out TEXTMETRIC tm);
protected override void WndProc(ref Message m) { var message = (Utils.ControlExtensions.WindowsMessage)m.Msg; Graphics graphics; Keys key; System.Drawing.SizeF size; System.Drawing.Point point; RectangleF rect; SolidBrush brush; Brush fontBrush; StringFormat stringFormat; switch (message) { case ControlExtensions.WindowsMessage.CREATE: using (graphics = this.CreateGraphics()) { tm = graphics.GetTextMetrics(this.Font); } nCharX = tm.tmAveCharWidth; nCharY = tm.tmHeight; this.BackColor = SystemColors.Window; builder = new StringBuilder(); break; case ControlExtensions.WindowsMessage.SIZE: var loHigh = m.LParam.ToLowHiWord(); nWindowX = loHigh.Low; nWindowCharsX = Math.Max(1, nWindowX / nCharX); nWindowY = loHigh.High; nWindowCharsY = Math.Max(1, nWindowY / nCharY); if (caret != null) { caret.Position = new System.Drawing.Point(0, 0); } break; case ControlExtensions.WindowsMessage.KEYDOWN: key = (Keys)m.WParam; switch (key) { case Keys.Home: nCaretPosX = 0; break; case Keys.End: nCaretPosX = nWindowCharsX - 1; break; case Keys.PageUp: nCaretPosY = 0; break; case Keys.PageDown: nCaretPosY = nWindowCharsY - 1; break; case Keys.Left: nCaretPosX = Math.Max(nCaretPosX - 1, 0); break; case Keys.Right: nCaretPosX = Math.Min(nCaretPosX + 1, nWindowCharsX - 1); break; case Keys.Up: nCaretPosY = Math.Max(nCaretPosY - 1, 0); break; case Keys.Down: nCaretPosY = Math.Min(nCaretPosY + 1, nWindowCharsY - 1); break; case Keys.Delete: if (builder.Length > 0) { builder.RemoveEnd(1); caret.Hide(); using (graphics = this.CreateGraphics()) { brush = new SolidBrush(this.BackColor); fontBrush = SystemBrushes.WindowText; stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces); size = graphics.MeasureString(this.Text, this.Font, int.MaxValue, stringFormat); rect = new RectangleF(0, 0, size.Width, size.Height); point = new System.Drawing.Point((int)rect.Right, (int)rect.Top); graphics.FillRectangle(brush, this.ClientRectangle); graphics.DrawString(this.Text, this.Font, fontBrush, rect.Location); brush.Dispose(); } caret.Position = point; caret.Show(); } return; } break; case ControlExtensions.WindowsMessage.CHAR: var ch = (char)m.WParam; key = ch.ToKey(); switch (key) { case Keys.Back: this.SendMessage(ControlExtensions.WindowsMessage.KEYDOWN, (int)Keys.Delete, 1L); break; case Keys.Tab: do { this.SendMessage(ControlExtensions.WindowsMessage.CHAR, ' ', 1L); }while (nCaretPosX % 4 != 0); break; case Keys.Return: nCaretPosX = 0; if (++nCaretPosY == nWindowCharsY) { nCaretPosY = 0; } break; case Keys.Escape: case Keys.LineFeed: this.Beep(ControlExtensions.BeepType.OK); break; default: builder.Append(key.ToAscii()); caret.Hide(); using (graphics = this.CreateGraphics()) { brush = new SolidBrush(this.BackColor); fontBrush = SystemBrushes.WindowText; stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces); size = graphics.MeasureString(this.Text, this.Font, int.MaxValue, stringFormat); rect = new RectangleF(0, 0, size.Width, size.Height); point = new System.Drawing.Point((int)rect.Right, (int)rect.Top); graphics.FillRectangle(brush, this.ClientRectangle); graphics.DrawString(this.Text, this.Font, fontBrush, rect.Location); brush.Dispose(); } caret.Position = point; caret.Show(); break; } break; case ControlExtensions.WindowsMessage.PAINT: break; case ControlExtensions.WindowsMessage.SETFOCUS: caret = new Caret(this, 0, this.Height); caret.Position = new System.Drawing.Point(0, 0); caret.Show(); break; case ControlExtensions.WindowsMessage.KILLFOCUS: if (caret != null) { caret.Dispose(); } break; } base.WndProc(ref m); }
private static extern bool GetTextMetrics(IntPtr hdc, ref TEXTMETRIC lptm);
public static bool GetTextMetrics(this DeviceContext deviceContext, out TEXTMETRIC metrics) => GdiMethods.GetTextMetrics(deviceContext, out metrics);
//-------------------------------------------------------- public static void PrintText(TextBox textIn, IntPtr hdc) { // Split input data into separate lines of text. char [] achNewLine = new char[] { '\n' }; String [] astrSplit; astrSplit = textIn.Text.Split(achNewLine); // Calculate longest string in the document int cchMax = 0; int cstr = astrSplit.Length; for (int i = 0; i < cstr; i++) { if (astrSplit[i].Length > cchMax) { cchMax = astrSplit[i].Length; } } // Allocate conversion buffers. byte[] byteData = new Byte[cchMax]; char[] chData = new Char[cchMax]; System.Text.Encoder d; d = System.Text.Encoding.UTF8.GetEncoder(); // Get device resolution int cxyInch = GdiGraphics.GetDeviceCaps(hdc, CAPS.LOGPIXELSY); // In draft mode, the PCL driver returns wrong value. if (cxyInch == 0) { cxyInch = 150; } // Calculate page size. int cxPhysPage = GdiGraphics.GetDeviceCaps(hdc, CAPS.PHYSICALWIDTH); int cyPhysPage = GdiGraphics.GetDeviceCaps(hdc, CAPS.PHYSICALHEIGHT); int cxOff = GdiGraphics.GetDeviceCaps(hdc, CAPS.PHYSICALOFFSETX); int cyOff = GdiGraphics.GetDeviceCaps(hdc, CAPS.PHYSICALOFFSETY); // Calculate line height. TEXTMETRIC tm = new TEXTMETRIC(); GdiFont.GetTextMetrics(hdc, ref tm); int cyLineHeight = tm.tmHeight + tm.tmExternalLeading; // Init text drawing coordinates; int xText = cxyInch - cxOff; int yText = cxyInch - cyOff; // Calculate page boundaries int yFirst = yText; int yLast = cyPhysPage - cxyInch; // Notify GDI of document and page start. DOCINFO di = new DOCINFO(); di.cbSize = Marshal.SizeOf(di); Printing.StartDoc(hdc, ref di); Printing.StartPage(hdc); try { // Set iEnd -- trim extra carriage-return from text int iEnd = 0; int cchString = astrSplit[0].Length; char ch = astrSplit[0][cchString - 1]; if (ch == '\r') { iEnd = -1; } // Loop through list of strings. for (int i = 0; i < cstr; i++) { cchString = astrSplit[i].Length; if (cchString > 0) { // Draw line of text. GdiGraphics.ExtTextOut(hdc, xText, yText, 0, IntPtr.Zero, astrSplit[i], cchString + iEnd, IntPtr.Zero); } // Advance to next line. yText += cyLineHeight; // Skip to next page (if not at document end) if (yText >= yLast && (i + 1) < cstr) { Printing.EndPage(hdc); Printing.StartPage(hdc); yText = yFirst; } } } finally { // End of page & end of document. Printing.EndPage(hdc); Printing.EndDoc(hdc); } } // PrintText()
internal static extern bool GetTextMetrics(IntPtr hdc, out TEXTMETRIC tm);
public static extern bool GetTextMetrics(Win32DCSafeHandle hdc, out TEXTMETRIC tm);
public static extern int GetTextMetricsA(IntPtr hDC, ref TEXTMETRIC tm);
}; //********************************************************************** [DllImport("gdi32.dll")] public extern static int
public static extern bool GetTextMetricsW( DeviceContext hdc, out TEXTMETRIC lptm);
public static extern bool GetTextMetrics(IntPtr dc, out TEXTMETRIC metric);
public static bool GetTextMetrics(DeviceContext deviceContext, out TEXTMETRIC metrics) { return(Imports.GetTextMetricsW(deviceContext, out metrics)); }
public static extern bool GetTextMetrics(HandleRef hdc, TEXTMETRIC tm);
public Bitmap TextRenderHQ(int _size, string _font, string _color, int _offset, string _str, bool _shadow) { privateFonts = new PrivateFontCollection(); privateFonts.AddFontFile(_font); font = new Font(privateFonts.Families[0], _size); String s = _str; String[] rgbs = _color.Split(new Char[] { ' ', ',', ';', ':' }); Color[] colors = new Color[rgbs.Length]; for (Int32 i = 0; i < rgbs.Length; i++) { colors[i] = HexColorToColor(rgbs[i]); } Bitmap bmp = new Bitmap(10, 10, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmp); IntPtr hdc = g.GetHdc(); IntPtr oldfont = Gdi.SelectObject(hdc, font.ToHfont()); TEXTMETRIC tm = new TEXTMETRIC(); Gdi.GetTextMetrics(hdc, ref tm); Int32 totalwidth = 0; Int32[] width = new Int32[1]; Int32[] widths = new Int32[s.Length]; Graphics gs = Graphics.FromImage(new Bitmap(1, 1)); for (Int32 i = 0; i < s.Length; i++) { Gdi.GetCharWidth(hdc, s[i], s[i], width); SizeF sizeW = gs.MeasureString(s[i].ToString(), new Font(_font, _size)); if (s[i] >= 0x4e00 && s[i] <= 0x9fbb) { widths[i] = (int)sizeW.Width - _offset; } else { widths[i] = width[0]; } } SizeF sizeF = gs.MeasureString(s, font); totalwidth = (int)sizeF.Width; Gdi.SelectObject(hdc, oldfont); g.ReleaseHdc(hdc); g.Dispose(); bmp.Dispose(); LayeredImage image = new LayeredImage(totalwidth + 4, tm.tmHeight + 4 + 4); Layer bg = image.Layers.Add(); bg.Clear(Color.Black); bg.DrawText(0, 0, s, font, new SolidBrush(Color.White)); Layer copybg = image.Layers.Copy(bg); copybg.Blur(4, 4); Layer white = image.Layers.Add(); white.Clear(Color.White); if (_shadow) { Layer shadow = image.Layers.Copy(copybg); shadow.Invert(); shadow.Opacity = 0.5; shadow.OffsetX += 4; shadow.OffsetY += 4; } Int32 offsetx = 0; Int32 colorindex = 0; Layer final = image.Layers.Add(); for (Int32 i = 0; i < s.Length; i++) { Color c = colors[colorindex]; colorindex++; if (colorindex >= colors.Length) { colorindex = 0; } SolidBrush brush = new SolidBrush(c); final.FillRectangle(offsetx, 0, widths[i], tm.tmHeight, brush); offsetx += widths[i]; } final.BumpMap(copybg, 135, 45, 3, false); final.Mask = (FastBitmap)bg.Bitmap.Clone(); FastBitmap result = image.Flatten(); result._bitmap.MakeTransparent(); return(result._bitmap); }
internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
public static extern bool GetTextMetrics( IntPtr hdc, out TEXTMETRIC lptm );
public static int GetTextBaseline(Control ctrl, System.Drawing.ContentAlignment alignment) { Rectangle clientRectangle = ctrl.ClientRectangle; int num = 0; int num2 = 0; using (Graphics graphics = ctrl.CreateGraphics()) { IntPtr hdc = graphics.GetHdc(); IntPtr handle = ctrl.Font.ToHfont(); try { IntPtr handle2 = SelectObject(new HandleRef(ctrl, hdc), new HandleRef(ctrl, handle)); TEXTMETRIC tEXTMETRIC = new TEXTMETRIC(); GetTextMetrics(new HandleRef(ctrl, hdc), tEXTMETRIC); num = tEXTMETRIC.tmAscent + 1; num2 = tEXTMETRIC.tmHeight; SelectObject(new HandleRef(ctrl, hdc), new HandleRef(ctrl, handle2)); } finally { DeleteObject(new HandleRef(ctrl.Font, handle)); graphics.ReleaseHdc(hdc); } } if ((alignment & (System.Drawing.ContentAlignment)7) != (System.Drawing.ContentAlignment)0) { return clientRectangle.Top + num; } if ((alignment & (System.Drawing.ContentAlignment)112) != (System.Drawing.ContentAlignment)0) { return clientRectangle.Top + clientRectangle.Height / 2 - num2 / 2 + num; } return clientRectangle.Bottom - num2 + num; }
private FontMetricsImpl(Graphics graphics, Font font) { metrics = GenerateTextMetrics(graphics, font); }
internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) { IntPtr dc; IntPtr prevobj; TEXTMETRIC tm; tm = new TEXTMETRIC(); dc = Win32GetDC (IntPtr.Zero); prevobj = Win32SelectObject (dc, font.ToHfont ()); if (Win32GetTextMetrics (dc, ref tm) == false) { prevobj = Win32SelectObject (dc, prevobj); Win32DeleteObject (prevobj); Win32ReleaseDC (IntPtr.Zero, dc); ascent = 0; descent = 0; return false; } prevobj = Win32SelectObject (dc, prevobj); Win32DeleteObject (prevobj); Win32ReleaseDC (IntPtr.Zero, dc); ascent = tm.tmAscent; descent = tm.tmDescent; return true; }
public static extern bool GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
public static extern Boolean GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)] public static extern bool GetTextMetrics( IntPtr hdc, out TEXTMETRIC lptm );
public static extern bool GetTextMetrics([In] SafeHandle hDC, [Out] out TEXTMETRIC lptm);
private FontMetricsImpl(Graphics graphics, System.Drawing.Font font) { this.metrics = this.GenerateTextMetrics(graphics, font); }
public static extern bool GetTextMetrics(IntPtr hdc, out TEXTMETRIC lptm);
static extern bool GetTextMetrics(DeviceContextSafeHandle hdc, out TEXTMETRIC lptm);