public void GetFonts() { LOGFONT lf = CreateLogFont(""); IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(lf)); Marshal.StructureToPtr(lf, plogFont, true); int ret = 0; try { //If anyone knows of a better way to get the pointer please let me know var pictureBox = new PictureBox(); var graphic = pictureBox.CreateGraphics(); var ptr = graphic.GetHdc(); del1 = new EnumFontExDelegate(callback1); ret = EnumFontFamiliesEx(ptr, plogFont, del1, IntPtr.Zero, 0); graphic.ReleaseHdc(ptr); } catch { Console.WriteLine("Error!"); } finally { Marshal.DestroyStructure(plogFont, typeof(LOGFONT)); } }
public Win32FontSystem(Control control) { // Get the list of font families. LOGFONT lf = CreateLogFont(); IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(lf)); Marshal.StructureToPtr(lf, plogFont, true); using (Graphics g = control.CreateGraphics()) { try { IntPtr hDC = g.GetHdc(); EnumFontExDelegate callback = delegate(ref ENUMLOGFONTEX lpelfe, ref NEWTEXTMETRICEX lpntme, int FontType, int lParam) { try { // For now, just get the western font names.. if (lpelfe.elfScript == "Western") { _familyLogFonts[lpelfe.elfFullName] = lpelfe.elfLogFont; } } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.ToString()); } return(1); }; EnumFontFamiliesEx(hDC, plogFont, callback, IntPtr.Zero, 0); _familyNames = _familyLogFonts.Keys.ToList(); g.ReleaseHdc(hDC); } catch { MessageBox.Show("Error enumerating Win32 fonts"); Debug.Assert(false); } finally { Marshal.DestroyStructure(plogFont, typeof(LOGFONT)); } } }
private static extern int EnumFontFamiliesEx(IntPtr hdc, [In] IntPtr pLogfont, EnumFontExDelegate lpEnumFontFamExProc, IntPtr lParam, uint dwFlags);
public static LOGFONT GetLOGFONT(string fontName, bool bold, bool italic) { if (string.IsNullOrEmpty(fontName)) { fontName = "Arial"; } LOGFONT lf = new LOGFONT(); lf.lfCharSet = 1; lf.lfFaceName = fontName; if (italic) { lf.lfItalic = 255; } if (bold) { lf.lfWeight = 700; } else { lf.lfWeight = 400; } IntPtr plogFont = Marshal.AllocHGlobal(Marshal.SizeOf(lf)); Marshal.StructureToPtr(lf, plogFont, true); LOGFONTFinder finder = new LOGFONTFinder(); try { IntPtr HDC = Win32.GetDC(IntPtr.Zero); EnumFontExDelegate del = new EnumFontExDelegate(finder.EnumFontFamExProc); int ret = 0; ret = EnumFontFamiliesEx(HDC, plogFont, del, IntPtr.Zero, 0); //System.Diagnostics.Trace.WriteLine("EnumFontFamiliesEx = " + ret.ToString()); Win32.ReleaseDC(IntPtr.Zero, HDC); } catch (System.Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { Marshal.DestroyStructure(plogFont, typeof(LOGFONT)); } bool find; LOGFONT result = finder.Find(lf, out find); if (find) { return(result); } else { if (fontName.Equals("Arial")) { throw new PDFException("System fonts are not available."); } } return(GetLOGFONT("Arial", bold, italic)); }