// gdiplus01.cpp 2013.02.07 by Hatada // // Load image from file. #define WINVER 0x0501 // 0x0501 = WindowsXP #define _WIN32_WINNT 0x0501 #include #include using namespace Gdiplus; #pragma comment(lib, "user32.lib") #pragma comment(lib, "gdiplus.lib") #define SCRW 512 #define SCRH 512 #define TM_COUNT1 1 #define FPS 60 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static Image *img; static Image *bg; static Graphics *g; RECT rect; static float x, y, dx, dy; static int wdw_w, wdw_h; switch (uMsg) { case WM_CREATE: // get window size GetClientRect(hWnd, &rect); wdw_w = rect.right - rect.left; wdw_h = rect.bottom - rect.top; img = new Image(L"test.png"); bg = new Image(L"bg.png"); // int work x = (float)(wdw_w / 2); y = (float)(wdw_h / 2); dx = (float) wdw_w / (float)FPS; dy = dx * 0.7; SetTimer(hWnd, TM_COUNT1, (int)(1000 / FPS), NULL); break; case WM_TIMER: // main loop x += dx; y += dy; if (x <= 0 || x + img->GetWidth() >= wdw_w) dx *= -1; if (y <= 0 || y + img->GetHeight() >= wdw_h) dy *= -1; InvalidateRect(hWnd, NULL, TRUE); // InvalidateRect(hWnd, NULL, FALSE); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); g = new Graphics(hdc); g->DrawImage(bg, 0, 0, bg->GetWidth(), bg->GetHeight()); g->DrawImage(img, (int)x, (int)y, img->GetWidth(), img->GetHeight()); delete (g); EndPaint(hWnd, &ps); break; case WM_DESTROY: delete (img); delete (bg); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) { MSG msg; GdiplusStartupInput gpSI; ULONG_PTR lpToken; /* GDI+ */ GdiplusStartup(&lpToken, &gpSI, NULL); HCURSOR hCursor = LoadCursor(NULL, IDC_ARROW); HBRUSH hBrush = (HBRUSH)(COLOR_WINDOW + 1); WNDCLASS wcl = { 0, WndProc, 0, 0, hInst, NULL, hCursor, hBrush, NULL, "mh" }; DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE; if (!RegisterClass(&wcl) || !CreateWindowEx(0, "mh", "gdiplus test", style, CW_USEDEFAULT, CW_USEDEFAULT, SCRW, SCRH, NULL, NULL, hInst, NULL)) return FALSE; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } /* GDI+I */ GdiplusShutdown(lpToken); return msg.wParam; }