タイマーを使って、

定期的に描画することができます。

 

カップが移動する動画です。

 

〇プログラム例

 

  1. #include <windows.h>
  2.  
  3. static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  4. {
  5.     HDC hdc;
  6.     static HDC hMemDC;
  7.     PAINTSTRUCT ps;
  8.     LPSTR str = TEXT("こんにちは、日本");
  9.     static HBITMAP hForeBmp;
  10.     static HFONT hFont[2];
  11.     LPSTR FORE_BitMapFile = TEXT("sカップ.bmp");
  12.     static BITMAP bmpFore;
  13.     static HBRUSH hBrush;
  14.     RECT rect;
  15.     static POINT pt;
  16.     static BOOL xIncrease, yIncrease;
  17.  
  18.     switch (uMsg)
  19.     {
  20.     case WM_DESTROY:
  21.         DeleteDC(hMemDC);
  22.  
  23.         DeleteObject(hForeBmp);
  24.         DeleteObject(hFont[0]);
  25.  
  26.         PostQuitMessage(0);
  27.         return 0;
  28.  
  29.     case WM_CREATE:
  30.         hFont[0] = CreateFont(
  31.             40, 0, 0, 0,
  32.             FW_NORMAL, FALSE, FALSE, FALSE,
  33.             SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS,
  34.             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  35.             VARIABLE_PITCH | FF_DONTCARE, L"メイリオ"
  36.         );
  37.         ShowWindow(hWnd, SW_SHOW);
  38.         hdc = GetDC(hWnd);
  39.         hMemDC = CreateCompatibleDC(hdc);
  40.         hForeBmp = LoadImage(NULL, FORE_BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  41.         GetObject(hForeBmp, sizeof(BITMAP), &bmpFore);
  42.         SelectObject(hMemDC, hForeBmp);
  43.         SetTimer(hWnd, 1, 1, NULL);
  44.  
  45.         return 0;
  46.         
  47.     case WM_TIMER:
  48.         if (xIncrease)
  49.             pt.x++;
  50.         else
  51.             pt.x--;
  52.         
  53.         if (yIncrease)
  54.             pt.y++;
  55.         else
  56.             pt.y--;
  57.  
  58.         GetClientRect(hWnd, &rect);
  59.         if (pt.x + bmpFore.bmWidth > rect.right)
  60.             xIncrease = FALSE;
  61.         else if (pt.x < 0)
  62.             xIncrease = TRUE;
  63.         if (pt.y + bmpFore.bmHeight > rect.bottom)
  64.             yIncrease = FALSE;
  65.         else if (pt.y < 0)
  66.             yIncrease = TRUE;
  67.  
  68.         InvalidateRect(hWnd, NULL, TRUE);
  69.         return 0;
  70.  
  71.     case WM_PAINT:
  72.         hdc = BeginPaint(hWnd, &ps);
  73.         SelectObject(hdc, hFont[0]);
  74.         SetTextColor(hdc, RGB(0, 0xFF, 0));
  75.         TextOut(hdc, 0, 0, str, lstrlen(str));
  76.  
  77.         BitBlt(hdc, pt.x, pt.y, pt.x + bmpFore.bmWidth, pt.y + bmpFore.bmHeight,
  78.             hMemDC,0,0,SRCCOPY);
  79.  
  80.         EndPaint(hWnd, &ps);
  81.  
  82.         return 0;
  83.     }
  84.  
  85.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  86.  
  87. }
  88.  
  89. int APIENTRY wWinMain(
  90.     _In_ HINSTANCE hInstance,
  91.     _In_opt_ HINSTANCE hPrevInstance,
  92.     _In_ LPWSTR ipCmdLine,
  93.     _In_ int nCmdShow
  94. ) {
  95.  
  96.     WNDCLASSEXW wcex;
  97.  
  98.     wcex.cbSize = sizeof(WNDCLASSEX);
  99.  
  100.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  101.     wcex.lpfnWndProc = WndProc;
  102.     wcex.cbClsExtra = 0;
  103.     wcex.cbWndExtra = 0;
  104.     wcex.hInstance = hInstance;
  105.     wcex.hIcon = NULL;
  106.     wcex.hCursor = NULL;
  107.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  108.     wcex.lpszMenuName = NULL;
  109.     wcex.lpszClassName = TEXT("TestWindow");
  110.     wcex.hIconSm = NULL;
  111.  
  112.     RegisterClassExW(&wcex);
  113.  
  114.     HWND hWnd = CreateWindowEx(0UL, TEXT("TestWindow"), TEXT("日本"), WS_OVERLAPPEDWINDOW,
  115.         CW_USEDEFAULT, 0, 500, 400, NULL, NULL, hInstance, NULL);
  116.  
  117.     if (!hWnd)
  118.     {
  119.         return FALSE;
  120.     }
  121.  
  122.     MSG msg;
  123.  
  124.     while (GetMessage(&msg, NULL, 0, 0))
  125.     {
  126.         DispatchMessage(&msg);
  127.     }
  128.  
  129.     return (int)msg.wParam;
  130.  
  131. }

 

〇実行結果

カップがウィンドウ内を移動します。