I'm new to codeguru and beginner with MFC.
Can anyone please explain, or point out to an internet resource that explains how to create, initialize, and maintain a memory device context that works as a local buffer for images? The idea is to maintain some large images in local DCs and bitmaps, and only bitblt them to screen in OnDraw().
What I did so far was was to define CDC and CBitmap objects as members of my View class, and then tried to initialize them with the sequence that begins at "// Initialize buffer". I placed that sequence in either OnInitialUpdate, or PreCreateWindow, or OnPrepareDC, or the view constructor, to no avail. The code always crashes in OnDraw, and I've noticed that the m_hDC member of myDevice is zero at that point.
Obviously, the initialization is wrong and MFC does (too many) things in the background which I'm not aware of.... My question was where can I read about that?
Thank you D_Drmmr.
The solution described in that example creates the DC object (CMemDC) and its bitmap within OnDraw, so they only exist and have to be updated within that scope. I'm looking for a solution that creates a persistent bitmap (and/or DC) that can be updated asynchronously between the calls to OnDraw.
Thanks, Cris.
Thank you D_Drmmr.
The solution described in that example creates the DC object (CMemDC) and its bitmap within OnDraw, so they only exist and have to be updated within that scope. I'm looking for a solution that creates a persistent bitmap (and/or DC) that can be updated asynchronously between the calls to OnDraw.
Thanks, Cris.
You can save the DC and bitmap as class members instead of as local variables. You can initialize them the first time OnDraw is called. It's really just a matter of copy-pasting some code from the example.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
The execution crashes at CreateCompatibleDC(pDC); if I remove that line it crashes at CreateCompatibleBitmap(...).
This happens ONLY when bmp and myDevice are defined as members of CMyView class, as in my first post.
If I use local variables (insert the two lines commented out above ASSERT_VALID(pDC)) everything WORKS fine.
There are other forces at work here....
With best regards,
Cris.
Last edited by Cristi; January 29th, 2013 at 09:18 AM.
The execution crashes at CreateCompatibleDC(pDC); if I remove that line it crashes at CreateCompatibleBitmap(...).
This happens ONLY when bmp and myDevice are defined as members of CMyView class, as in my first post.
If I use local variables (insert the two lines commented out above ASSERT_VALID(pDC)) everything WORKS fine.
There are other forces at work here....
OnDraw will be called hundreds of times. You don't need to create a DC and bitmap each time, but only once (since you keep them as member variables). So, the first thing is to check if the DC has been created already. You can simply do that using
Code:
if (!myDevice) {
// initialize DC and bitmap
myDevice.CreateCompatibleDC(pDC);
// etc.
}
Then you can copy the rest of the initialization from the CMemDC constructor in the link I posted, as well as the bit blitting from the destructor.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks