CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    GDI+ and Flat API

    I am trying to figure out how to call a GDI+ Flat Api function, but cannot get anything working.

    Here is my code:
    Code:
    #include <windows.h>
    #include <objidl.h>
    #include <gdiplus.h>
    #include <GdiplusFlat.h>
    using namespace Gdiplus;
    #pragma comment (lib,"Gdiplus.lib")
    
    VOID OnPaint(HDC hdc)
    {
    	Graphics graphics(hdc);
    	Pen      pen(Color(255, 0, 0, 255));
    	//graphics.DrawLine(&pen, 0, 0, 200, 100);
    	GdipDrawLine(&graphics, &pen, 0, 0, 200, 0); //Flat Api version
    }
    Here I am trying to call only one flat function GdipDrawLine.

    Any suggestions?
    Last edited by mmscg; December 8th, 2011 at 11:08 PM.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GDI+ and Fat API

    • Once included Gdiplus, use DllExports namespace i.e. DllExports::GdipDrawLine.
    • First two arguments of DllExports::GdipDrawLine are not of type Gdilpus::Graphics and Gdiplus::Pen, but GpGraphics and GpPen.
      Code:
      GpStatus WINGDIPAPI
      GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1, REAL y1,
                            REAL x2, REAL y2);
      None has implicit conversion from one to another.
    • It has no sense in a C++ program to directly use flat GDI+ APIs.
      Generally, GDI+ classes methods are simple inline wrappers, e.g.
      Code:
          Status DrawLine(IN const Pen* pen,
                          IN INT x1,
                          IN INT y1,
                          IN INT x2,
                          IN INT y2)
          {
              return SetStatus(DllExports::GdipDrawLineI(nativeGraphics,
                                                         pen->nativePen,
                                                         x1,
                                                         y1,
                                                         x2,
                                                         y2));
          }
    • Given the above points, you have to choose one of two:
      1. Use only GDI+ classes, recommended in C++ code;
      2. Use GDI+ flat API only in C code (as well as in other languages, except C++).
    Last edited by ovidiucucu; December 8th, 2011 at 06:04 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Apr 2004
    Posts
    204

    Re: GDI+ and Flat API

    So are you are saying the Flat Api functions cannot be used in C++,
    or just that using the wrapper method is easier?

    I added

    using namespace Gdiplus:: DllExports;

    to my code and I now get this error:

    error C2664: 'Gdiplus:llExports::GdipDrawLine' : cannot convert parameter 1 from 'Gdiplus::Graphics *__w64 ' to 'Gdiplus::GpGraphics *'

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GDI+ and Flat API

    Quote Originally Posted by mmscg View Post
    So are you are saying the Flat Api functions cannot be used in C++,
    or just that using the wrapper method is easier?
    I have never said "cannot". You can use flat GDI+ API in C++ code but it's like scratching with the foot.
    Yes, the (wrapper) C++ classes are easier to use in C++ code. For that purpose they have been designed.
    Quote Originally Posted by mmscg View Post
    I added

    using namespace Gdiplus:: DllExports;

    to my code and I now get this error:

    error C2664: 'Gdiplus:llExports::GdipDrawLine' : cannot convert parameter 1 from 'Gdiplus::Graphics *__w64 ' to 'Gdiplus::GpGraphics *'
    Have you ever read this?
    Quote Originally Posted by ME View Post
    • First two arguments of DllExports::GdipDrawLine are not of type Gdilpus::Graphics and Gdiplus::Pen, but GpGraphics and GpPen.
      Code:
      GpStatus WINGDIPAPI
      GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1, REAL y1,
                            REAL x2, REAL y2);
      None has implicit conversion from one to another.
      ...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Apr 2004
    Posts
    204

    Re: GDI+ and Fat API

    Yes, I had read that, but still being fairly new to C++ I find this very confusing.

    Can one type be cast to another then?

    I have been using GDI+ in my VB6 projects for the last year, and have become fairly proficient with the Flat Apis.

    As I continue to learn C++, and start to port some of my projects from VB6 to C++,
    I thought it might be easier for me to continue to use the Flat Api methodology for the GDI+ conversion.

    Also now, even for just curiosity sake, I would like to see how to draw a line in C++ using flat GDI+ api.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GDI+ and Fat API

    Well, here is an example of using GDI+ flat API.
    Code:
       using namespace DllExports;
    
       GpPen* pen = NULL;
       GpGraphics* graphics = NULL;
       ARGB color = 255 << ALPHA_SHIFT | 255 << BLUE_SHIFT;
       GpUnit unit = UnitPixel;
       REAL width = 1.0;
    
       GdipCreatePen1(color, width, unit, &pen);
       GdipCreateFromHDC(hdc, &graphics);
       GdipDrawLine(graphics, pen, 0, 0, 100, 50);
       // ...
       GdipDeletePen(pen);
       GdipDeleteGraphics(graphics);
    Resources
    Last edited by ovidiucucu; December 10th, 2011 at 01:14 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Apr 2004
    Posts
    204

    Re: GDI+ and Flat API

    Thank you... you C++ guys are amazing!!

    This works perfectly for me, and should be enough to get me on my way.
    (I'm sure I'll hit a few stumbling blocks, and might have to ask more questions).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured