EnumFontFamiliesEx in C#

August 08th, 2007 - 03:11 am ET by Estee Tan | Report spam
Hi all,

I am not very familiar with win32 api. How do you use EnumFontFamiliesEx in
C# when there is a call back in the argument?

cheers
email Follow the discussionReplies 5 repliesReplies Make a reply

Similar topics

Replies

#1 Michael Phillips, Jr.
August 08th, 2007 - 10:29 am ET | Report spam
Use a c# delegate to pass the callback function using p-invoke.

Here is some example code to help you get started:
http://www.codeproject.com/dotnet/C...arp_VB.asp


"Estee Tan" wrote in message
news:
Hi all,

I am not very familiar with win32 api. How do you use EnumFontFamiliesEx
in
C# when there is a call back in the argument?

cheers


Replies Reply to this message
#2 Estee Tan
August 08th, 2007 - 09:04 pm ET | Report spam
Hi Michael,

I have tried the following code but the callback code is not being called at
all.

class PrinterEnumerator
{

public delegate int EnumFontExDelegate(ref
PrinterHelper.ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr
lParam);

[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In] ref
PrinterHelper.LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
dwFlags);

[DllImport("gdi32.dll")]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
string lpszOutput, IntPtr lpInitData);

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer,
ref int size);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;

if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name length{1}",
dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");

}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
IntPtr.Zero);

// instantiate the delegate
EnumFontExDelegate del = new
EnumFontExDelegate(EnumFontFamExProc);
PrinterHelper.LOGFONT logFont = PrinterHelper.CreateLogFont("");

// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero, 0);
DeleteDC(hPrinterDC);
}

public static int EnumFontFamExProc(ref PrinterHelper.ENUMLOGFONTEX
lpelfe, IntPtr lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test Printer Name :" + lpelfe.elfFullName);
return 1;
}
}


I would expect the EnumFontFamExProc will be called at least once or more
times but it's not. Do you have any clue why?

cheers
Estee


"Michael Phillips, Jr." wrote:

Use a c# delegate to pass the callback function using p-invoke.

Here is some example code to help you get started:
http://www.codeproject.com/dotnet/C...arp_VB.asp


"Estee Tan" wrote in message
news:
> Hi all,
>
> I am not very familiar with win32 api. How do you use EnumFontFamiliesEx
> in
> C# when there is a call back in the argument?
>
> cheers





Replies Reply to this message
#3 Estee Tan
August 08th, 2007 - 11:22 pm ET | Report spam
Hi there,

I post it again with the full code I had. I had no luck in the call back
method EnumFontFamExProc. It does not print out the "Test :" in my debug
console output.
Is there anything missing in my code?

class PrinterEnumerator
{
public const int LF_FACESIZE = 32;
private const byte DEFAULT_CHARSET = 1;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public int lfHeight = 0;
public int lfWidth = 0;
public int lfEscapement = 0;
public int lfOrientation = 0;
public int lfWeight = 0;
public byte lfItalic = 0;
public byte lfUnderline = 0;
public byte lfStrikeOut = 0;
public byte lfCharSet = 0;
public byte lfOutPrecision = 0;
public byte lfClipPrecision = 0;
public byte lfQuality = 0;
public byte lfPitchAndFamily = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string lfFaceName = string.Empty;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ENUMLOGFONTEX
{
public LOGFONT elfLogFont;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfFullName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfStyle;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfScript;
}

public delegate int EnumFontExDelegate(ref ENUMLOGFONTEX lpelfe,
IntPtr lpntme, int FontType, IntPtr lParam);

[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In,
MarshalAs(UnmanagedType.LPStruct)] ref LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
dwFlags);

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
string lpszOutput, IntPtr lpInitData);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer,
ref int size);


public static LOGFONT CreateLogFont()
{
LOGFONT lf = new LOGFONT();
lf.lfHeight = 0;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 0;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
lf.lfFaceName = "";
return lf;
}

public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;

if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name length{1}",
dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");

}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
IntPtr.Zero);

// instantiate the delegate
EnumFontExDelegate del = new
EnumFontExDelegate(EnumFontFamExProc);
LOGFONT logFont = CreateLogFont();

// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero, 0);
DeleteDC(hPrinterDC);
}

public static int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe, IntPtr
lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test :" + lpelfe.elfFullName);
return 1;
}
}

"Estee Tan" wrote:

Hi Michael,

I have tried the following code but the callback code is not being called at
all.

class PrinterEnumerator
{

public delegate int EnumFontExDelegate(ref
PrinterHelper.ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr
lParam);

[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In] ref
PrinterHelper.LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
dwFlags);

[DllImport("gdi32.dll")]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
string lpszOutput, IntPtr lpInitData);

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer,
ref int size);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;

