This content originally appeared on DEV Community and was authored by 海前 王
#include<string>
#include<atlimage.h>
#include <iostream>
using namespace std;
//name:保存的文件名
//hWnd:要截图的窗口句柄,NULL表示对桌面截图
bool SavePic(wstring name, HWND hWnd) {
HDC hDc = NULL;
hWnd = (hWnd == NULL) ? GetDesktopWindow() : hWnd; //判断窗口句柄是否为NULL,如果为NULL则默认获取桌面DC
hDc = GetDC(hWnd); //获取DC
if (hDc == NULL) return false;
int bitOfPix = GetDeviceCaps(hDc, BITSPIXEL); //获取DC像素的大小
int width = GetDeviceCaps(hDc, HORZRES); //获取DC宽度
int hight = GetDeviceCaps(hDc, VERTRES); //获取DC高度
UINT dpi = GetDpiForWindow(hWnd); //获取dpi
float fold; //根据dpi计算放大倍数
switch (dpi) {
case 96:
fold = 1;
break;
case 120:
fold = 1.25;
break;
case 144:
fold = 1.5;
break;
case 192:
fold = 2;
break;
case 216:
fold = 2.25;
break;
default:
fold = 1;
break;
}
width *= fold; //复原宽度
hight *= fold; //复原高度
CImage image;
image.Create(width, hight, bitOfPix); //为图像类创建与窗口DC相同大小的DC
BitBlt(image.GetDC(), 0, 0, width, hight, hDc, 0, 0, SRCCOPY); //将窗口DC图像复制到image
image.Save(name.data(), Gdiplus::ImageFormatPNG); //保存为png格式图片文件
image.ReleaseDC(); //释放DC
ReleaseDC(hWnd, hDc); //释放DC资源
}
//#include <afxstr.h> // CString header
#include <string> // std::string
#include <locale> // std::wstring_convert
#include <codecvt> // std::wstring_convert
int main() {
std::string f;
std::getline(std::cin, f);
// Convert std::string to std::wstring
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wstr = converter.from_bytes(f);
// Create CString from std::wstring
//CString d(wstr.c_str());
wstr += L".png";
// Use CString
SavePic(wstr, NULL);
}
This content originally appeared on DEV Community and was authored by 海前 王
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
海前 王 | Sciencx (2024-08-27T01:33:19+00:00) win32_api_shortCut. Retrieved from https://www.scien.cx/2024/08/27/win32_api_shortcut/
" » win32_api_shortCut." 海前 王 | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/win32_api_shortcut/
HARVARD海前 王 | Sciencx Tuesday August 27, 2024 » win32_api_shortCut., viewed ,<https://www.scien.cx/2024/08/27/win32_api_shortcut/>
VANCOUVER海前 王 | Sciencx - » win32_api_shortCut. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/win32_api_shortcut/
CHICAGO" » win32_api_shortCut." 海前 王 | Sciencx - Accessed . https://www.scien.cx/2024/08/27/win32_api_shortcut/
IEEE" » win32_api_shortCut." 海前 王 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/win32_api_shortcut/. [Accessed: ]
rf:citation » win32_api_shortCut | 海前 王 | Sciencx | https://www.scien.cx/2024/08/27/win32_api_shortcut/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.