如何调用windows自带函数生成缩略图

#include <windows.h>
#include <shlobj.h>
#include <thumbcache.h>
#include <GdiPlus.h>

HBITMAP g_hThumbnail;           // Thumbnail to create

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);

            // The GDI+ objects provide an operator new implementation (in GdiplusBase) and that does
            // not throw. Thus clients must check the return value from new rather than catch this failure
            // as an exception
            Gdiplus::Graphics *pGraphics = new Gdiplus::Graphics(hdc);
            if (pGraphics)
            {
                Gdiplus::Bitmap * pBitmap = Gdiplus::Bitmap::FromHBITMAP(g_hThumbnail, NULL);
                if (pBitmap)
                {
                    pGraphics->DrawImage(pBitmap, 0, 0);
                    delete pBitmap;
                }
                pBitmap = NULL;
                delete pGraphics;
            }

            EndPaint(hWnd, &ps);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pszCmdLine, int nCmdShow)
{
    Gdiplus::GdiplusStartupInput startupInput;
    ULONG_PTR           gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &startupInput, NULL);

    int numArgs;
    PWSTR *ppszArgs = CommandLineToArgvW(pszCmdLine, &numArgs);
    if (ppszArgs && (numArgs == 2))
    {
        PCWSTR pszSize = ppszArgs[0];
        PCWSTR pszFile = ppszArgs[1];

        int nSize = _wtoi(pszSize);     // Size of thumbnail

        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
        if (SUCCEEDED(hr))
        {
            IShellItem *psi;
            hr = SHCreateItemFromParsingName(pszFile, NULL, IID_PPV_ARGS(&psi));
            if (SUCCEEDED(hr))
            {
                IThumbnailProvider *pThumbProvider;
                hr = psi->BindToHandler(NULL, BHID_ThumbnailHandler, IID_PPV_ARGS(&pThumbProvider));
                if (SUCCEEDED(hr))
                {
                    WTS_ALPHATYPE wtsAlpha;
                    hr = pThumbProvider->GetThumbnail(nSize, &g_hThumbnail, &wtsAlpha);
                    if (SUCCEEDED(hr))
                    {
                        WNDCLASSEX wcex = { sizeof(wcex) };
                        wcex.lpfnWndProc = WndProc;
                        wcex.hInstance = hInstance;
                        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
                        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
                        wcex.lpszClassName = L"ThumbnailAppClass";

                        RegisterClassEx(&wcex);

                        HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, L"Thumbnail Provider SDK Sample",
                            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                            nSize, nSize, NULL, NULL, hInstance, NULL);
                        if (hWnd)
                        {
                            ShowWindow(hWnd, nCmdShow);

                            MSG msg;
                            while (GetMessage(&msg, NULL, 0, 0))
                            {
                                TranslateMessage(&msg);
                                DispatchMessage(&msg);
                            }
                        }
                        DeleteObject(g_hThumbnail);
                    }
                    pThumbProvider->Release();
                }
                psi->Release();
            }
            CoUninitialize();
        }
    }
    else
    {
        MessageBox(NULL, L"Usage: ThumbnailProvider.exe <size> <Absolute Path to file>", L"Wrong number of arguments.", MB_OK);
    }

    if (ppszArgs)
    {
        LocalFree(ppszArgs);
    }

    Gdiplus::GdiplusShutdown(gdiplusToken); // shut down GDIPlus
    return 0;
}


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值