Win32 SDK 显示图像

#include <Windows.h>
#include <OCIdl.h>
#include <OleCtl.h>


BOOL DrawPicture(char* pszFilePath,HWND hWnd,int nDrawWidth,int nDrawHeight)
{
    HDC hDC = GetDC(hWnd);
    if(hDC == NULL)
    {
        return GetLastError();
    }
    
    IPicture* pPic = NULL;
    IStream* pStream = NULL;
    int ret = 0;
    HANDLE hFile = NULL;
    DWORD dwFileSize = 0;
    DWORD dwByteRead = 0;
    LPVOID pvData = NULL;
    HGLOBAL hMem = NULL;
    
    hFile = CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
        goto exit;

    dwFileSize = GetFileSize(hFile, NULL);
    if (dwFileSize == INVALID_FILE_SIZE)
        goto exit;
    
    hMem = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
    if (hMem == NULL)
        goto exit;
    
    pvData = GlobalLock(hMem);
    
    if(!ReadFile(hFile, pvData, dwFileSize, &dwByteRead,NULL) || dwByteRead !=dwFileSize)
    {
        goto exit;
    }
    
    GlobalUnlock(hMem);
    
    CreateStreamOnHGlobal(hMem, TRUE, &pStream);

    ret = OleLoadPicture(pStream, dwFileSize, TRUE, IID_IPicture, (LPVOID*)&pPic);
    
    if(ret != S_OK)
        goto exit;
    
    OLE_XSIZE_HIMETRIC picWidth;
    OLE_YSIZE_HIMETRIC picHeight;
    pPic->get_Width(&picWidth);
    pPic->get_Height(&picHeight);
    
    ret = pPic->Render(hDC, 0, 0, nDrawWidth, nDrawHeight, 0, picHeight, picWidth, -picHeight, NULL);
exit:
    if(hDC)
    {
        ReleaseDC(hWnd, hDC);
        hDC = NULL;
    }
    if(hFile)
    {
        CloseHandle(hFile);
        hFile = NULL;
    }
    if(pPic)
    {
        pPic->Release();
        pPic = NULL;
    }
    if(pStream)
    {
        pStream->Release();
        pStream = NULL;
    }
    if(hMem)
    {
        GlobalFree(hMem);
        hMem = NULL;
    }
    if(ret)
        return TRUE;
    else
        return FALSE;

} 

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_PAINT:
        {
            PAINTSTRUCT ps = {0};
            BeginPaint(hwnd, &ps);
            RECT rc;
            GetClientRect(hwnd, &rc);
            DrawPicture("H:/Desktop/psu.jpg", hwnd, rc.right-rc.left, rc.bottom-rc.top);            
            EndPaint(hwnd, &ps);
            return 0;
        }

    case WM_CLOSE:
        DestroyWindow(hwnd);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    default:
        break;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "vbgk_class";
    wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;

    if(!RegisterClassEx(&wc))
    {
        return 1;
    }

    CoInitialize(NULL);

    int cxScreen = GetSystemMetrics(SM_CXSCREEN);
    int cyScreen = GetSystemMetrics(SM_CYSCREEN);

    HWND hwnd = CreateWindowEx(0, "vbgk_class", "Pic Viewer", WS_OVERLAPPEDWINDOW,
        (cxScreen - 900) / 2, (cyScreen - 700) / 2, 900, 700, NULL, NULL, hInstance, NULL);
    if(hwnd == NULL)
    {
        return 2;
    }

    UpdateWindow(hwnd);
    ShowWindow(hwnd, nShowCmd);

    MSG msg;
    BOOL bRet;

    while((bRet = GetMessage(&msg, hwnd, 0, 0)) != 0)
    {
        if(bRet == -1)
            break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

转载于:https://www.cnblogs.com/ytjjyy/archive/2012/07/14/2591172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值