简体   繁体   English

c ++程序截取屏幕截图

[英]c++ Program to take a screenshot

I am making a program that will click the printscreen key of the keyboard. 我正在制作一个程序,它将点击键盘的printscreen键。 The code I am using is the following: 我使用的代码如下:

INPUT myInput;

myInput.type = INPUT_KEYBOARD;

KEYBDINPUT keyboardInput;
keyboardInput.wScan = 0;
keyboardInput.dwFlags = 0;
keyboardInput.time = 0;
keyboardInput.dwExtraInfo = 0;
keyboardInput.wVk = VK_SNAPSHOT; 
myInput.ki = keyboardInput;

SendInput(1, &myInput, sizeof(INPUT));//pressing the printscreen key

keyboardInput.dwFlags = KEYEVENTF_KEYUP;
myInput.ki = keyboardInput;

SendInput(1, &myInput, sizeof(INPUT));//releasing the printscreen key

for some reason the code doesn't work what so ever. 由于某种原因,代码不能正常工作。 If I go to paint and try to paist from clipboard, it will only past whatever printscreen I had done before I had used my program. 如果我去绘画并试图从剪贴板中进行paist,那么它只会通过我在使用我的程序之前所做的任何打印屏幕。 Also my keyboard doesn't need me to press the "alt" with the print screen in order for it to work.. 另外我的键盘不需要我用打印屏幕按“alt”以使其工作..

I had tryed to included the pressing of the Alt key beeeforee the pressing of the printscreen key, as well as the release of the Alt key afffftterr the releasing of the printscreen key, and the difference I got was that when I tried to past it on paint, I paist some kind of a full black screen... This was just a test I did to see if it makes a difference, but my actual keyboard takes screenshots with only the hit of the print screen button. 我曾尝试按下Alt键,按下打印屏幕键,以及释放Alt键,释放打印屏幕键,我得到的差异是当我试图通过它时油漆,我是一个完整的黑色屏幕...这只是我做的一个测试,看看它是否有所作为,但我的实际键盘仅截取打印屏幕按钮的截图。

Any ideas on what I am doing wrong guys? 关于我做错了什么人的任何想法?

Edited: just to let you guys know, the program does in fact compile. 编辑:只是为了让你们知道,该程序确实编译。 I have also added other code that saves the clipboard file to a directory, and I do save a file correctly if I manually hit the print screen button... but if I keep looping this code with the saving to a directory, the same picture of the manually obtained screenshot appears... so that is how I know for sure that there is a problem with the hitting of the printscreen button. 我还添加了其他代码,将剪贴板文件保存到目录中,如果我手动点击打印屏幕按钮,我会正确保存文件...但是如果我保持循环这段代码并保存到目录,则相同的图片手动获取的屏幕截图显示...这就是我如何确定打印屏幕按钮的打击有问题。

On the windows platform: You have to follow a certain sequence of simulated key presses. 在Windows平台上:您必须遵循一定的模拟按键序列。

The code below is a simulates keybd_event() keyboard events and puts the captured screen into the clipboard. 下面的代码是模拟keybd_event()键盘事件,并将捕获的屏幕放入剪贴板。

#include <iostream>
#include <windows.h>
using namespace std;



int main()
{
    keybd_event(VK_MENU, 0, 0, 0); //Alt Press
    keybd_event(VK_SNAPSHOT, 0, 0, 0); //PrntScrn Press


    keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0); //PrntScrn Release
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0); //Alt Release

return 0;
}

That is code for taking a screenshot in BMP and to convert it to JPG: 这是在BMP中截取屏幕截图并将其转换为JPG的代码:

void TakeScreenShot(const std::string& path)
{
//setting to the screen shot
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

//handler of the bitmap that save the screen shot
HBITMAP hBitmap;

//I have to give for it time to make it work
Sleep(100);

//take the screen shot
OpenClipboard(NULL);

//save the screen shot in the bitmap handler 
hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);

//relese the screen shot
CloseClipboard();

std::vector<BYTE> buf;
IStream *stream = NULL;
HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &stream);
CImage image;
ULARGE_INTEGER liSize;

// screenshot to jpg and save to stream
image.Attach(hBitmap);
image.Save(stream, Gdiplus::ImageFormatJPEG);
IStream_Size(stream, &liSize);
DWORD len = liSize.LowPart;
IStream_Reset(stream);
buf.resize(len);
IStream_Read(stream, &buf[0], len);
stream->Release();

// put the imapge in the file
std::fstream fi;
fi.open(path, std::fstream::binary | std::fstream::out);
fi.write(reinterpret_cast<const char*>(&buf[0]), buf.size() * sizeof(BYTE));
fi.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM