Thread: Font Embedding to TextBox

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Font Embedding to TextBox

    I have successfully loaded a .ttf font. Now, how do I use it in a TextBox?

    FILE: MainWindow.xaml.cs
    Code:
    namespace myNameSpace
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
    		public MainWindow()
    		{
    			InitializeComponent();
    			LoadFont();
    		}
    
    		[DllImport("gdi32.dll")]
    		private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
    		PrivateFontCollection mFontCollection = new PrivateFontCollection();
    		private void LoadFont()
    		{
    			// specify embedded resource name
    			string resource = "myNameSpace.fixedsys.ttf";
    			// receive resource stream
    			Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
    			//create an unsafe memory block for the data         
    			System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);         
    			//create a buffer to read in to         
    			Byte[] fontData = new Byte[fontStream.Length];         
    			//fetch the font program from the resource         
    			fontStream.Read(fontData, 0, (int)fontStream.Length);         
    			//copy the bytes to the unsafe memory block         
    			Marshal.Copy(fontData, 0, data, (int)fontStream.Length);          
    			// We HAVE to do this to register the font to the system (Weird .NET bug !)         
    			uint cFonts = 0;         
    			AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts);          
    			//pass the font to the font collection         
    			mFontCollection.AddMemoryFont(data, (int)fontStream.Length);
    			//close the resource stream
    			fontStream.Close();         
    			//free the unsafe memory         
    			Marshal.FreeCoTaskMem(data); 
    		}
    	}
    }
    FILE: MainWindow.xaml
    Code:
    <Window x:Class="myNameSpace.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Hello World" Height="768" Width="1366" Loaded="Window_Loaded" Closed="Window_Closed">
        <Grid Background="Black">
    	
    		<TextBox 
    			x:Name="OutputWindow_TEST"
    			TextWrapping="Wrap"
    			HorizontalScrollBarVisibility="Auto"
    			VerticalScrollBarVisibility="Auto"
    			Background="Black" 
    			Foreground="White"
    			Width="338"
    		/>
    	</Grid>
    </Window>
    So, in my "OutputWindow_TEST" textbox, how do I add my embedded font?

    I type the "FontFamily" property of the textbox and all my system fonts show up as options, but not the one I embedded.

    Thanks.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'd say you need to assign a FontFamily from the Families property of your collection to your textblock's FontFamily property. You will get no Intellisense in XAML for something you will only have loaded at runtime.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by nvoigt View Post
    I'd say you need to assign a FontFamily from the Families property of your collection to your textblock's FontFamily property. You will get no Intellisense in XAML for something you will only have loaded at runtime.
    Okay, so I add:

    Code:
    this.myTextBox.FontFamily = mFontCollection.Families[0];
    And get this error:
    error CS0029: Cannot implicitly convert type 'System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily'

    When I debug mFontCollection.Families[0], sure enough, it contains:
    Name:
    ((System.Drawing.Text.FontCollection)(mFontCollect ion)).Families[0].Name
    Value:
    "FixedsysTTF"

    So I know it's getting loaded correctly.


    Any ideas where I'm messing up?

    Thanks.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If your font is known at compile-time, you may want to look at this MSDN page instead of loading it with an old windows forms method.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issues embedding Guile in C++
    By Lysander in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2011, 01:00 AM
  2. Python Embedding Help...
    By Rendered in forum C Programming
    Replies: 2
    Last Post: 11-17-2007, 10:08 AM
  3. embedding web pages
    By Devil Panther in forum Windows Programming
    Replies: 9
    Last Post: 01-14-2005, 09:37 AM
  4. font & font color in edit control
    By XRIC++ in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2003, 09:07 PM