DEV Community

海前 王
海前 王

Posted on

win32_api_shortCut

#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);

}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)