2017-06-20 100 views
0

我希望在使用下面的程序時,OnPaint函數中的graphics.DrawImage(image,0,0)顯示從文件中讀取的圖像。取而代之的是爲應用程序獲取一張白色畫布。 我在做什麼錯?gdi plus圖像未顯示

我在Windows 10上使用visual studio 2017社區。 謝謝!

/* https://msdn.microsoft.com/en-us/library/vs/alm/ms533895(v=vs.85).aspx */ 

#define UNICODE 
#include <windows.h> 
#include <objidl.h> 
#include <gdiplus.h> 
using namespace Gdiplus; 
#pragma comment (lib,"Gdiplus.Lib") 
#pragma comment (lib,"User32.Lib") 
#pragma comment (lib,"Gdi32.Lib") 

VOID OnPaint(HDC hdc, Image * image) 
{ 
    Graphics graphics(hdc); 
    graphics.DrawImage(image,0,0); 
} 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow) 
{ 
    HWND    hWnd; 
    MSG     msg; 
    WNDCLASS   wndClass; 
    GdiplusStartupInput gdiplusStartupInput; 
    ULONG_PTR   gdiplusToken; 

    // Initialize GDI+. 
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 

    wndClass.style   = CS_HREDRAW | CS_VREDRAW; 
    wndClass.lpfnWndProc = WndProc; 
    wndClass.cbClsExtra  = 0; 
    wndClass.cbWndExtra  = 0; 
    wndClass.hInstance  = hInstance; 
    wndClass.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
    wndClass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
    wndClass.lpszMenuName = NULL; 
    wndClass.lpszClassName = TEXT("GettingStarted"); 

    RegisterClass(&wndClass); 

    hWnd = CreateWindow(
     TEXT("GettingStarted"), // window class name 
     TEXT("Getting Started"), // window caption 
     WS_OVERLAPPEDWINDOW,  // window style 
     CW_USEDEFAULT,   // initial x position 
     CW_USEDEFAULT,   // initial y position 
     CW_USEDEFAULT,   // initial x size 
     CW_USEDEFAULT,   // initial y size 
     NULL,      // parent window handle 
     NULL,      // window menu handle 
     hInstance,    // program instance handle 
     NULL);     // creation parameters 

    ShowWindow(hWnd, iCmdShow); 
    UpdateWindow(hWnd); 

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

    GdiplusShutdown(gdiplusToken); 
    return msg.wParam; 
} // WinMain 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
    WPARAM wParam, LPARAM lParam) 
{ 
    HDC   hdc; 
    PAINTSTRUCT ps; 
    Image *image = NULL; 
    switch(message) 
    { 
    case WM_CREATE: 
     //create image 
    MessageBox(NULL, L"small.png", L"File Path", MB_OK); 
     image = new Image(L"small.png"); 
     if (image) 
     return 0; 
     else 
     return -1; 
    case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     OnPaint(hdc,image); 
     EndPaint(hWnd, &ps); 
     return 0; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     return 0; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
} // WndProc 
+0

當您獲取WM_PAINT消息時,'image'變量將再次爲NULL。使用調試器很容易看到。你可以聲明它是'static'作爲解決方法。 –

+0

感謝漢斯。我將「圖像」設置爲靜態,圖像按預期顯示! – user308879

回答

0

的錯誤指出由Hans帕桑特 - 在WndProcimage參數重新設置爲NULL和graphics.DrawImage(image,0,0)將一無所獲顯示。