本文整理汇总了C#中System.Drawing.Font.ToHfont方法的典型用法代码示例。如果您正苦于以下问题:C# Font.ToHfont方法的具体用法?C# Font.ToHfont怎么用?C# Font.ToHfont使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Font
的用法示例。
在下文中一共展示了Font.ToHfont方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font)
{
IntPtr hdc = IntPtr.Zero;
if ( graphics != null )
{
// Get device context from the graphics passed in
hdc = graphics.GetHdc();
}
else
{
// Get screen device context
hdc = WindowsAPI.GetDC(IntPtr.Zero);
}
IntPtr fontHandle = font.ToHfont();
IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);
Win32.RECT rect = new Win32.RECT();
rect.left = 0;
rect.right = 0;
rect.top = 0;
rect.bottom = 0;
WindowsAPI.DrawText(hdc, text, text.Length, ref rect,
(int)(DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_LEFT | DrawTextFormatFlags.DT_CALCRECT));
WindowsAPI.SelectObject(hdc, currentFontHandle);
WindowsAPI.DeleteObject(fontHandle);
if ( graphics != null )
graphics.ReleaseHdc(hdc);
else
WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);
return new Size(rect.right - rect.left, rect.bottom - rect.top);
}
示例2: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font)
{
IntPtr hdc = IntPtr.Zero;
if ( graphics != null )
{
// Get device context from the graphics passed in
hdc = graphics.GetHdc();
}
else
{
// Get screen device context
hdc = APIsGdi.GetDC(IntPtr.Zero);
}
IntPtr fontHandle = font.ToHfont();
IntPtr currentFontHandle = APIsGdi.SelectObject(hdc, fontHandle);
APIsStructs.RECT rect = new APIsStructs.RECT();
rect.left = 0;
rect.right = 0;
rect.top = 0;
rect.bottom = 0;
APIsUser32.DrawText(hdc, text, text.Length, ref rect,
APIsEnums.DrawTextFormatFlags.SINGLELINE | APIsEnums.DrawTextFormatFlags.LEFT | APIsEnums.DrawTextFormatFlags.CALCRECT);
APIsGdi.SelectObject(hdc, currentFontHandle);
APIsGdi.DeleteObject(fontHandle);
if(graphics != null)
graphics.ReleaseHdc(hdc);
else
APIsUser32.ReleaseDC(IntPtr.Zero, hdc);
return new Size(rect.right - rect.left, rect.bottom - rect.top);
}
示例3: CodepointRange
/// <summary>
/// Creates a new CodepointRange object for a font.
/// </summary>
public CodepointRange(Font font)
{
List<char> rangeList = new List<char>();
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
IntPtr hdc = g.GetHdc();
IntPtr hFont = font.ToHfont();
IntPtr oldFont = SelectObject(hdc, hFont);
uint size = GetFontUnicodeRanges(hdc, IntPtr.Zero);
IntPtr glyphSet = Marshal.AllocHGlobal((int)size);
GetFontUnicodeRanges(hdc, glyphSet);
codepointCount = Marshal.ReadInt32(glyphSet, 8);
int tmp = 0;
int count = Marshal.ReadInt32(glyphSet, 12);
for (int i = 0; i < count; i++)
{
char firstIncluded = (char)Marshal.ReadInt16(glyphSet, 16 + i * 4);
char firstExcluded = (char)(firstIncluded + Marshal.ReadInt16(glyphSet, 18 + i * 4));
tmp += firstExcluded - firstIncluded;
rangeList.Add(firstIncluded);
rangeList.Add(firstExcluded);
}
SelectObject(hdc, oldFont);
DeleteObject(hFont);
Marshal.FreeHGlobal(glyphSet);
g.ReleaseHdc(hdc);
g.Dispose();
if (tmp != codepointCount) throw new Exception(font.FontFamily.Name);
ranges = rangeList.ToArray();
if (ranges.Length < 2) throw new Exception();
}
示例4: DrawText
public void DrawText(string text, Point point, Font font, Color foreColor)
{
IntPtr fontHandle = font.ToHfont();
IntPtr oldFontHandle = NativeMethods.SelectObject(this.graphicsHandle, fontHandle);
int oldBkMode = NativeMethods.SetBkMode(this.graphicsHandle, NativeMethods.TRANSPARENT);
int oldTextColor = NativeMethods.SetTextColor(this.graphicsHandle, Color.FromArgb(0, foreColor.R, foreColor.G, foreColor.B).ToArgb());
Size size = this.MeassureTextInternal(text);
NativeMethods.RECT clip = new NativeMethods.RECT();
clip.left = point.X;
clip.top = point.Y;
clip.right = clip.left + size.Width;
clip.bottom = clip.top + size.Height;
// ExtTextOut does not show Mnemonics highlighting.
NativeMethods.DrawText(this.graphicsHandle, text, text.Length, ref clip, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT);
NativeMethods.SetTextColor(this.graphicsHandle, oldTextColor);
NativeMethods.SetBkMode(this.graphicsHandle, oldBkMode);
NativeMethods.SelectObject(this.graphicsHandle, oldFontHandle);
NativeMethods.DeleteObject(fontHandle);
}
示例5: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags)
{
IntPtr hdc = IntPtr.Zero;
if ( graphics != null )
{
// Get device context from the graphics passed in
hdc = graphics.GetHdc();
}
else
{
// Get screen device context
hdc = WindowsAPI.GetDC(IntPtr.Zero);
}
IntPtr fontHandle = font.ToHfont();
IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);
Win32.RECT rect = new Win32.RECT();
rect.left = rc.Left;
rect.right = rc.Right;
rect.top = rc.Top;
rect.bottom = rc.Bottom;
WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags);
WindowsAPI.SelectObject(hdc, currentFontHandle);
WindowsAPI.DeleteObject(fontHandle);
if ( graphics != null )
graphics.ReleaseHdc(hdc);
else
WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);
return new Size(rect.right - rect.left, rect.bottom - rect.top);
}
示例6: IsFixedPitch
/// <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;
}
示例7: GetFontUnicodeRanges
/*
* get unicode font ranges
*/
public static List<FontRange> GetFontUnicodeRanges(Font font)
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = g.GetHdc();
IntPtr hFont = font.ToHfont();
IntPtr old = SelectObject(hdc, hFont);
uint size = GetFontUnicodeRanges(hdc, IntPtr.Zero);
IntPtr glyphSet = Marshal.AllocHGlobal((int)size);
GetFontUnicodeRanges(hdc, glyphSet);
List<FontRange> fontRanges = new List<FontRange>();
int count = Marshal.ReadInt32(glyphSet, 12);
for (int i = 0; i < count; i++)
{
FontRange range = new FontRange();
range.Low = (UInt16)Marshal.ReadInt16(glyphSet, 16 + i * 4);
range.High = (UInt16)(range.Low + Marshal.ReadInt16(glyphSet, 18 + i * 4) - 1);
fontRanges.Add(range);
}
SelectObject(hdc, old);
Marshal.FreeHGlobal(glyphSet);
g.ReleaseHdc(hdc);
g.Dispose();
return fontRanges;
}
示例8: MeasureString
/// <summary>
/// Measure a multiline string
/// </summary>
public static Size MeasureString(Graphics gr, Font font, string text, int width)
{
if (text == null) return new Size(1, 1);
Rect bounds = new Rect() { Left = 0, Right = width, Bottom = 1, Top = 0 };
IntPtr hDc = gr.GetHdc();
try
{
int flags = DTCALCRECT | DTWORDBREAK;
IntPtr controlFont = font.ToHfont();
IntPtr originalObject = SelectObject(hDc, controlFont);
try
{
DrawText(hDc, text, text.Length, ref bounds, flags);
}
finally
{
SelectObject(hDc, originalObject); // Release resources
}
}
finally
{
gr.ReleaseHdc(hDc);
}
return new Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
}
示例9: GetTextMetrics
Win32.TEXTMETRIC GetTextMetrics(Font f)
{
Win32.TEXTMETRIC t;
IntPtr hdc = Win32.GetDC(Handle);
Win32.SelectObject(hdc, f.ToHfont());
Win32.GetTextMetrics(hdc, out t);
return t;
}
示例10: KerningHelper
public KerningHelper( Font font )
{
dc = CreateCompatibleDC( IntPtr.Zero );
if( dc == IntPtr.Zero )
throw new System.ComponentModel.Win32Exception();
hFont = font.ToHfont();
oldFont = SelectObject( dc, hFont );
}
示例11: DrawText
public static void DrawText(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, TextStyle textStyle)
{
if (!VisualStyleRenderer.IsSupported) {
TextRenderer.DrawText(graphics, text, font, bounds, color, flags);
return;
}
IntPtr primaryHdc = graphics.GetHdc();
// Create a memory DC so we can work offscreen
IntPtr memoryHdc = CreateCompatibleDC(primaryHdc);
// Create a device-independent bitmap and select it into our DC
BITMAPINFO info = new BITMAPINFO();
info.biSize = Marshal.SizeOf(info);
info.biWidth = bounds.Width;
info.biHeight = -bounds.Height;
info.biPlanes = 1;
info.biBitCount = 32;
info.biCompression = 0; // BI_RGB
IntPtr dib = CreateDIBSection(primaryHdc, info, 0, 0, IntPtr.Zero, 0);
SelectObject(memoryHdc, dib);
// Create and select font
IntPtr fontHandle = font.ToHfont();
SelectObject(memoryHdc, fontHandle);
// Draw glowing text
VisualStyleRenderer renderer = new VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
DTTOPTS dttOpts = new DTTOPTS();
dttOpts.dwSize = Marshal.SizeOf(typeof(DTTOPTS));
if (textStyle == TextStyle.Glowing) {
dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE | DTT_TEXTCOLOR;
}
else {
dttOpts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR;
}
dttOpts.crText = ColorTranslator.ToWin32(color);
dttOpts.iGlowSize = 8; // This is about the size Microsoft Word 2007 uses
RECT textBounds = new RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts);
// Copy to foreground
const int SRCCOPY = 0x00CC0020;
BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY);
// Clean up
DeleteObject(fontHandle);
DeleteObject(dib);
DeleteDC(memoryHdc);
graphics.ReleaseHdc(primaryHdc);
}
示例12: MeasureText
public Size MeasureText(string text, Font font)
{
IntPtr fontHandle = font.ToHfont();
IntPtr oldFontHandle = NativeMethods.SelectObject(this.graphicsHandle, fontHandle);
Size size = this.MeassureTextInternal(text);
NativeMethods.SelectObject(this.graphicsHandle, oldFontHandle);
NativeMethods.DeleteObject(fontHandle);
return size;
}
示例13: DrawString
public static void DrawString(Control context, Font font, string text, Rectangle textBounds, bool useMnemonics, GdiTextDrawMode textMode)
{
IntPtr hdc = User32.GetDC(context.Handle);
try
{
Gdi32.SetBkColor(hdc, Gdi32.ToColorRef(context.BackColor));
Gdi32.SetTextColor(hdc, Gdi32.ToColorRef(context.ForeColor));
try
{
IntPtr hFont = font.ToHfont();
try
{
IntPtr hPrevFont = Gdi32.SelectObject(hdc, hFont);
try
{
StringBuilder sb = new StringBuilder(text);
RECT rect = textBounds;
User32.DRAWTEXTPARAMS dtparams = new User32.DRAWTEXTPARAMS();
dtparams.cbSize = (uint)Marshal.SizeOf(typeof(User32.DRAWTEXTPARAMS));
User32.DT flags =
textMode == GdiTextDrawMode.EndEllipsis ? User32.DT.END_ELLIPSIS
: textMode == GdiTextDrawMode.WordBreak ? User32.DT.WORDBREAK | User32.DT.END_ELLIPSIS
: User32.DT.WORDBREAK;
if (useMnemonics)
flags |= User32.DT.NOPREFIX;
if (0 == User32.DrawTextEx(hdc, sb, sb.Length, ref rect, flags, ref dtparams))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
finally
{
Gdi32.SelectObject(hdc, hPrevFont);
}
}
finally
{
Gdi32.DeleteObject(hFont);
}
}
finally
{
}
}
finally
{
User32.ReleaseDC(context.Handle, hdc);
}
}
示例14: GetTextSize
public static Size GetTextSize( Graphics g, string text, Font font )
{
IntPtr hdc = GetGraphicsHDC( g );
IntPtr hFont = font.ToHfont();
IntPtr hOldFont = WindowsAPI.SelectObject( hdc, hFont );
RECT rect = new RECT();
WindowsAPI.DrawText( hdc, text, text.Length, ref rect, DEF_TEXTFORMAT );
WindowsAPI.SelectObject( hdc, hOldFont );
WindowsAPI.DeleteObject( hFont );
ReleaseGraphicHDC( g, hdc );
return ( Size )rect;
}
示例15: MeasureString
/// <summary>
/// Measure a multiline string
/// </summary>
public static Size MeasureString(Graphics gr, Font font, string text, int width)
{
try{
Rect bounds = new Rect() { Left = 0, Right = width, Bottom = 1, Top = 0 };
IntPtr hDc = gr.GetHdc();
int flags = DTCALCRECT | DTWORDBREAK;
IntPtr controlFont = font.ToHfont();
IntPtr originalObject = SelectObject(hDc, controlFont);
DrawText(hDc, text, text.Length, ref bounds, flags);
SelectObject(hDc, originalObject); // Release resources
gr.ReleaseHdc(hDc);
return new Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
}catch(Exception){
return Size.Empty;
}
}