What I'm trying to do is pop-up a bitmap, present it for some period of
time, then remove it.
My application runs in the background and therefore does not have a visible
window. I just put the bitmap on top of whatever happens to be displayed.
After a period of time, I want the bitmap to go away and the windows that it
covered to be redrawn.
Here's the code I use to draw the bitmap:
{
HDC hdc;
hBmp = SHLoadDIBitmap(TEXT("\\smiley.bmp"));
hdc = GetDC(NULL);
CopyBitmap(hdc, hBmp);
DeleteObject(hBmp);
ReleaseDC(NULL, hdc);
}
void CopyBitmap(HDC hdc, HBITMAP hBitmap)
{
// Create a DC that matches the device
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld;
BITMAP bmp;
// Select the bitmap into the compatible device context
hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
// Get the bitmap dimensions from the bitmap
GetObject(hBitmap, sizeof(bmp), &bmp);
// Copy the bitmap image from the memory DC to the screen DC
StretchBlt( hdc,
13, 70, 213, 180,
hdcMem,
0, 0, bmp.bmWidth, bmp.bmHeight,
SRCCOPY);
// Restore original bitmap selection and destroy the memory DC
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
return;
}
I've tried doing an InvalidateRect(GetForegroundWindow(), NULL, FALSE) and
UpdateWindow(GetForegroundWindow()) but that didn't do it. The bitmap just
stays there.
Any thoughts or suggestions on how to redraw the area the bitmap covers?
Mac
A few comments:
#1 You're just drawing wherever you please. The reason you haven't
seen a discussion of this issue is because people don't usually do
this. You need to understand the implication of this before you rely
on this mechanism. There's no guarantee that your bitmap will not be
drawn over by the running app.
#2 When you're using UpdateWindow() you're setting the "erase
background" flag to false. This should be true if you want the app to
erase everything and repaint.
#3 If you really want a "save bits" routine, you should use GetPixel to
save the background, draw your bitmap, then restore the bits you saved.
However, the caveat in #1 still applies. The bits you restore might
not be valid for the running app.
A better way to do this might be to create a borderless window, draw
the bitmap in *your* window, and hide or destroy the window when you're
done with it.
-Dan.
Thanks for the suggestions. The "better way" you suggested is definitely
the way to go. I've already coded it up (it wasn't as bad as I thought it
would be) and it's working very nicely. I initially didn't want to do my own
window since my app is hidden, but I can see now that it was an unnecessary
concern.
Thanks again.
Mac
I am also trying to dispaly a bitmap image into a window & able to do that
as per your discussion. But im facing memory loss.
My resource relesase sequence is...
ReleaseDC();
DeleteObject();
DeleteDC();
DestroyWindow(); //fails with error invalid handle.
I tried to destroy the created window but DestroyWindow() fails with error
invalid handle.
Can u people plz tell what should be resource release sequence. Thanks in
advance.