GetDC function

"anonymous@discussions.microsoft.com" <lewix@discussions.microsoft.com>
wrote in message
news:5c171357-414c-4815-a105-05f8076d448d_WBRev3_@discussions.microsoft.com
> This post has been edited either by the author or a moderator in the
> Microsoft Forums: http://forums.microsoft.com Hi,
> i hope i'm not posting a newbie question.
> I have to get current DC handler of my window dialog, so (as
> suggested on most forums on the net) i use GetDC() function.
> It is explained (in MSDN site too) GetDC has one argument: HWND hWnd.
> But if i type
> HDC hDC = GetDC(m_hWnd)
> on my project, the compiler returns this error: "GetDC function
> doesn't accepts arguments".
> In fact the intellisense of my Visual Studio .net (2003) suggests me:
> "this->GetDC()",         not "this->GetDC(HWND hWnd)"
MFC's CWnd class (from which your dialog class is ultimately derived) is a wrapper around HWND, and holds an HWND as a member variable. For almost every Win32 API of the form SomeFunction(HWND hwnd, moreParameters), CWnd has a matching member function SomeFunction(moreParameters), which most of the time simply forwards to the API passing m_hWnd as the first parameter.
 
So, inside a method of CWnd-derived class, GetDC refers to CWnd::GetDC(). If for some reason you insist on calling the raw API function, you can do it with ::GetDC(someHWND). Note that CWnd::GetDC() simply calls ::GetDC(m_hWnd), which is likely what you want anyway.
--
With best wishes,
    Igor Tandetnik


Answer this question