if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name length{1}",
dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");

}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
IntPtr.Zero);

// instantiate the delegate
EnumFontExDelegate del = new
EnumFontExDelegate(EnumFontFamExProc);
PrinterHelper.LOGFONT logFont = PrinterHelper.CreateLogFont("");

// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero, 0);
DeleteDC(hPrinterDC);
}

public static int EnumFontFamExProc(ref PrinterHelper.ENUMLOGFONTEX
lpelfe, IntPtr lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test Printer Name :" + lpelfe.elfFullName);
return 1;
}
}


I would expect the EnumFontFamExProc will be called at least once or more
times but it's not. Do you have any clue why?

cheers
Estee


"Michael Phillips, Jr." wrote:

> Use a c# delegate to pass the callback function using p-invoke.
>
> Here is some example code to help you get started:
> http://www.codeproject.com/dotnet/C...arp_VB.asp
>
>
> "Estee Tan" wrote in message
> news:
> > Hi all,
> >
> > I am not very familiar with win32 api. How do you use EnumFontFamiliesEx
> > in
> > C# when there is a call back in the argument?
> >
> > cheers
>
>
>


Replies Reply to this message
#4 Michael Phillips, Jr.
August 09th, 2007 - 01:10 pm ET | Report spam
You have a marshaling problem with your code.

The problem is easy to fix.
1) Change to the following declaration:
[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc,
[In] IntPtr pLogfont,
EnumFontExDelegate lpEnumFontFamExProc,
IntPtr lParam,
uint dwFlags);
2) Use Marshaling to marshal your managed LOGFONT to an unmanaged LOGFONT
...
LOGFONT logFont = CreateLogFont();
// add the following:
IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(logFont));
Marshal.StructureToPtr(logFont, plogFont, true);
EnumFontFamiliesEx(hPrinterDC, plogFont, del, IntPtr.Zero, 0);
// add the following:
// Free the pointer when your are finished with it
Marshal.FreeHGlobal(plogFont);

Now your code will work as you expected it to.


"Estee Tan" wrote in message
news:
Hi there,

I post it again with the full code I had. I had no luck in the call back
method EnumFontFamExProc. It does not print out the "Test :" in my debug
console output.
Is there anything missing in my code?

class PrinterEnumerator
{
public const int LF_FACESIZE = 32;
private const byte DEFAULT_CHARSET = 1;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public int lfHeight = 0;
public int lfWidth = 0;
public int lfEscapement = 0;
public int lfOrientation = 0;
public int lfWeight = 0;
public byte lfItalic = 0;
public byte lfUnderline = 0;
public byte lfStrikeOut = 0;
public byte lfCharSet = 0;
public byte lfOutPrecision = 0;
public byte lfClipPrecision = 0;
public byte lfQuality = 0;
public byte lfPitchAndFamily = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string lfFaceName = string.Empty;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ENUMLOGFONTEX
{
public LOGFONT elfLogFont;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfFullName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfStyle;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public string elfScript;
}

public delegate int EnumFontExDelegate(ref ENUMLOGFONTEX lpelfe,
IntPtr lpntme, int FontType, IntPtr lParam);

[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In,
MarshalAs(UnmanagedType.LPStruct)] ref LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
dwFlags);

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
string lpszOutput, IntPtr lpInitData);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError > true)]
public static extern bool GetDefaultPrinter(StringBuilder
pszBuffer,
ref int size);


public static LOGFONT CreateLogFont()
{
LOGFONT lf = new LOGFONT();
lf.lfHeight = 0;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 0;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
lf.lfFaceName = "";
return lf;
}

public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;

if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name length{1}",
dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");

}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
IntPtr.Zero);

// instantiate the delegate
EnumFontExDelegate del = new
EnumFontExDelegate(EnumFontFamExProc);
LOGFONT logFont = CreateLogFont();

// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero,
0);
DeleteDC(hPrinterDC);
}

public static int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe,
IntPtr
lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test :" + lpelfe.elfFullName);
return 1;
}
}

"Estee Tan" wrote:

Hi Michael,

I have tried the following code but the callback code is not being called
at
all.

class PrinterEnumerator
{

public delegate int EnumFontExDelegate(ref
PrinterHelper.ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr
lParam);

[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc, [In] ref
PrinterHelper.LOGFONT lpLogfont,
EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
dwFlags);

[DllImport("gdi32.dll")]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32
capindex);

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string
lpszDevice,
string lpszOutput, IntPtr lpInitData);

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError >> true)]
public static extern bool GetDefaultPrinter(StringBuilder
pszBuffer,
ref int size);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

