Solved

Memory DC, How?

Posted on 2003-11-21
6
680 Views
Last Modified: 2008-03-17
I've been though a ton of sample source, and still am so confused on this.  What I need to do seems so simple, yet I cannot seem to get the syntax down.

I have two DCs, hCurrentPage and hNextPage. Both DCs have the same dimensions.  Here is what I need to do.

1.  Create a memory DC
        //set drawing rectangle to the dimensions of the pages
        CRect rcDraw(0,0,sizeImage.cx,sizeImage.cy);
       
        //create the memory DC
        HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
        HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
        HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen,
             GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES));
       
        SelectObject(hdcCompatible, hbmScreen);

2.  Copy the content of hNextPage into a memory DC using BitBlt SRCCOPY
        ::BitBlt(hdcCompatible, 0,0, rcDraw.Width(), rcDraw.Height()
                , hNextPage, 0, 0, SRCCOPY);

3.  Copy the content of hCurrentPage into the memory DC using BitBlt SRCCOPY
        //In the actual program I deflate the rectangle, but for clarity I have ommitte that code
        ::BitBlt(hdcCompatible,rcDraw.left,rcDraw.top,rcDraw.Width(),rcDraw.Height()
                , hCurrentPage,rcDraw.left,rcDraw.top,SRCCOPY);

4.  Copy the combined content in the memory DC to hCurrentPage  using BitBlt SRCCOPY
        ::BitBlt(hCurrentPage,rcNew.left,rcNew.top,rcNew.Width(),rcNew.Height()
                , hdcCompatible, rcNew.left, rcNew.top, SRCCOPY);

5.  Delete the memory DC
       DeleteDC(hdcCompatible);


What am I doing wrong?  If it makes any difference, the above code is in a loop to perform a transition between hCurrentPage and hNextPage.

Thanks.
0
Comment
Question by:BWarmuskerken
  • 3
  • 3
6 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 250 total points
ID: 9797368
Hi there,

First of all: in what function are you calling this loop? Is it part of a message handling for a know window?

Depending on this knowlegde one could decide to get a compatible DC for *that* window. For me it is not fully clear, that hCurrentPage, hNextPage and hdcCompatible are all compatible. The first 2 are but the third perhaps not.

Since like this I would suggest to create the compatible bitmap from hNextPage and copy that content as well:
(relating to http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20803968.html)

   HDC hdcCompatible = CreateCompatibleDC (hNextPage);
   HBITMAP hbmDC = CreateCompatibleBitmap (hNextPage);
   ::BitBlt (hdcCompatible, 0, 0, rcDraw.Width(), rcDraw.Height(), hNextPage, 0, 0, SRCCOPY);

   Do steps 3,4,5 now.
0
 
LVL 1

Author Comment

by:BWarmuskerken
ID: 9797580
Hi SteH,

Thanks for the help, again.  I guess I didn't realize I could use CreateCompatibleDC on the DC I already had.  I have to admit that I feel a little stupd now.  Hopefully, I won't be back to ask any more questions, but if I do, I know who to ask now.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9798141
I think everone is starting with question which turn out to be stupid if one knows more. Thats just learning.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:BWarmuskerken
ID: 9798225
SteH,  

I know this is after the fact, but...

Im doing this in a For loop

for ()
{
HDC hdcCompatible = CreateCompatibleDC (hNextPage);
   HBITMAP hbmDC = CreateCompatibleBitmap (hNextPage);
   ::BitBlt (hdcCompatible, 0, 0, rcDraw.Width(), rcDraw.Height(), hNextPage, 0, 0, SRCCOPY);
   // Other code here to BitBlt

  DeleteDC(hdcCompatible);
}

Is creating and deleteing the object going to cause me hassles?
0
 
LVL 13

Expert Comment

by:SteH
ID: 9798253
It should not harm if all memory is properly released.

You could change it to:

HDC hdcCompatible = CreateCompatibleDC (hNextPage);
HBITMAP hbmDC = CreateCompatibleBitmap (hNextPage);
for ()
{
   ::BitBlt (hdcCompatible, 0, 0, rcDraw.Width(), rcDraw.Height(), hNextPage, 0, 0, SRCCOPY);
   // Other code here to BitBlt
}
DeleteDC(hdcCompatible);

What could be a problem is that the content of hNextPage and hCurrentPage change during the loop and the starting point in the second iteration might not be as desired. Perhaps you can store the images after each iteration and have a look at them.



0
 
LVL 1

Author Comment

by:BWarmuskerken
ID: 9798298
Putting those lines outside of the loop made all the difference in the world.  It's smoothe.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

22 Experts available now in Live!

Get 1:1 Help Now