示例#1
0
        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));
        }