【发布时间】:2011-04-27 08:33:16
【问题描述】:
我正在使用 C++ 在 Qt4 中编写屏幕捕获应用程序。我在调整双屏时遇到问题。我无法使用第二个屏幕获取图像。我尝试了一个 c# 应用程序,它将在一个图像中抓取所有桌面,我可以从那里提取每个屏幕桌面图像。 这是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 代码更小,我还测试了桌面的本机窗口句柄是否与 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)
【问题讨论】:
-
另外我找到了一个使用 GDI experts-exchange.com/Microsoft/Development/Q_26484815.html 的例子,看底部,数字是硬编码的,所以你必须得到尺寸并修改代码
标签: qt screen-capture multiple-monitors