• 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;
    }
  • 相关阅读:
    R语言中gsub使用示例记录
    linux 系统中wget实现并行下载
    R语言中实现将多行数据合并为一行
    python安装包国内镜像加速
    VMware Workstation 与 Device/Credential Guard不兼容
    缩放矩阵, 这里面有关于矩阵的 种种操作 这个要学下 很有用
    ffd 点的控制效果、
    摘抄 : max mel 的一些用法。
    帮朋友写的查找选择的父子骨骼。之前我都是用递归,这个好、
    距离的算法。 下面两个是对等的。
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2591172.html
Copyright © 2020-2023  润新知