Hello everyone,
So I have my own "simpleBitmap" class which I use to acquire screenshots, and in turn I to a lot of low level manipulation of the pixels (the reason for making a class). Now I know my "screenshots" are working fine, I can save them to file, I can analyze individual pixels etc.
Now I want to be able to draw these simpleBitmap objects into a CWnd, however I want my class to remain MFC independant. Following is the code used to acquire the screenshot.
Code:
bool simpleBitmap::acquire( const string& wind_name, RECT rect )
{
cleanup_resources();
HWND hWnd = ::FindWindow(NULL,wind_name.c_str());
if (!hWnd)
return false;
// If the target rect is 0,0,0,0 take the whole window.
POINT start = {rect.left,rect.top};
if ( (rect.left == 0 && rect.right == 0) || (rect.top == 0 && rect.bottom == 0) )
{
RECT wndRect;
::GetWindowRect(hWnd,&wndRect);
rect = wndRect;
}
int w = rect.right-rect.left;
int h = rect.bottom-rect.top;
HDC hDc = ::GetWindowDC(hWnd);
hMemDc = CreateCompatibleDC(hDc);
hBmp = CreateCompatibleBitmap(hDc,w,h);
if ( !hDc || !hMemDc || !hBmp )
return false;
::SelectObject(hMemDc,hBmp);
::BitBlt(hMemDc,0,0,w,h,hDc,start.x,start.y,SRCCOPY);
::ReleaseDC(hWnd, hDc);
bmpRect = rect;
return true;
}
Now the member variables hMemDC, and hBmp are stored within the class. Here is my... attempt at drawing to a window. Note however that this custom CWnd class is drawn onto a dialog, I know the class is capable of drawing succesfully (I have used simple CDC drawing stuff like coloring the background white).
Code:
void CBitmapWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Check if the simple bitmap is initialized.
if ( sBmp )
{
// Get HDC and make sure the bitmap is selected into it.
HBITMAP hBmp = sBmp->get_hbitmap();
HDC hDC = sBmp->getSafeDC();
::SelectObject(hDC,hBmp);
// The rectangle describing the bitmap.
CRect bmpRect;
bmpRect = sBmp->get_rect();
// Convert to MFC CDC
CDC CMemDC;
CMemDC.FromHandle(hDC);
CRect wndRect;
this->GetClientRect(&wndRect);
//Draw
dc.StretchBlt(0,0,wndRect.Width(),wndRect.Height(),&CMemDC,0,0,bmpRect.Width()-1,bmpRect.Height()-1,SRCCOPY);
/*
// This call works, and draws the background white.
CBrush b(RGB(255,255,255));
dc.SelectObject(&b);
dc.Rectangle(wndRect);
*/
}
}
Sorry for the large amount of code, just trying to be complete in my question. So of course the question is... what would be the appropriate way to draw an HBITMAP/HDC acquired in the above manner to a simple CWnd class. I have looked around a lot, but there seem to be a huge number of combinations to draw Bitmaps, none of which have worked for me yet.
Thank you