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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(8, 8);
this.textBox1.M ultiline = true;
this.textBox1.N ame = "textBox1";
this.textBox1.S crollBars = System.Windows. Forms.ScrollBar s.Vertical;
this.textBox1.S ize = new System.Drawing. Size(224, 248);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(240, 32);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "Go";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(320, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
[DllImport("Gdi3 2.dll")]
protected static extern int GetObject(Syste m.IntPtr hGDIObj, int chBuffer,
System.IntPtr lpBuffer);
private unsafe void button1_Click(o bject sender, System.EventArg s e)
{
System.Text.Str ingBuilder sb=new System.Text.Str ingBuilder();
sb.Append("Mono spaced font families:\r\n") ;
foreach(FontFam ily ff in FontFamily.Fami lies)
{
Font fn=null;
if(ff.IsStyleAv ailable(FontSty le.Regular))
fn=new Font(ff,10);
else if(ff.IsStyleAv ailable(FontSty le.Italic))
fn=new Font(ff,10,Font Style.Italic);
else if(ff.IsStyleAv ailable(FontSty le.Bold))
fn=new Font(ff,10,Font Style.Bold);
else
break;
LOGFONT[] lf=new LOGFONT[]{new LOGFONT()};
//fn.ToLogFont((o bject)lf);
GetObject(fn.To Hfont(),sizeof( LOGFONT),Marsha l.UnsafeAddrOfP innedArrayEleme n
t(lf,0));
if((lf[0].lfPitchAndFami ly & 0x03)
!=(int)PitchAnd FamilyDefs.FIXE D_PITCH) //This isn't working becase the info
is not in the LOGFONT!!! note that the operator should be == not !=
{
sb.Append(ff.Na me+"\r\n");
}
fn.Dispose();
}
this.textBox1.T ext=sb.ToString ();
}
}
public enum OutPrecisionDef s
{
OUT_DEFAULT_PRE CIS,
OUT_STRING_PREC IS,
OUT_CHARACTER_P RECIS,
OUT_STROKE_PREC IS,
OUT_TT_PRECIS,
OUT_DEVICE_PREC IS,
OUT_RASTER_PREC IS,
OUT_TT_ONLY_PRE CIS,
OUT_OUTLINE_PRE CIS,
OUT_SCREEN_OUTL INE_PRECIS,
OUT_PS_ONLY_PRE CIS
}
public enum ClipPrecisionDe fs
{
CLIP_DEFAULT_PR ECIS = 0,
CLIP_CHARACTER_ PRECIS = 1,
CLIP_STROKE_PRE CIS = 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_QUA LITY, //4
CLEARTYPE_QUALI TY, //5
CLEARTYPE_NATUR AL_QUALITY //6
}
public enum PitchAndFamilyD efs
{
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_CHARSE T =128,
HANGEUL_CHARSET =129,
HANGUL_CHARSET =129,
GB2312_CHARSET =134,
CHINESEBIG5_CHA RSET =136,
OEM_CHARSET =255,
JOHAB_CHARSET =130,
HEBREW_CHARSET =177,
ARABIC_CHARSET =178,
GREEK_CHARSET =161,
TURKISH_CHARSET =162,
VIETNAMESE_CHAR SET =163,
THAI_CHARSET =222,
EASTEUROPE_CHAR SET =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(La youtKind.Sequen tial)]
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 lfPitchAndFamil y;
}
}
"Brian P. Bailey" <NS************ @hotmail.com> wrote in message
news:FNAzb.4123 72$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
InstalledFontCo llection
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 lfPitchAndFamil y 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