在 C++/CLI 中做截图

Do screenshot in C++/CLI

我使用 windows 7 和 VS2010。我写了一段C++/CLI代码来做测量,测量过程中我想截图。

我试过用C++(使用GDI库)截屏的方法,但编译文件失败。 所以我想 GDI 库不能在 C++/CLI 中使用。 如何在C++/CLI项目中截图粘贴到word中?

代码如下:

#include "stdafx.h"

using namespace System;
using namespace System::IO;

int main(array<System::String ^> ^args)
{

String^ fileName = "AP_result.doc";

StreamWriter^ sw = gcnew StreamWriter(fileName);
 ...
sw->Close();
 return 0;
}

我试过 Slawomir Orlowski 的方法,但出现错误:

error

如此处所述

你需要获取屏幕上下文,然后将其放入位图文件中,上面的答案中有更好的解释,因此请检查答案和评论

屏幕截图的 C++/CLI 代码:

using namespace System;
using namespace System::Windows;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;


int main(array<System::String ^> ^args)
{
    Screen^ r = System::Windows::Forms::Screen::PrimaryScreen;
    int width = r->Bounds.Width;
    int height = r->Bounds.Height;

    Bitmap^ bmp = gcnew Bitmap(width, height, PixelFormat::Format32bppArgb);
    Graphics^ screen = Graphics::FromImage(bmp);
    screen->CopyFromScreen(0,0,0,0, Size(width, height), CopyPixelOperation::SourceCopy);
    bmp->Save("screenshot.bmp");      
    return 0;
}

在您的项目中,您必须添加一些引用:System.Drawing、System.Windows、System.Windows.Forms.