Global GDI+ Graphics object

Posted on 2006-05-26
Last Modified: 2008-02-01

I tried to create a global graphics object but it is not working:

HDC *graHDC;
Graphics graphics(graHDC);

//OnPaint fuction
VOID OnPaint(Graphics* g){

//IN WinMain
graHDC = GetDC(hWnd);


It give this error :
error C2664: 'OnPaint' : cannot convert parameter 2 from 'class Gdiplus::Graphics' to 'class Gdiplus::Graphics *'  No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe.

Or maybe graphics cannot be create in this way?

Question by:iphone
    1 Comment
    LVL 48

    Accepted Solution

    To fix compilation error you need to write:

    But I don't think that this will work at runtime. Graphics object must be created in every WM_PAINT message handler by the following way:

    LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        PAINTSTRUCT ps;
        HDC hdc;
        Graphics* pGraphics;
        switch (message)
            case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);
                pGraphics = Graphics::FromHDC(hdc);
                delete pGraphics;
                EndPaint(hwnd, &ps);
                return 0L;

            // Process other messages.  

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now