public static void Test()
{
IntPtr hPrinterDC;
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;

if (GetDefaultPrinter(dp, ref size))
{
Console.Write(String.Format("Printer: {0}, name
length{1}",
dp.ToString().Trim(), size));
}
else
{
Console.WriteLine("Error!");

}
hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
IntPtr.Zero);

// instantiate the delegate
EnumFontExDelegate del = new
EnumFontExDelegate(EnumFontFamExProc);
PrinterHelper.LOGFONT logFont =
PrinterHelper.CreateLogFont("");

// call the win32 function
EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero,
0);
DeleteDC(hPrinterDC);
}

public static int EnumFontFamExProc(ref
PrinterHelper.ENUMLOGFONTEX
lpelfe, IntPtr lpntme, int FontType, IntPtr lParam)
{
Console.WriteLine("Test Printer Name :" +
lpelfe.elfFullName);
return 1;
}
}


I would expect the EnumFontFamExProc will be called at least once or more
times but it's not. Do you have any clue why?

cheers
Estee


"Michael Phillips, Jr." wrote:

> Use a c# delegate to pass the callback function using p-invoke.
>
> Here is some example code to help you get started:
> http://www.codeproject.com/dotnet/C...arp_VB.asp
>
>
> "Estee Tan" wrote in message
> news:
> > Hi all,
> >
> > I am not very familiar with win32 api. How do you use
> > EnumFontFamiliesEx
> > in
> > C# when there is a call back in the argument?
> >
> > cheers
>
>
>




Replies Reply to this message
#5 Estee Tan
August 10th, 2007 - 03:06 am ET | Report spam
Thanks for the reply!!!! Appreciated!

"Michael Phillips, Jr." wrote:

You have a marshaling problem with your code.

The problem is easy to fix.
1) Change to the following declaration:
[DllImport("gdi32.dll")]
static extern int EnumFontFamiliesEx(IntPtr hdc,
[In] IntPtr pLogfont,
EnumFontExDelegate lpEnumFontFamExProc,
IntPtr lParam,
uint dwFlags);
2) Use Marshaling to marshal your managed LOGFONT to an unmanaged LOGFONT
...
LOGFONT logFont = CreateLogFont();
// add the following:
IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(logFont));
Marshal.StructureToPtr(logFont, plogFont, true);
EnumFontFamiliesEx(hPrinterDC, plogFont, del, IntPtr.Zero, 0);
// add the following:
// Free the pointer when your are finished with it
Marshal.FreeHGlobal(plogFont);

Now your code will work as you expected it to.


"Estee Tan" wrote in message
news:
> Hi there,
>
> I post it again with the full code I had. I had no luck in the call back
> method EnumFontFamExProc. It does not print out the "Test :" in my debug
> console output.
> Is there anything missing in my code?
>
> class PrinterEnumerator
> {
> public const int LF_FACESIZE = 32;
> private const byte DEFAULT_CHARSET = 1;
>
> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
> public class LOGFONT
> {
> public int lfHeight = 0;
> public int lfWidth = 0;
> public int lfEscapement = 0;
> public int lfOrientation = 0;
> public int lfWeight = 0;
> public byte lfItalic = 0;
> public byte lfUnderline = 0;
> public byte lfStrikeOut = 0;
> public byte lfCharSet = 0;
> public byte lfOutPrecision = 0;
> public byte lfClipPrecision = 0;
> public byte lfQuality = 0;
> public byte lfPitchAndFamily = 0;
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
> public string lfFaceName = string.Empty;
> }
>
> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
> public struct ENUMLOGFONTEX
> {
> public LOGFONT elfLogFont;
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
> public string elfFullName;
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
> public string elfStyle;
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
> public string elfScript;
> }
>
> public delegate int EnumFontExDelegate(ref ENUMLOGFONTEX lpelfe,
> IntPtr lpntme, int FontType, IntPtr lParam);
>
> [DllImport("gdi32.dll")]
> static extern int EnumFontFamiliesEx(IntPtr hdc, [In,
> MarshalAs(UnmanagedType.LPStruct)] ref LOGFONT lpLogfont,
> EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
> dwFlags);
>
> [DllImport("gdi32.dll")]
> static extern IntPtr CreateDC(string lpszDriver, string lpszDevice,
> string lpszOutput, IntPtr lpInitData);
>
> [DllImport("gdi32.dll")]
> static extern bool DeleteDC(IntPtr hdc);
>
> [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError > > true)]
> public static extern bool GetDefaultPrinter(StringBuilder
> pszBuffer,
> ref int size);
>
>
> public static LOGFONT CreateLogFont()
> {
> LOGFONT lf = new LOGFONT();
> lf.lfHeight = 0;
> lf.lfWidth = 0;
> lf.lfEscapement = 0;
> lf.lfOrientation = 0;
> lf.lfWeight = 0;
> lf.lfItalic = 0;
> lf.lfUnderline = 0;
> lf.lfStrikeOut = 0;
> lf.lfCharSet = DEFAULT_CHARSET;
> lf.lfOutPrecision = 0;
> lf.lfClipPrecision = 0;
> lf.lfQuality = 0;
> lf.lfPitchAndFamily = 0;
> lf.lfFaceName = "";
> return lf;
> }
>
> public static void Test()
> {
> IntPtr hPrinterDC;
> StringBuilder dp = new StringBuilder(256);
> int size = dp.Capacity;
>
> if (GetDefaultPrinter(dp, ref size))
> {
> Console.Write(String.Format("Printer: {0}, name length{1}",
> dp.ToString().Trim(), size));
> }
> else
> {
> Console.WriteLine("Error!");
>
> }
> hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
> IntPtr.Zero);
>
> // instantiate the delegate
> EnumFontExDelegate del = new
> EnumFontExDelegate(EnumFontFamExProc);
> LOGFONT logFont = CreateLogFont();
>
> // call the win32 function
> EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero,
> 0);
> DeleteDC(hPrinterDC);
> }
>
> public static int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe,
> IntPtr
> lpntme, int FontType, IntPtr lParam)
> {
> Console.WriteLine("Test :" + lpelfe.elfFullName);
> return 1;
> }
> }
>
> "Estee Tan" wrote:
>
>> Hi Michael,
>>
>> I have tried the following code but the callback code is not being called
>> at
>> all.
>>
>> class PrinterEnumerator
>> {
>>
>> public delegate int EnumFontExDelegate(ref
>> PrinterHelper.ENUMLOGFONTEX lpelfe, IntPtr lpntme, int FontType, IntPtr
>> lParam);
>>
>> [DllImport("gdi32.dll")]
>> static extern int EnumFontFamiliesEx(IntPtr hdc, [In] ref
>> PrinterHelper.LOGFONT lpLogfont,
>> EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint
>> dwFlags);
>>
>> [DllImport("gdi32.dll")]
>> private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32
>> capindex);
>>
>> [DllImport("gdi32.dll")]
>> static extern IntPtr CreateDC(string lpszDriver, string
>> lpszDevice,
>> string lpszOutput, IntPtr lpInitData);
>>
>> [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError > >> true)]
>> public static extern bool GetDefaultPrinter(StringBuilder
>> pszBuffer,
>> ref int size);
>>
>> [DllImport("gdi32.dll")]
>> static extern bool DeleteDC(IntPtr hdc);
>>
>> public static void Test()
>> {
>> IntPtr hPrinterDC;
>> StringBuilder dp = new StringBuilder(256);
>> int size = dp.Capacity;
>>
>> if (GetDefaultPrinter(dp, ref size))
>> {
>> Console.Write(String.Format("Printer: {0}, name
>> length{1}",
>> dp.ToString().Trim(), size));
>> }
>> else
>> {
>> Console.WriteLine("Error!");
>>
>> }
>> hPrinterDC = CreateDC("WINSPOOL", dp.ToString(), null,
>> IntPtr.Zero);
>>
>> // instantiate the delegate
>> EnumFontExDelegate del = new
>> EnumFontExDelegate(EnumFontFamExProc);
>> PrinterHelper.LOGFONT logFont =
>> PrinterHelper.CreateLogFont("");
>>
>> // call the win32 function
>> EnumFontFamiliesEx(hPrinterDC, ref logFont, del, IntPtr.Zero,
>> 0);
>> DeleteDC(hPrinterDC);
>> }
>>
>> public static int EnumFontFamExProc(ref
>> PrinterHelper.ENUMLOGFONTEX
>> lpelfe, IntPtr lpntme, int FontType, IntPtr lParam)
>> {
>> Console.WriteLine("Test Printer Name :" +
>> lpelfe.elfFullName);
>> return 1;
>> }
>> }
>>
>>
>> I would expect the EnumFontFamExProc will be called at least once or more
>> times but it's not. Do you have any clue why?
>>
>> cheers
>> Estee
>>
>>
>> "Michael Phillips, Jr." wrote:
>>
>> > Use a c# delegate to pass the callback function using p-invoke.
>> >
>> > Here is some example code to help you get started:
>> > http://www.codeproject.com/dotnet/C...arp_VB.asp
>> >
>> >
>> > "Estee Tan" wrote in message
>> > news:
>> > > Hi all,
>> > >
>> > > I am not very familiar with win32 api. How do you use
>> > > EnumFontFamiliesEx
>> > > in
>> > > C# when there is a call back in the argument?
>> > >
>> > > cheers
>> >
>> >
>> >





email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search