Example #1
0
 private void PrepareDefaultStringFormat()
 {
     if (defaultStringFormat == IntPtr.Zero)
     {
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipStringFormatGetGenericDefault(out defaultStringFormat));
     }
 }
Example #2
0
 private void PrepareGraphics()
 {
     if (graphics == IntPtr.Zero)
     {
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipCreateFromHDC(hdc, out graphics));
     }
 }
Example #3
0
        public void Dispose()
        {
            if (graphics != IntPtr.Zero)
            {
                GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteGraphics(graphics));
                graphics = IntPtr.Zero;
            }

            if (defaultStringFormat != IntPtr.Zero)
            {
                GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteStringFormat(defaultStringFormat));
                graphics = IntPtr.Zero;
            }

            if (originalPen != IntPtr.Zero)
            {
                Gdi32API.SelectObjectChecked(hdc, originalPen);
            }

            if (originalBrush != IntPtr.Zero)
            {
                Gdi32API.SelectObjectChecked(hdc, originalBrush);
            }

            if (originalFont != IntPtr.Zero)
            {
                Gdi32API.SelectObjectChecked(hdc, originalFont);
            }
        }
Example #4
0
 private void DrawStringGDIPlus(Color color, FontConfig font, int x, int y, string text)
 {
     PrepareGraphics();
     PrepareDefaultStringFormat();
     GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipCreateFontFamilyFromName(font.FontFamily, IntPtr.Zero, out var fontFamilyPtr));
     try
     {
         FontStyle fontStyle = GdiPlusAPI.GetFontStyle(font);
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipCreateFont(fontFamilyPtr, font.Size, fontStyle, Unit.UnitPixel, out var fontPtr));
         try
         {
             GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipCreateSolidFill(color.ToArgb(), out var brush));
             try
             {
                 RectF rect = new RectF(x, y, 0, 0);
                 GdiPlusAPI.GdipDrawString(graphics, text, text.Length, fontPtr, ref rect, defaultStringFormat, brush);
             }
             finally
             {
                 GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteBrush(brush));
             }
         }
         finally
         {
             GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteFont(fontPtr));
         }
     }
     finally
     {
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteFontFamily(fontFamilyPtr));
     }
 }
Example #5
0
 private void FillRectangleGDIPlus(Color color, int x, int y, int width, int height)
 {
     PrepareGraphics();
     GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipCreateSolidFill(color.ToArgb(), out var brush));
     try
     {
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipFillRectangleI(graphics, brush, x, y, width, height));
     }
     finally
     {
         GdiPlusAPI.CheckStatus(GdiPlusAPI.GdipDeleteBrush(brush));
     }
 }