![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 4
| Drawing HBITMAP into CWnd, Acquired from "screenshot" 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;
}
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);
*/
}
}
Thank you |
| DeusAduro is offline | |
| | #2 | |
| Registered User Join Date: May 2009
Posts: 4
| Ok well I seem to have... 5 minutes later found a solution to my own problem. The only change I made was: Code: // Replace: CMemDC.FromHandle(hDC); // With: CMemDC.Attach(hDC); Quote:
| |
| DeusAduro is offline | |
| | #3 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 460
| FromHandle creates fills an the internal data structure to be compatible with and a copy of the platforms SDK's DC. CDC is just a wrapper around various DC functionality. In the first case you are returning a CDC that has been created from a DC, in the second case you are internally creating a DC on the current object from the handle. |
| valaris is offline | |
| | #4 |
| train spotter Join Date: Aug 2001 Location: near a computer
Posts: 3,356
| Is there a reason you are mixing MFC and WIN32? [HDC and CDC] You seem to be doing more work than required because of this mix. You can access the WIN32 GDI handle (HDC, HBITMAP etc) by casting the m_hObject member of any MFC CGdiObject. I use something like…. Code: CWnd *pWnd=NULL;
if (pWnd = CWnd::FindWindow(wind_name.c_str(),NULL))
{
CRect Area(&Rect);//init
if(!Area.Width() || !Area.Height())//validate
pWnd->GetWindowRect(Area);
//get the DC
CDC *pDC=NULL;
pDC = pWnd->GetWindowDC();
//free members if required
//TODO add free GDI resource code
//fill members
m_DC.CreateCompatibleDC(pDC);// CDC (is public in this example)
m_Bmp.CreateCompatibleBitmap(pDC, Area.Width(),Area.Height()); // CBitmap
//save original CBitmap to return our CDC to the default state before DeleteDC()
m_OrBmp=m_DC.SelectObject(&m_Bmp); // CBitmap *
//copy
m_DC.BitBlt(Area.left, Area.top, Area.Width(),Area.Height(),pDC,0,0, SRCCOPY );
//clean up
pWnd->ReleaseDC(pDC);
}
Code: void CBitmapWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Check if the simple bitmap is initialized.
if ( sBmp )
{
//get areas
CRect Area;
GetClientRect(Area,0);
CRect bmpRect;
bmpRect = sBmp->get_rect();
//Draw
dc.StretchBlt(0, 0, Area.Width(),Area.Height(),&sBmp.m_DC,0,0,bmpRect.Width()-1,bmpRect.Height()-1,SRCCOPY);
}
}
__________________ "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter." Friedrich Nietzsche "I spent a lot of my money on booze, birds and fast cars......the rest I squandered." George Best "If you are going through hell....keep going." Winston Churchill Last edited by novacain; 06-08-2009 at 12:37 AM. |
| novacain is offline | |
| | #5 |
| Registered User Join Date: Jun 2009
Posts: 5
| CDC::FromHandle in winapi I am trying to convert all my mfc codes to my own class (pure winapi). Since MFC is actually wrapping around the WinAPI. But I am having problem with CDC::FromHandle, there is no equivalent in WinAPI. I know, I can replace any call CDC::FromHandle and directly get the DC from ::GetDC, but it means I have to change all my code as well. Is there any way to replace CDC::FromHandle with pure WinAPI ? or any clue ? thx |
| binyo66 is offline | |
| | #6 | |
| train spotter Join Date: Aug 2001 Location: near a computer
Posts: 3,356
| Quote:
What EXACTLY are you trying to do? BitBlt()? just use the HDC. Change GDI objects? use SelectObject(), catching the released GDI object so you can clean up correctly. Without details it is hard to give a meaningful response. You appear to want to know how to construct a CDC (an MFC object) in pure WIN32, which is impossible. CDC::FromHandle(HDC ) takes a WIN32 handle to a DC (a HDC) and returns a pointer to an MFC class CDC, initialised with that DC. You can't use a CDC in pure WIN32. BTW anything with '::' in front of it is MFC not WIN32.
__________________ "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter." Friedrich Nietzsche "I spent a lot of my money on booze, birds and fast cars......the rest I squandered." George Best "If you are going through hell....keep going." Winston Churchill | |
| novacain is offline | |
| | #7 | |||
| Registered User Join Date: Jun 2009
Posts: 5
| Quote:
Quote:
![]() Quote:
Well you are probably right, it is not the right thread. again my apology for it (I just doesnt want to duplicate theread), and I will not attempt to reply this thread again, if u need to comment this post just pm me ). thx | |||
| binyo66 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| drawing on bitmaps | eth0 | Windows Programming | 2 | 03-24-2006 05:56 PM |
| HBITMAP in Class | Born_2B_Alone | Windows Programming | 1 | 10-09-2004 05:47 PM |