简体   繁体   English

如何使用c++获取win32中活动窗口的句柄?

[英]How to get the handle of the active window in win32 using c++?

[![enter image description here][1]][1]I am trying to capture the active window in Win32 using C++. [![在此处输入图像描述][1]][1]我正在尝试使用 C++ 捕获 Win32 中的活动窗口。 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.使用BitBlt函数我可以捕获,但是一旦另一个窗口打开,我已经捕获的同一个窗口应该只被捕获。 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 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.


}

Hook the mouse event Before sending active message to the window.在向窗口发送活动消息之前钩住鼠标事件。 Use WindowFromPoint to get the specified window(Hwnd).使用 WindowFromPoint 获取指定的窗口(Hwnd)。 Then use GetWindowRect to get the window rect area.然后使用 GetWindowRect 获取窗口矩形区域。 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.在该区域中,对矩形内的所有点调用WindowFromPoint,与Hwnd(是否为子窗口)进行比较,得到重叠的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. PS:我遇到 BITMAPIINFO 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏。 Here provide a solution. 这里提供一个解决方案。

You can't capture the image of Chrome using BitBlt() , unless disable the Hardware Acceleration option of Chrome.您无法使用BitBlt()捕获 Chrome 的图像,除非禁用 Chrome 的硬件加速选项。 But PrintWindow() works with PW_RENDERFULLCONTENT flag.但是PrintWindow()使用 PW_RENDERFULLCONTENT 标志。 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.在使用 PrintWindow (hWndActWnd,ActWndDC,0x00000003) 时,将图像左对齐。然后修改CreateCompatibleBitmap() cxcy ,您可以轻松去除边框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM