Exemple #1
0
        Create(
            string strFace,
            int iSize,
            int degrees,
            IntPtr hdc)
        {
            int cyDevice_Res = GdiGraphics.GetDeviceCaps(hdc, CAPS.LOGPIXELSY);

            // Calculate font height.
            float flHeight = ((float)iSize * (float)cyDevice_Res) / 72.0F;
            int   iHeight  = (int)(flHeight + 0.5);

            // Set height negative to request "Em-Height" (versus
            // "character-cell height" for positive size)
            //iHeight = iHeight * (-1);

            // Allocate managed code logfont structure
            LOGFONT logfont = new LOGFONT();

            logfont.lfHeight         = iHeight;
            logfont.lfWidth          = 0;
            logfont.lfEscapement     = degrees * 10;
            logfont.lfOrientation    = 0;
            logfont.lfWeight         = 0;
            logfont.lfItalic         = 0;
            logfont.lfUnderline      = 0;
            logfont.lfStrikeOut      = 0;
            logfont.lfCharSet        = 0;
            logfont.lfOutPrecision   = 0;
            logfont.lfClipPrecision  = 0;
            logfont.lfQuality        = 0;
            logfont.lfPitchAndFamily = 0;

            // Allocate unmanaged code logfont structure.
            int    cbLogFont   = Marshal.SizeOf(logfont);
            int    cbMem       = cbLogFont + LF_FACESIZE;
            IntPtr iptrLogFont = NativeHeap.LocalAlloc(NativeHeap.LPTR, cbMem);

            if (iptrLogFont == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            // Copy managed structure to unmanaged buffer
            Marshal.StructureToPtr(logfont, iptrLogFont, false);

            // Set pointer to end of structure
            IntPtr ipFaceDest = (IntPtr)((int)iptrLogFont + cbLogFont);

            // Copy string to a character array.
            char [] achFace = strFace.ToCharArray();
            int     cch     = strFace.Length;

            // Copy facename to unmanaged buffer
            Marshal.Copy(achFace, 0, ipFaceDest, cch);

            return(CreateFontIndirect(iptrLogFont));
        }
Exemple #2
0
        Create(
            IntPtr hdcDevice,
            string strFace,
            float sinSize,
            int degrees)
        {
            // Calculate font height based on this ratio:
            //
            //    Height in Pixels       Desired Point Size
            //  -------------------  =   ------------------
            //   Device Resolution              72
            //
            // (72 point = approx. 1 inch.)
            //
            // Which results in the following formula:
            //
            // Height = (Desired_Pt * Device_Res) / 72
            //
            IntPtr hfont = IntPtr.Zero;

            float sinDevice_Res;

            sinDevice_Res = (float)GdiGraphics.GetDeviceCaps(
                hdcDevice, GdiGraphics.LOGPIXELSY);

            // Calculate font height.
            float flHeight = (sinSize * sinDevice_Res) / 72.0F;
            int   iHeight  = (int)(flHeight + 0.5);

            // Set height negative to request "Em-Height" (versus
            // "character-cell height" for positive size)
            iHeight = iHeight * (-1);

            // Allocate managed code logfont structure
            LOGFONT logfont = new LOGFONT();

            logfont.lfHeight         = iHeight;
            logfont.lfWidth          = 0;
            logfont.lfEscapement     = degrees * 10;
            logfont.lfOrientation    = 0;
            logfont.lfWeight         = 0;
            logfont.lfItalic         = 0;
            logfont.lfUnderline      = 0;
            logfont.lfStrikeOut      = 0;
            logfont.lfCharSet        = 0;
            logfont.lfOutPrecision   = 0;
            logfont.lfClipPrecision  = 0;
            logfont.lfQuality        = 0;
            logfont.lfPitchAndFamily = 0;

            // Allocate unmanaged code logfont structure.
            int    cbLogFont   = Marshal.SizeOf(logfont);
            int    cbMem       = cbLogFont + LF_FACESIZE;
            IntPtr iptrLogFont = NativeHeap.LocalAlloc(
                NativeHeap.LPTR, cbMem);

            if (iptrLogFont == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            // Copy managed structure to unmanaged buffer
            Marshal.StructureToPtr(logfont, iptrLogFont, false);

            // Set pointer to end of structure
            IntPtr iptrFaceDest = (IntPtr)((int)iptrLogFont +
                                           cbLogFont);

            // Copy string to a character array.
            char [] achFace = strFace.ToCharArray();
            int     cch     = strFace.Length;

            if ((cch + 2) > LF_FACESIZE)
            {
                cch = LF_FACESIZE;
            }

            // Copy facename to unmanaged buffer
            Marshal.Copy(achFace, 0, iptrFaceDest, cch);

            hfont = CreateFontIndirect(iptrLogFont);

            NativeHeap.LocalFree(iptrLogFont);

            return(hfont);
        }