-1

[![enter image description here][1]][1]I am trying to capture the active window in Win32 using C++. With the BitBlt function I am able to capture, but once another window opens, the same window which I have already captured should only be captured. I don't want the other window which I have opened, it should be black. Can someone help with a solution?

https://www.codeproject.com/Articles/20367/Screen-Capture-Simple-Win32-Dialog-Based

void CaptureActiveWindow(void)
{
    RECT ActWndRect;

    WCHAR buf [100],buf1[20];
    int xSrc=0,ySrc=-19;
    int DepcWidth=10, DepcHeight=5;
    OutputDebugString(L"Start capture act window ");     
    HDC ActWndDC = GetDC(hWndActWnd);               //DC for the window you have clicked on 

    MemDC = CreateCompatibleDC(ActWndDC);           //Memory DC Compatible with Above DC

    GetWindowRect(hWndActWnd,&ActWndRect);          //Will Store the Windows Are in Rectangle 

    wsprintf(buf,L"x1 = %d , y1 = %d, x2 = %d y2 =%d",ActWndRect.left,ActWndRect.top,ActWndRect.right,ActWndRect.bottom);
    OutputDebugString(buf); 

    int Width = ActWndRect.right-ActWndRect.left;       //Width of the Window
    int Height =ActWndRect.bottom-ActWndRect.top;       //Hight of the Window

    if(GetWindowText(hWndActWnd,buf1,20) >0)
    {
        OutputDebugString(buf1);
    }
    if(CaptureControl)
    {
        ySrc= DepcWidth = DepcHeight = 0;
    }

    HBITMAP hBitmap = CreateCompatibleBitmap(DlgDC,Width-DepcWidth,Height-DepcHeight);//Will Create Bitmap Comatible With Our Window
    SelectObject(MemDC,hBitmap);


    BitBlt(MemDC,0,0,Width,Height,ActWndDC,xSrc,ySrc,SRCCOPY);//Will Copy the Window into MemDC
    //BitBlt(DeskDC,110,110,Width,Height,MemDC,Begpt.x,Begpt.y,SRCCOPY);


    SaveBitmap(MemDC, hBitmap,"Sample.bmp");    // will Save DC into .bmp File  
    ShowImage();                                //Will Show u the .bmp File in MSPAINT.


}

1 Answers1

1

Hook the mouse event Before sending active message to the window. Use WindowFromPoint to get the specified window(Hwnd). Then use GetWindowRect to get the window rect area. In this area, call WindowFromPoint for all the point in the rect, compare it with Hwnd(if it is a child window or not), and get the overlap RECT. After getting the bitmap of the capture window and then overwrites the black on the covered rect.

PS: I encounter BITMAPINFO error: Run-Time Check Failure #2 - Stack around the variable was corrupted. Here provide a solution.

You can't capture the image of Chrome using BitBlt(), unless disable the Hardware Acceleration option of Chrome. But PrintWindow() works with PW_RENDERFULLCONTENT flag. When use it, the image in center will have a black border. While using PrintWindow (hWndActWnd,ActWndDC,0x00000003) align the image to the left.Then modify cx and cy of CreateCompatibleBitmap(), you can remove the border easily.

