CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Guest

    How to save a copy of CDC for later use?

    I am trying to draw text on the screen in a fast speed. The drawing is triggered by a timer constantly.
    In the painting routine, I have to get a DC, select fonts and colors, and then release the DC and unselect the fonts.
    This is done everytime the timer is triggered, which makes the drawing way too slow.
    I am wondering if there is a way the CDC can be saved and used later. Thanks.

    Here is what I am doing now:

    void MyClass::OnTimer(...)
    {
    PaintText();
    }

    void MyClass::PaintText()
    {
    CDC *pDC=GetDC();
    CFont *pOld;
    pOld = pDC->SelectObject(font);
    pDC->SetTextColor(txtColor);
    pDC->SetBkColor(bkColor);

    pDC->TextOut(......);

    pDC->SelectObject(pOld);
    ReleaseDC(pDC);
    }


  2. #2
    Join Date
    Apr 1999
    Location
    Scottsdale, Arizona
    Posts
    28

    Re: How to save a copy of CDC for later use?

    What if you made the dc a data member of your class?
    If the drawing function is triggered by a timer, then I think this method might work. If it is called as a result of a WM_PAINT message, then I don't think it will.


    class MyClass
    {
    private:
    CDC *dc;
    };

    // constructor
    MyClass::MyClass()
    c(GetDC())
    {
    dc->SelectObject(font);
    dc->SetTextColor(txtColor);
    dc->SetBkColor(bkColor);
    }

    // destructor
    MyClass::~MyClass()
    {
    ReleaseDC(dc);
    }

    // drawing function
    void MyClass::PaintText()
    {
    dc->TextOut(......);
    }




  3. #3
    Guest

    Re: How to save a copy of CDC for later use?

    Beaglebuddy, Thanks for the posting.
    It doesn't work and I don;t know why.

    My app is SDI with a CFormView. The timing and drawing are carried out within the View class.
    It does work when Get/ReleaseDC within the drawing function (not the default OnDraw()).



  4. #4
    Join Date
    Apr 1999
    Location
    Scottsdale, Arizona
    Posts
    28

    Re: How to save a copy of CDC for later use?

    According to the on-line documentation for CWnd::GetDC()

    The pointer may be temporary and should not be stored for later use.

    Thus, it may not be possible to save the dc for later use. You may no choice but to create it and release it each time in your drawing function. The documentation doesn't say under what conditions the dc returned by GetDC() is permanent or temporary, and thus there's no way for you to know.


  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to save a copy of CDC for later use?

    This may be tricky, but if you change the window class style to include CS_OWNDC, the drawing should speed up. Look up "Private Device Contexts" in the on-line help for more info.

    The tricky part is that if the window is of the MFC variety, you will need to override PreCreateWindow() so that you can get the WNDCLASS structure and set the style member to be CS_OWNDC. To get the class, you'll need to call GetClassInfo(), set the WNDCLASS style member, and call RegisterClass() to register the new window class.

    BTW, the WNDCLASS structure and the members are described in Chapter *1* of most WIN API textbooks for beginners. This is one case where Win API programming is *way* easier than MFC!

    Regards,

    Paul McKenzie



  6. #6
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How to save a copy of CDC for later use?

    There is a limit number of DCs in the Windows environment, and it is not a good neighbour approach to hug one of them for the lifetime of your application

    Sally


  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to save a copy of CDC for later use?

    The DC limit is not imposed in Windows NT 32-bit programs. I'm not sure about Win95/Win98 though.

    Definitely there is a limit in 16-bit code.

    Regards,

    Paul McKenzie


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