嵌入式(setparent)directx游戏C#的窗口捕获

编程入门 行业动态 更新时间:2024-10-14 06:22:04
本文介绍了嵌入式(setparent)directx游戏C#的窗口捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目标: - 捕获一个包含directx游戏窗口的winform窗口(如截图,但具体句柄)。 场景: - 在winform中我通常插入一个panel1。 - 我使用winapi SETPARENT在penel1中插入一个directx主游戏窗口 问题: 当捕获winform窗口(this.handle)时,panel1中嵌入式游戏的图像不会出现。只有面板出现。 我无法使用ScreenShot方法,因为它会让另一个窗口结束。 示例: 使用屏幕截图(捕获屏幕) 使用WinAPI(捕获特定窗口) 我尝试过:

Goal: - Capture a winform window containing a directx game window (like screenshot, but specific handle). Scenario: - Inside a winform I insert a panel1 normally. - I used the winapi SETPARENT to insert a directx main game window inside the penel1 Problem: When capturing the winform window (this.handle), the image of the embedded game in the panel1 does not appear. Only the panel appears. I can't use the ScreenShot method, because it will get another window over. Example: With screenshot (capturing the screen) With WinAPI (capturing the specific window) What I have tried:

public Image CaptureWindow(IntPtr handle, int imgX = 0, int imgY = 0, int largura = 0, int altura = 0) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); if(largura == 0 || altura == 0) { largura = windowRect.right - windowRect.left; altura = windowRect.bottom - windowRect.top; } // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, largura, altura); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over //GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, imgX, imgY, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return img; }

推荐答案

使用带有Directx窗口的SetParent后,需要刷新winform。只是:this.Refresh(); After use SetParent with Directx window, need to refresh the winform. Just: this.Refresh();

更多推荐

嵌入式(setparent)directx游戏C#的窗口捕获

本文发布于:2023-11-07 23:38:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌入式   窗口   游戏   directx   setparent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!