Jeffreys
  • 431
  • 2
  • 8
  • Shao : Hi above in qusetion descrption i have added one image please check .There initially after selecting single window (which is quick access) after i am trying to bring another window on top of it it became blank .that behaviour i want.please look in to it. tahnk you – Venkatesh Nagaraju Aug 12 '19 at 05:35
  • @ Jeffrey Shao: i want to capture the specific window even if it is hidden and active in both cases .could you please help me with that – Venkatesh Nagaraju Aug 13 '19 at 12:56
  • Sorry,I misunderstood what you mean. When click the window, this window turns active, you only can capture the whole window. We also can't crop the bitmap. Your thoughts may not be realized. – Jeffreys Aug 14 '19 at 08:50
  • @ Jeffrey Shao:Sorry for that,generally if a window is hidden means any other window is already there on that window(window which we want to capture) .The other window which is already there on the window( which you want to capture) that window(other window which is on top of the window which we want to capture) should become black and any pop up comes in between that also should become black.i am giving another case in next comment please check. – Venkatesh Nagaraju Aug 14 '19 at 10:37
  • @ Jeffrey Shao:Another case is while capturing the window if you try to bring any other window on top of the capturing window in that case also other window should become black or we can even dont allow any operations on capturing window .In my question description i gave image that is when iam trying bring another window on the capturing window the other window became black. – Venkatesh Nagaraju Aug 14 '19 at 10:38
  • I have a bad workaround. Hook the mouse event Before sending active message to the window. Use `WindowFromPoint` to get the specified window(Hwnd). Then use `GetWindowRect` to get the window rect area. In this area, call `WindowFromPoint` for all the point in the rect, compare it with Hwnd(if it is a child window or not), and get the overlap RECT. After getting the bitmap of the capture window and then overwrites the black on the covered rect. – Jeffreys Aug 15 '19 at 06:58
  • @ Jeffrey Shao:Hi thank you for your reply now i am able to capture even if it hidden but i am not able to capture chrome and calulator windows i dont know why? .could you please tell me how to capture caluculator and chrome.Thank you – Venkatesh Nagaraju Aug 16 '19 at 12:02
  • About Chrome, Settings -> Advanced -> Uncheck “Use hardware acceleration when available” in category System. Then you can capture the Chrome window. – Jeffreys Aug 19 '19 at 05:43
  • About Caluculator, it seems that app in Microsoft Store can't be captured using this method. Maybe you can use `Printwindow()`, as this [case](https://stackoverflow.com/questions/30682228/troubleshoot-why-printwindow-is-blank) said in the question. – Jeffreys Aug 19 '19 at 06:06
  • @ Jeffrey Shao:Hi thank you for your reply now i am able to capture even chrome but if we Uncheck “Use hardware acceleration when available” ,capturing chrome and chrome application also becomes slow .Could you please tell me any other way to capture . – Venkatesh Nagaraju Aug 20 '19 at 06:33
  • Of course, the biggest area where you see this problem is trying to capture Chromium-based software, specifically Google Chrome or any Electron-based software . With Chrome the commonly recommended fix, disabling the Hardware Acceleration option, is a suboptimal solution, as it removes a very powerful feature from your browser。 Outside of Chromium apps, you have issues with most Windows Universal apps, obvious examples are the built-in Calculator and Groove Music programs in Windows 10. Currently my best solution is to capture the desktop window and crop it to match where the window is at. – Jeffreys Aug 20 '19 at 08:59
  • @ Jeffrey Shao:Hi thank you for your reply could you please tell me how to capture the chrome using Printwindow();.Do i need to pass the chrome handle to Printwindow()? – Venkatesh Nagaraju Aug 22 '19 at 10:30
  • @ Jeffrey Shao: Hi i want to capture the specific window using Directx also including chrome.Any help would be appriocited.Thank you – Venkatesh Nagaraju Aug 22 '19 at 12:50
  • You can refer to this [case](https://stackoverflow.com/questions/30021274/capture-screen-using-directx) how to capture screen using directx. Please read it [Printwindow()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-printwindow). – Jeffreys Aug 23 '19 at 01:27
  • #define PW_RENDERFULLCONTENT 0x00000002 PrintWindow (hWndActWnd,ActWndDC,PW_RENDERFULLCONTENT); can capture Chrome without Uncheck “Use hardware acceleration when available” – Venkatesh Nagaraju Aug 27 '19 at 11:09
  • Shao : Now chrome is capturing fine but with this flag(PW_RENDERFULLCONTENT) all other windows moving right-bottom corner and black region getting generated on left and top.Any solution would be appreciated – Venkatesh Nagaraju Aug 29 '19 at 10:32
  • use `PrintWindow (hWndActWnd,ActWndDC,0x00000003)`, modify this parameter to align the image to the left. Then modify `cx` and `cy` of `CreateCompatibleBitmap()`. – Jeffreys Aug 30 '19 at 07:55
  • with 0x00000002, align the image to the center. – Jeffreys Aug 30 '19 at 08:05
  • @ Jeffrey Shao:I have added another Image please check .When i click on the middle of the window it is not taking entire window only part of the window .But Iwant to capture the whole window .please check give solution where ever i click on window it should capture entire window. – Venkatesh Nagaraju Sep 05 '19 at 12:17
  • Hi, the window is childwindow of the root window, use GetAncestor(hWndActWnd, GA_ROOT) to get the root window. If this case have been solved, you should mark it as answer. If you have another questions, firstly you should search. When you fail to find the solution, please ask a new question in So fourm. – Jeffreys Sep 06 '19 at 10:00
  • Shao: Hi with this PrintWindow (hWndActWnd,ActWndDC,0x00000003) i am able to capture chrome but i am not able to perform any action on window while capturing but after passing NULL instead of 0x00000003 this flag i am able to perform action on windows but i can't capture chrome.can you please help me. – Venkatesh Nagaraju Sep 09 '19 at 12:57
  • What action do you want to perform? – Jeffreys Sep 10 '19 at 08:35
  • For example if i am initially opened and capturing ( C\\Downloads )window(folder) but while capturing if i want to open (C\\ Documents )window(folder) i have to click on that widow right? but while capturing i am not able click on any other window(folder) after passing 0x00000003 flag capturing window is hanging not allowing any click on it – Venkatesh Nagaraju Sep 10 '19 at 14:03
  • why do you want to do this? It is fast to capture the window. – Jeffreys Sep 11 '19 at 01:40
  • Shao:That's what is functionality .That is how it works while capturing specific window.So could please tell me how to handle that case? – Venkatesh Nagaraju Sep 26 '19 at 08:34
  • I suggest you ask a new qusetion in SO fourm. – Jeffreys Sep 27 '19 at 01:02
  • Shao:This is the last question about this question please give some solution.If i post another question it takes lot of time for others to understand – Venkatesh Nagaraju Sep 27 '19 at 05:29