我正在用 C++ 在 Qt4 中编写一个屏幕捕获应用程序。我在调整双屏时遇到问题。我无法使用第二个屏幕获取图像。我尝试了 ac# 应用程序,它将在一个图像中抓取所有桌面,我可以从那里提取每个屏幕桌面图像。这是c#代码
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public class TestGrab
{
[STAThread]
static void Main(string[] args)
{
IntPtr hDC = WindowsNative.GetDC(WindowsNative.GetDesktopWindow());
IntPtr hBitmap = WindowsNative.GetCurrentObject(hDC,
WindowsNative.OBJ_BITMAP);
System.Drawing.Bitmap imageDesktop = System.Drawing.Image.FromHbitmap(
hBitmap);
imageDesktop.Save(@"c:\zzzzdesktop.png");
}
}
public class WindowsNative
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr ptr);
public const int OBJ_BITMAP = 7;
[DllImport("gdi32.dll")]
public static extern IntPtr GetCurrentObject(IntPtr hdc, uint
uiObjectType);
}
Qt 代码更小,我还测试了桌面的本机 Windows 句柄是否与 Qt desktop0>winId() 不同,但它们是相等的
QPixmap CaptureWinDesktop()
{
WId desktop=GetDesktopWindow();
WId desktop2=QApplication::desktop()->winId();
if(desktop!=desktop2)
{
qDebug("sunt fdiferite WId");
}
QPixmap entireDesktop= QPixmap::grabWindow(desktop);
return entireDesktop;
}
我不确定这是 Qt 中的错误还是功能,使用相同的窗口句柄它仅重试第一个桌面,而实际上它是由 2 个屏幕组成的中继桌面。一个想法是使用本机 Windows 调用并将图像保存在临时文件中并从那里加载 QPixmap,但是在没有 MFC 的 c++ 文件中保存 HBITMAP 并不简单。结论:您认为 Qt 中的错误是什么?知道如何解决它(没有 MFC)