Well, I was hoping to give you a nice example but I've discovered that much
of the information that I hoped was available is not.
I did manage to put together something that gets what font info is available
and has some interop for the LOGFONT structures but alas the system doesn't
return the information required such as the font pitch and family.
For what it's worth you can see the code after my signature. Perhaps if I
play with it some more I'll be able to squeeze a bit more juice out of it.
--
Bob Powell [MVP]
C#, System.Drawing
The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
Read my Blog at
http://bobpowelldotnet.blogspot.com
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace logfontexmpl
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(224, 248);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Go";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[DllImport("Gdi32.dll")]
protected static extern int GetObject(System.IntPtr hGDIObj, int chBuffer,
System.IntPtr lpBuffer);
private unsafe void button1_Click(object sender, System.EventArgs e)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
sb.Append("Monospaced font families:\r\n");
foreach(FontFamily ff in FontFamily.Families)
{
Font fn=null;
if(ff.IsStyleAvailable(FontStyle.Regular))
fn=new Font(ff,10);
else if(ff.IsStyleAvailable(FontStyle.Italic))
fn=new Font(ff,10,FontStyle.Italic);
else if(ff.IsStyleAvailable(FontStyle.Bold))
fn=new Font(ff,10,FontStyle.Bold);
else
break;
LOGFONT[] lf=new LOGFONT[]{new LOGFONT()};
//fn.ToLogFont((object)lf);
GetObject(fn.ToHfont(),sizeof(LOGFONT),Marshal.Uns afeAddrOfPinnedArrayElemen
t(lf,0));
if((lf[0].lfPitchAndFamily & 0x03)
!=(int)PitchAndFamilyDefs.FIXED_PITCH) //This isn't working becase the info
is not in the LOGFONT!!! note that the operator should be == not !=
{
sb.Append(ff.Name+"\r\n");
}
fn.Dispose();
}
this.textBox1.Text=sb.ToString();
}
}
public enum OutPrecisionDefs
{
OUT_DEFAULT_PRECIS,
OUT_STRING_PRECIS,
OUT_CHARACTER_PRECIS,
OUT_STROKE_PRECIS,
OUT_TT_PRECIS,
OUT_DEVICE_PRECIS,
OUT_RASTER_PRECIS,
OUT_TT_ONLY_PRECIS,
OUT_OUTLINE_PRECIS,
OUT_SCREEN_OUTLINE_PRECIS,
OUT_PS_ONLY_PRECIS
}
public enum ClipPrecisionDefs
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2,
CLIP_MASK = 0xf,
CLIP_LH_ANGLES = (1<<4),
CLIP_TT_ALWAYS = (2<<4),
CLIP_EMBEDDED = (8<<4)
}
public enum QualityDefs
{
DEFAULT_QUALITY, //0
DRAFT_QUALITY, //1
PROOF_QUALITY, //2
NONANTIALIASED_QUALITY, //3
ANTIALIASED_QUALITY, //4
CLEARTYPE_QUALITY, //5
CLEARTYPE_NATURAL_QUALITY //6
}
public enum PitchAndFamilyDefs
{
DEFAULT_PITCH =0,
FIXED_PITCH =1,
VARIABLE_PITCH =2,
FF_DONTCARE =(0<<4), /* Don't care or don't know. */
FF_ROMAN =(1<<4), /* Variable stroke width, serifed. */
/* Times Roman, Century Schoolbook, etc. */
FF_SWISS =(2<<4), /* Variable stroke width, sans-serifed. */
/* Helvetica, Swiss, etc. */
FF_MODERN =(3<<4), /* Constant stroke width, serifed or
sans-serifed. */
/* Pica, Elite, Courier, etc. */
FF_SCRIPT =(4<<4), /* Cursive, etc. */
FF_DECORATIVE =(5<<4), /* Old English, etc. */
}
public enum CharsetDefs
{
ANSI_CHARSET =0,
DEFAULT_CHARSET =1,
SYMBOL_CHARSET =2,
SHIFTJIS_CHARSET =128,
HANGEUL_CHARSET =129,
HANGUL_CHARSET =129,
GB2312_CHARSET =134,
CHINESEBIG5_CHARSET =136,
OEM_CHARSET =255,
JOHAB_CHARSET =130,
HEBREW_CHARSET =177,
ARABIC_CHARSET =178,
GREEK_CHARSET =161,
TURKISH_CHARSET =162,
VIETNAMESE_CHARSET =163,
THAI_CHARSET =222,
EASTEUROPE_CHARSET =238,
RUSSIAN_CHARSET =204,
MAC_CHARSET =77,
BALTIC_CHARSET =186
}
public enum FontWeightDefs
{
FW_DONTCARE =0,
FW_THIN =100,
FW_EXTRALIGHT =200,
FW_LIGHT =300,
FW_NORMAL =400,
FW_MEDIUM =500,
FW_SEMIBOLD =600,
FW_BOLD =700,
FW_EXTRABOLD =800,
FW_HEAVY =900,
FW_ULTRALIGHT =200,
FW_REGULAR =400,
FW_DEMIBOLD =600,
FW_ULTRABOLD =800,
FW_BLACK =900
}
[StructLayout(LayoutKind.Sequential)]
public struct LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
}
}
"Brian P. Bailey" <NS************@hotmail.com> wrote in message
news:FNAzb.412372$HS4.3330367@attbi_s01...
Hello, I'm trying to enumerate the fixed-pitch font families installed on
a system. I am _not_ trying to create a generic monospaced font. I've tried
looking at:
FontFamily
InstalledFontCollection
Font
The closest I could get was to use Font.ToLogFont(...). I should be able
to get a GDI LOGFONT structure and then look at the lfPitchAndFamily member.
Problem is, this requires unmanaged code, and I cannot find any quick
examples of how to use this function anyway (e.g., I cannot find a
namespace containing the LOGFONT structure, so how do I cast the object parameter?).
I clearly have no experience in using unmanaged code with .NET.
Is there an easier way to do this, preferably with managed code only? If
not, can someone provide or point me to an example of using the
ToLogFont(...) function, including namespace references?
Thanks in advance,
Brian P. Bailey