有遮挡有隐藏应用程序录像【更新中】

使用PrintWindow方法可以获得程序位图,无关乎托管和非托管,只是好像很多游戏使用DS的是无法获取位图的,代码如下:(大部分参考自:http://blog.csdn.net/spiderlily/article/details/8548470

IntPtr hWndSrc = FindWindow(null, findStr);
IntPtr hSrcDC = GetWindowDC(hWndSrc);
Rect rect = new Rect();
GetWindowRect(hWndSrc, out rect);
Rectangle rectex = new Rectangle(0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top);
IntPtr hBitmap = CreateCompatibleBitmap(hSrcDC, rectex.Width, rectex.Height);
IntPtr hmemdc = CreateCompatibleDC(hSrcDC);
SelectObject(hmemdc, hBitmap);
int re = PrintWindow(hWndSrc, hmemdc, 0);
Bitmap bmp = null;
if (re != 0)
{
 bmp = Bitmap.FromHbitmap(hBitmap);
}
DeleteObject(hBitmap);
DeleteDC(hmemdc);//删除用过的对象       
ReleaseDC(hWndSrc, hSrcDC);
newBitmap = bmp;
this.Invalidate();
Thread.Sleep(threadInterval_ms);    //我是一个睡美人儿~

其中:

public struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

而DWM实现代码:

IntPtr hWndSrc = FindWindow(null, findStr);
if (System.Environment.OSVersion.Version.Major >= 6)
{
    DwmIsCompositionEnabled(ref en);
    if (en)
    {
    Int32 hResult = 0;
    hThumbail = DwmRegisterThumbnail(this.Handle, hWndSrc);
        if (hThumbail != IntPtr.Zero)
        {
        DWM_THUMBNAIL_PROPERTIES dskThumbProps = new DWM_THUMBNAIL_PROPERTIES();
        dskThumbProps.dwFlags = 
            DWM_THUMBNAIL_PROPERTIES.DWM_TNP_RECTDESTINATION | 
           DWM_THUMBNAIL_PROPERTIES.DWM_TNP_VISIBLE | 
          DWM_THUMBNAIL_PROPERTIES.DWM_TNP_OPACITY | 
        DWM_THUMBNAIL_PROPERTIES.DWM_TNP_SOURCECLIENTAREAONLY;
        dskThumbProps.opacity = 255;
        dskThumbProps.fVisible = true;
        dskThumbProps.rcSource = dskThumbProps.rcDestination = 
        new Rectangle(0, 0, ClientRectangle.Right, ClientRectangle.Bottom);
        dskThumbProps.fSourceClientAreaOnly = false;
            
         DwmUpdateThumbnailProperties(hThumbail, dskThumbProps);
        }        
    }
    else
    {... }
}
else
{...   }

你可能感兴趣的:(有遮挡有隐藏应用程序录像【更新中】)