Click to See Complete Forum and Search --> : How to save a copy of CDC for later use?


May 6th, 1999, 09:58 AM
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);
}

beaglebuddy
May 6th, 1999, 11:10 AM
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()
:dc(GetDC())
{
dc->SelectObject(font);
dc->SetTextColor(txtColor);
dc->SetBkColor(bkColor);
}

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

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

May 6th, 1999, 03:50 PM
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()).

beaglebuddy
May 6th, 1999, 05:22 PM
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.

Paul McKenzie
May 6th, 1999, 05:49 PM
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

sally
May 6th, 1999, 08:26 PM
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

Sally
May 6th, 1999, 08:26 PM
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

Paul McKenzie
May 7th, 1999, 04:43 AM
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