Win32 SDK顯示圖像(GDI+)


  剛學了GDI+, 發現顯示圖片很方便, 以前用OleLoadPicture+IPicture接口顯示, 別提有多
悲劇了, 現在學了GDI+, 太方便友好了, 哈哈~

  提供一個GDI+顯示圖片的示例供那些不知道怎么用Win32SDK顯示圖片的新手程序猿們一快速簡單的方法.

  程序工作方式, 程序啟動后, 任意拖動一張圖片到窗口即可顯示圖像, 格式包括但不限於JPG,BMP,PNG,...

  預覽:
  




  程序代碼:  

#include <windows.h>
#include <gdiplus.h>

#pragma comment(lib,"gdiplus")

using namespace Gdiplus;

//根據圖片的寬度和高度更新窗口客戶區的大小
void set_window_size(HWND hWnd,int width,int height)
{
    RECT rcWindow,rcClient;
    int border_width,border_height;

    GetWindowRect(hWnd,&rcWindow);
    GetClientRect(hWnd,&rcClient);

    border_width = (rcWindow.right-rcWindow.left) - (rcClient.right-rcClient.left);
    border_height = (rcWindow.bottom-rcWindow.top) - (rcClient.bottom-rcClient.top);

    SetWindowPos(hWnd,0,0,0,border_width+width,border_height+height,SWP_NOMOVE|SWP_NOZORDER);
}

void draw_image(HWND hWnd,wchar_t* file)
{
    HDC hdc;
    int width,height;

    //加載圖像
    Image image(file);
    if(image.GetLastStatus() != Status::Ok){
        MessageBox(hWnd,"圖片無效!",NULL,MB_OK);
        return;
    }
    
    //取得寬度和高度
    width = image.GetWidth();
    height = image.GetHeight();

    //更新窗口大小
    set_window_size(hWnd,width,height);

    hdc = GetDC(hWnd);

    //繪圖
    Graphics graphics(hdc);
    graphics.DrawImage(&image,0,0,width,height);

    ReleaseDC(hWnd,hdc);

    return;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_DROPFILES://拖動圖片文件時進行圖像顯示
        {
            wchar_t file[MAX_PATH];
            DragQueryFileW((HDROP)wParam,0,file,sizeof(file)/sizeof(*file));
            draw_image(hWnd,file);
            DragFinish((HDROP)wParam);
            return 0;
        }
    case WM_LBUTTONDOWN://左鍵單擊時拖動窗體
        SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
        return 0;
    case WM_CREATE:
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
    MSG msg;
    WNDCLASSEX wce={0};
    HWND hWnd;

    wce.cbSize = sizeof(wce);
    wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wce.hCursor = LoadCursor(NULL,IDC_ARROW);
    wce.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wce.hInstance = hInstance;
    wce.lpfnWndProc = WndProc;
    wce.lpszClassName = "MyClassName";
    wce.style = CS_HREDRAW|CS_VREDRAW;
    if(!RegisterClassEx(&wce))
        return 1;

    char* title = "Win32SDK GDI+ 圖像顯示示例程序(拖動圖片文件到窗口以顯示)";
    hWnd = CreateWindowEx(0,"MyClassName",title,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,NULL,hInstance,NULL);
    if(hWnd == NULL)
        return 1;

    //GdiPlus初始化
    ULONG_PTR gdiplusToken;
    GdiplusStartupInput gdiplusInput;
    GdiplusStartup(&gdiplusToken,&gdiplusInput,NULL);

    //窗口接收文件拖放
    DragAcceptFiles(hWnd,TRUE);

    ShowWindow(hWnd,nShowCmd);
    UpdateWindow(hWnd);

    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    //GdiPlus 取消初始化
    GdiplusShutdown(gdiplusToken);

    return msg.wParam;
}



示例項目下載(VS2012):http://files.cnblogs.com/nbsofer/show_pic.7z

女孩不哭 @ 2013-06-26 21:44:56 @ http://www.cnblogs.com/nbsofer



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM