当前位置: 首页>>代码示例>>C#>>正文


C# Font.ToHfont方法代码示例

本文整理汇总了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);
		}
开发者ID:sillsdev,项目名称:WorldPad,代码行数:35,代码来源:TextUtil.cs

示例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);
		}
开发者ID:lujinlong,项目名称:Apq,代码行数:35,代码来源:TextUtil.cs

示例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();
 }
开发者ID:floatas,项目名称:highsign,代码行数:33,代码来源:CodepointRange.cs

示例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);
        }
开发者ID:modulexcite,项目名称:CommandBar,代码行数:25,代码来源:TextGraphics.cs

示例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);

		}
开发者ID:bbriggs,项目名称:FieldWorks,代码行数:35,代码来源:TextUtil.cs

示例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;
		}
开发者ID:bossaia,项目名称:alexandrialibrary,代码行数:37,代码来源:NativeMethods.cs

示例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;
        }
开发者ID:0x00f,项目名称:stm32plus,代码行数:29,代码来源:FontUtil.cs

示例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);
        }
开发者ID:SeregaPru,项目名称:metrohome65,代码行数:30,代码来源:StringHelpers.cs

示例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;
 }
开发者ID:czaloj,项目名称:signed-distance-field-font-generator,代码行数:8,代码来源:Form1.cs

示例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 );
		}
开发者ID:ballju,项目名称:SpaceTrader-GPL-1.1.14,代码行数:9,代码来源:KerningHelper.cs

示例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);
        }
开发者ID:kzys,项目名称:Gmail-Notifier-Plus,代码行数:54,代码来源:GlassHelper.cs

示例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;
        }
开发者ID:modulexcite,项目名称:CommandBar,代码行数:12,代码来源:TextGraphics.cs

示例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);
            }

        }
开发者ID:gmilazzoitag,项目名称:OpenLiveWriter,代码行数:50,代码来源:GdiTextHelper.cs

示例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;
    }
开发者ID:ewosp,项目名称:sycorax,代码行数:16,代码来源:TextUtil.cs

示例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;
            }
        }
开发者ID:flts,项目名称:fleux,代码行数:20,代码来源:StringHelpers.cs


注:本文中的System.Drawing.Font.ToHfont方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。