GetDC function

  • trixter

    "anonymous@discussions.microsoft.com" <lewix@discussions.microsoft.com>
    wrote in message
    news:688985c4-d678-4309-84a9-dfbb5d58ec8d_WBRev2_@discussions.microsoft.com
    > Thanx a lot!, [:)]
    > i have just found out the solution me too (calling ::GetDC(m_hWnd),
    > but you explained the "why" of this problem.
    > I had to use ::GetDC(HWND hWnd) because this function returns HDC
    > variable that i  will need on my application.
    > CWnd::GetDC(void) returns pointer to CDC [:S]
    Just as CWnd is a wrapper around HWND, CDC is a wrapper around HDC. It has m_hDC member variable, and for each Win32 API function of the form SomeFunction(HDC, moreParams), CDC has a member function SomeFunction(moreParams). You rarely need an access to raw HWND and raw APIs manipulating HWND when using CWnd-derived classes. By the same token, you rarely need an access to raw HDC and raw APIs manipulating HDC when using CDC or CDC-derived classes. This is the whole point of using a framework like MFC in the first place.
    --
    With best wishes,
        Igor Tandetnik

  • fanse

    I show you the code where i need to use HDC variable (i have to change dialog background image, using FreeImage library dll):

    BOOL CLewixXpDlg::OnEraseBkgnd(CDC* pDC)
    {
    .......................
    .........................................
        // TODO: aggiungere qui il codice per la gestione dei messaggi e/o chiamare il codice predefinito.
        HINSTANCE hFI = LoadLibrary("FreeImage.dll");

        if (hFI==NULL)
            return CDialog::OnEraseBkgnd(pDC);
    ...........................
    .........................
    .............
          /*if functions linked don't point NULL...*/
        {
            CRect rcDest;
            GetClientRect(rcDest);

            FIBITMAP *dib = pFreeImage_Load(FIF_PNG, "c:\\push.png", 0);

            SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
            StretchDIBits(pDC->m_hDC, rcDest.left, rcDest.top,
                                rcDest.right-rcDest.left, rcDest.bottom-rcDest.top,
                                0, 0, pFreeImage_GetWidth(dib), pFreeImage_GetHeight(dib),
                                pFreeImage_GetBits(dib), pFreeImage_GetInfo(dib),
                               DIB_RGB_COLORS, SRCCOPY);

            pFreeImage_Unload(dib);
        }

        FreeLibrary(hFI);
        return    TRUE;
    }

    It's works, i have still to check it but i hope there aren't critical errors...

    Lewix

  • SSCIT

    Yes,
    you're right.

    Sorry but i'm working with MFC for few months, but i like it, it isn't easy but it is very coherent framework.
    Thanx for explaining, really.

    Lewix

  • shumona

    Yes, "easy to use" and quick GUI building i think sure they don't belong to MFC

    WTL I noticed some discussion about it some forum.
    Wow... WTL is also opensource (great sourceforge).
    Thanx for the links you give me; now i'm on wtl home page....
    Mmmm.. good. Smile.. i want know more about this project (there is a lot of documentation on codeproject sitem, thank you).

    Ok, you'll have news about opinion, thanx for your help on this thread.

    Lewix

  • Konigmann

    Hi,
    i hope i'm not posting a newbie question.
    I have to get current DC handler of my window dialog, so (as suggested on most forums on the net) i use GetDC() function.
    It is explained (in MSDN site too) GetDC has one argument: HWND hWnd.
    But if i type
    HDC hDC = GetDC(m_hWnd)
    on my project, the compiler returns this error: "GetDC function doesn't accepts arguments".
    In fact the intellisense of my Visual Studio .net (2003) suggests me:
    "this->GetDC()",         not "this->GetDC(HWND hWnd)"

    Why this happens What is the solution

    Thanx to anybody can help me.

    Lewix

    P.S.: my project is a MFC application, standard single dialog, dll shared.

  • donaldg

    "anonymous@discussions.microsoft.com" <lewix@discussions.microsoft.com>
    wrote in message
    news:c5cbaa61-f087-405e-ac5e-0fa73189c7b7@discussions.microsoft.com 
    > I show you the code where i need to use HDC variable (i have to
    > change dialog background image, using FreeImage library dll):
    >
    >         SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
     
    Equivalently,
     
    pDC->SetStretchBltMode(COLORONCOLOR);

    >         StretchDIBits(pDC->m_hDC, ...);
     
    Yes, StretchDIBits is one of the rare Win32 API functions that are not wrapped by MFC. That's why CDC exposes a raw HDC data member.
    --
    With best wishes,
        Igor Tandetnik

  • JayZee

    "anonymous@discussions.microsoft.com" <lewix@discussions.microsoft.com>
    wrote in message
    news:15d3d04e-6da2-4f19-a6c5-1458418dff35_WBRev1_@discussions.microsoft.com
    > Sorry but i'm working with MFC for few months, but i like it, it
    > isn't easy but it is very coherent framework.
    What follows is my personal opinion. There is quite a number of developers whose opinion differs.
     
    You can perhaps describe MFC as coherent, but I am more interested in the characteristics it lacks (IMHO) - convenient, easy to use, flexible, extensible. I had my share of fighting against MFC and I would not touch it again with a ten foot long pole. For a good description of some of the shortcomings of MFC's design, see
     
     
    I suggest looking into WTL (http://wtl.sf.net) - check out some articles at http://wtl.sf.net/links.htm . You might be particularly interested in "WTL for MFC Programmers" series (8 articles in total)
     
    ...
     
    Full disclosure: I am a developer on WTL project, so I'm obviously biased.
    --
    With best wishes,
        Igor Tandetnik

  • jmsigler2

    Thanx a lot!, Smile
    i have just found out the solution me too (calling ::GetDC(m_hWnd), but you explained the "why" of this problem.
    I had to use ::GetDC(HWND hWnd) because this function returns HDC variable that i  will need on my application.
    CWnd::GetDC(void) returns pointer to CDC Tongue Tied
    If exist other solutions to get hDC from a dialog tell me of course!!

    Thanx again

    Lewix



  • LinCar

    I didn't think to use some member variable (in this case m_hDC) because i often work with classes without direct access to member variable but by Get/Set methods.
    I have to be more "open mind" :)

    Thanx again!

    Lewix

  • GetDC function