// 02_gdiplus02.cpp // // Load image from resource. // // https://geolog.mydns.jp/www.geocities.jp/iooiau/tips/aero.html // https://sistemainformatico.tk/Archives/000027.php #define WINVER 0x0501 // 0x0501 = WindowsXP #define _WIN32_WINNT 0x0501 #include #include #include "resource.h" 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 HINSTANCE hInst; // Load image from resource Bitmap *LoadImageFromResource( HINSTANCE hinst, // handle instance LPCTSTR pName, // resource ID LPCTSTR pType // resource type ) { // Search resource HRSRC hRes = FindResource(hinst, pName, pType); if (hRes == NULL) { MessageBox(NULL, "Not found resource", "Error", MB_OK); return NULL; } // get resource size DWORD Size = SizeofResource(hinst, hRes); if (Size == 0) { MessageBox(NULL, "Resource size = 0", "Error", MB_OK); return NULL; } HGLOBAL hData = LoadResource(hinst, hRes); if (hData == NULL) { MessageBox(NULL, "Failure load resource", "Error", MB_OK); return NULL; } const void *pData = LockResource(hData); if (pData == NULL) { MessageBox(NULL, "Failure lock resource", "Error", MB_OK); return NULL; } // Copy resource data HGLOBAL hBuffer = GlobalAlloc(GMEM_MOVEABLE, Size); if (hBuffer == NULL) { MessageBox(NULL, "Failure alloc", "Error", MB_OK); return NULL; } void *pBuffer = GlobalLock(hBuffer); if (pBuffer == NULL) { MessageBox(NULL, "Failure lock", "Error", MB_OK); GlobalFree(hBuffer); return NULL; } CopyMemory(pBuffer, pData, Size); GlobalUnlock(hBuffer); // read image IStream *pStream; if (CreateStreamOnHGlobal(hBuffer, TRUE, &pStream) != S_OK) { MessageBox(NULL, "Failure create stream", "Error", MB_OK); GlobalFree(hBuffer); return NULL; } Bitmap *pBitmap = Bitmap::FromStream(pStream); pStream->Release(); return pBitmap; } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static Gdiplus::Bitmap* img; static Gdiplus::Bitmap* 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 = LoadImageFromResource(hInst, MAKEINTRESOURCE(IDI_BALL), RT_RCDATA); bg = LoadImageFromResource(hInst, MAKEINTRESOURCE(IDI_BG), RT_RCDATA); // not work // img = LoadImageFromResource(hInst, TEXT("IDI_BALL"), TEXT("RCDATA")); // bg = LoadImageFromResource(hInst, TEXT("IDI_BG"), TEXT("RCDATA")); if (img != NULL && bg != NULL) { // init work x = (float)(wdw_w / 2); y = (float)(wdw_h / 2); dx = (float) wdw_w / (float)FPS; dy = dx * 0.5; // Set timer 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); if (bg != NULL) g->DrawImage(bg, 0, 0, bg->GetWidth(), bg->GetHeight()); if (img != NULL) 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 hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) { MSG msg; GdiplusStartupInput gpSI; ULONG_PTR lpToken; hInst = hInstance; /* 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; }