zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    求数组中的最小子数组,时间复杂度o(n),java
    第四周进度条
    四则混合运算3
    软件工程作业3
    《构建之法》第三周阅读笔记
    第三周学习进度
    学习进度01
    构建之法阅读笔记01
    构建之法问题
    随机生成题目运算
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2591172.html
Copyright © 2011-2022 走看看