windows 获取剪切板中的图片大小

Table of Contents
    #pragma once
    
    class cliboard_data
    {
    public:
        static bool getImageSize(int &width, int &height);
        static unsigned int getLastError();
    };
    
    #include "cliboard_data.h"
    #include <stdint.h>
    #include <Windows.h>
    
    typedef struct
    {
        uint32_t biSize;
        int32_t  biWidth;
        int32_t  biHeight;
        uint16_t biPlanes;
        uint16_t biBitCount;
        uint32_t biCompression;
        uint32_t biSizeImage;
        int32_t  biXPelsPerMeter;
        int32_t  biYPelsPerMeter;
        uint32_t biClrUsed;
        uint32_t biClrImportant;
    } DIB;
    
    bool cliboard_data::getImageSize(int &width, int &height)
    {
        if (!(IsClipboardFormatAvailable(CF_BITMAP) || IsClipboardFormatAvailable(CF_DIB) || IsClipboardFormatAvailable(CF_DIBV5))) {
            return false;
        }
    
        bool result = false;
        if (OpenClipboard(NULL)) {
            HANDLE hClipboard = GetClipboardData(CF_DIB);
            if (!hClipboard) {
                hClipboard = GetClipboardData(CF_DIBV5);
            }
    
            if (hClipboard != NULL && hClipboard != INVALID_HANDLE_VALUE) {
                void* dib = GlobalLock(hClipboard);
                if (dib) {
                    DIB *info = reinterpret_cast<DIB*>(dib);
                    width = info->biWidth;
                    height = info->biHeight;
                    result = true;
                    GlobalUnlock(dib);
                }
            }
            CloseClipboard();
        }
    
        return result;
    }
    
    unsigned int cliboard_data::getLastError()
    {
        return GetLastError();
    }