Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HBITMAP WindowToBitmap(HWND hwnd,RECT* dims=NULL){
- HDC hdcWnd,hdcBitmap;
- int w=100,h=100;
- HBITMAP hbmBitmap;
- HBITMAP hbmOld;
- hdcWnd=GetDC(hwnd);
- if(hdcWnd){
- RECT wnd;
- if(dims==NULL){
- GetClientRect(hwnd,&wnd);
- }
- else{
- CopyRect(&wnd,dims);
- }
- w=wnd.right;
- h=wnd.bottom;
- hbmBitmap=(HBITMAP)CreateCompatibleBitmap(hdcWnd,w,h);
- }
- if(!hbmBitmap){
- if(hdcWnd){
- RECT wnd;
- GetWindowRect(hwnd,&wnd);
- w=wnd.right;
- h=wnd.bottom;
- hbmBitmap=CreateCompatibleBitmap(hdcWnd,w,h);
- }
- }
- if(!hbmBitmap){
- std::cout<<"Bitmap missing! "<<std::endl;
- }
- hdcBitmap=CreateCompatibleDC(hdcWnd);
- hbmOld=(HBITMAP)SelectObject((HDC)hdcBitmap,(HGDIOBJ)(HBITMAP)hbmBitmap);
- BitBlt(hdcBitmap,0,0,w,h,hdcWnd,0,0,SRCCOPY);
- SelectObject((HDC)hdcBitmap,(HGDIOBJ)(HBITMAP)hbmOld);
- DeleteDC(hdcBitmap);
- ReleaseDC(hwnd,hdcWnd);
- return hbmBitmap;
- }
Advertisement
Add Comment
Please, Sign In to add comment