CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    11

    [RESOLVED] STATIC control problems...

    I am trying to create a screen shot of a window using C++ API.
    I can capture the picture fine, i can save it, I can copy it to the clip board and paste it and it all works fine. But when I try to send it to a static control with SS_BITMAP style set it just shows a gray box.
    I can also BitBlt it to the screen.
    here is my code:
    Code:
    HWND wnd = FindWindow("Notepad", NULL);
                char title[256];
                GetWindowText(wnd, title, 256);
                printf("Found window '%s'\n", title);
                RECT wndRect;
                GetClientRect(wnd, &wndRect);
    
                HDC hSource = GetDC(wnd);
    
                HDC hOut; HBITMAP bOut; //BITMAP iOut;
    
                hOut = CreateCompatibleDC( GetDC(0) );
                bOut = CreateCompatibleBitmap( GetDC(0), wndRect.right, wndRect.bottom);
                //SelectObject(hOut, bOut);
                typedef BOOL (_stdcall *tPrintWindow)(  HWND, HDC,UINT);
    
                tPrintWindow pPrintWindow =0;
    
                HINSTANCE  handle = ::GetModuleHandle("user32.dll");
                if ( handle == 0 )
                    handle = ::LoadLibrary("User32.dll");
                if (handle)
                    pPrintWindow = (tPrintWindow)::GetProcAddress(handle,"PrintWindow");
    
                if( pPrintWindow)
                    pPrintWindow(wnd,hOut,0 );
                SelectBitmap(hOut, bOut);
                BitBlt(hOut, 0, 0, wndRect.right, wndRect.bottom, hSource, 0, 0, SRCCOPY);
    
                HDC blah = GetDC(0);
                BitBlt(blah, 0, 0, 100, 100, hOut, 0, 0, SRCCOPY);
    
                OpenClipboard(NULL);
                EmptyClipboard();
                SetClipboardData(CF_BITMAP, bOut);
                CloseClipboard();
                CreateBMPFile((TCHAR*)"IHateWindows.bmp", CreateBitmapInfoStruct(bOut),                      bOut, hOut);
                DeleteObject((HGDIOBJ)SendMessage(hPict,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)bOut));
                printf("end");
    Please, someone, Help.

  2. #2
    Join Date
    Dec 2008
    Posts
    11

    Re: STATIC control problems...

    I fixed it turns out I wasn't deleting the HDCs at all.
    Heres the new code that works:
    Code:
    case WM_RBUTTONDOWN:{
                HWND wnd = FindWindow("Notepad", NULL);
                //if(wnd==NULL) break;
                char title[256];
                GetWindowText(wnd, title, 256);
                printf("Found window '%s'\n", title);
                SetForegroundWindow(wnd);
                RECT wndRect;
                GetClientRect(wnd, &wndRect);
    
                HBITMAP hBmpOld;
                HDC hSource = GetDC(wnd);
    
                HDC hOut; HBITMAP bOut; //BITMAP iOut;
                HDC hdc = GetDC(0) ;
                hOut = CreateCompatibleDC(hdc);
                bOut = CreateCompatibleBitmap( GetDC(0), wndRect.right, wndRect.bottom);
                typedef BOOL (_stdcall *tPrintWindow)(  HWND, HDC,UINT);
    
                tPrintWindow pPrintWindow =0;
    
                HINSTANCE  handle = ::GetModuleHandle("user32.dll");
                if ( handle == 0 )
                    handle = ::LoadLibrary("User32.dll");
                if (handle)
                    pPrintWindow = (tPrintWindow)::GetProcAddress(handle,"PrintWindow");
    
                if( pPrintWindow)
                    pPrintWindow(wnd,hOut,0 );
                SelectBitmap(hOut, bOut);
                BitBlt(hOut, 0, 0, wndRect.right, wndRect.bottom, hSource, 0, 0, SRCCOPY);
    
                HDC blah = GetDC(0);
                BitBlt(blah, 0, 0, 100, 100, hOut, 0, 0, SRCCOPY);
                ReleaseDC(0,blah);
                SetPixel(hOut, 5, 5, RGB(255, 0, 0));
    
                DeleteDC(hOut);
                ReleaseDC(0,hdc);
                SendMessage(hPict,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)bOut);
                printf(".");
    }break;
    but now my problem is: if I take a picture of say, the top header of MSDN it show a lot of gray where i think there is transparency in the image on the msdn web page.
    so the tabs on the mdn web page are gray.
    and if there is a title bar over the capturing window.
    Last edited by r4z0rw0lf; December 21st, 2008 at 01:37 PM.

  3. #3
    Join Date
    Dec 2008
    Posts
    11

    Re: STATIC control problems...

    Alright Forget it. i got everything fixed.
    it wasnt the static control at all.
    in case you wanted to follow on my programming blunders:
    i got the SelectBitmap and printf window mixed-up.
    heres the NEW code that works even if the window is not visible.
    Code:
                HDC hSource = GetDC(0);
                HDC hOut = CreateCompatibleDC(hSource);
                 HBITMAP bOut;
                RECT wndRect;
                GetWindowRect(wnd, &wndRect);
                int height=wndRect.right-wndRect.left;
                int width=wndRect.bottom-wndRect.top;
    
                bOut = CreateCompatibleBitmap( hSource, height,width);//);
                hBmpOld = SelectBitmap(hOut, bOut);
                typedef BOOL (_stdcall *tPrintWindow)(  HWND, HDC,UINT);
    
                tPrintWindow pPrintWindow =0;
    
                HINSTANCE  handle = ::GetModuleHandle("user32.dll");
                if ( handle == 0 )
                    handle = ::LoadLibrary("User32.dll");
                if (handle)
                    pPrintWindow = (tPrintWindow)::GetProcAddress(handle,"PrintWindow");
                if( pPrintWindow){
                    printf("printWindow is declared!\n");
                    pPrintWindow(wnd,hOut,0 );
                }
                ReleaseDC(0,hSource);
                memcpy(bOut1,bOut,sizeof(bOut));;
                SendMessage(hPict,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)bOut);//
                SelectObject(hOut,hBmpOld);
                DeleteObject(bOut);
                DeleteDC(hOut);
                DeleteObject(hBmpOld);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured