fork download
  1. #include<windows.h>
  2. #include<gdiplus.h>
  3. #include <stdio.h>
  4. using namespace Gdiplus;
  5. #pragma comment(lib,"gdiplus.lib")
  6.  
  7.  
  8.  
  9.  
  10. void draw(HDC hdc,int x,int y)
  11. {
  12. GdiplusStartupInput gdiplusStartupInput;
  13. ULONG_PTR gdiplusToken; // ULONG PTR 為無符號長整型指針
  14. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  15. static int cy[]={0,600,1200,1800,2400},i=0;
  16. Rect rect(x,y,250,550); //(x,y,cx,cy) x,y為開始貼圖的位置;cx,cy為貼出來的大小
  17. Unit srcunit = UnitPixel; //像素大小
  18. Image* image = new Image(L"D:\\Desktop\\peo.png");
  19. Bitmap CacheImage( 1024, 768, image->GetPixelFormat() );
  20. Graphics CacheGraphics( &CacheImage );
  21. SolidBrush sbr(Color(128, 128, 128));
  22. CacheGraphics.FillRectangle(&sbr, 0, 0, 1024, 768);
  23. //CacheGraphics.DrawImage(image, rect,0,cy[i++],500,590,UnitPixel);
  24. CacheGraphics.DrawImage(image, rect, 0, 0, 250, 550, UnitPixel);
  25.  
  26. Graphics graphics(hdc);
  27. graphics.DrawImage(&CacheImage, 0,0); //(image,rect,x,y,cx,cy,Unit)
  28. //x,y為要貼的圖的左上角位置;cx,cy為要貼的圖的右下角位置
  29. ;
  30. if(i==5)i=0;
  31. delete image;
  32. GdiplusShutdown(gdiplusToken);
  33. return;
  34. }
  35.  
  36.  
  37. LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  38. {
  39. HDC hdc ;
  40. PAINTSTRUCT ps ;
  41. RECT rect ;
  42. static int x=20,y=20;
  43. switch(uMsg){
  44. case WM_PAINT:
  45. hdc = BeginPaint (hwnd, &ps);
  46. draw(hdc,x,y);
  47. EndPaint (hwnd, &ps);
  48. case WM_CREATE:break;
  49. case WM_KEYDOWN:
  50. switch (wParam){
  51. case VK_UP: y--;break;
  52. case VK_DOWN:y++;break;
  53. case VK_LEFT:x--;break;
  54. case VK_RIGHT:x++;break;
  55. default: break;
  56. }
  57. InvalidateRect(hwnd,NULL,FALSE) ;
  58. break;
  59. case WM_CLOSE: DestroyWindow(hwnd);break;
  60. case WM_DESTROY: PostQuitMessage(0); break;
  61. case WM_COMMAND:break;
  62.  
  63. default: return DefWindowProc(hwnd,uMsg,wParam,lParam);
  64. }
  65.  
  66.  
  67.  
  68. }
  69.  
  70. int WINAPI WinMain(HINSTANCE hInstance,
  71. HINSTANCE hPrevInstance,
  72. LPSTR lpCmdLine,
  73. int nCmdShow){
  74. WNDCLASSEX wcx;
  75. MSG Msg; //訊息
  76. HWND hwnd; //代號
  77. //註冊窗口
  78. wcx.cbSize =sizeof(wcx);
  79. wcx.style =CS_HREDRAW | CS_VREDRAW; //指定視窗樣式為:改變視窗大小就重繪
  80. wcx.lpfnWndProc =WndProc; //指定處理函式為WndProc
  81. wcx.cbClsExtra =0;
  82. wcx.cbWndExtra =0;
  83. wcx.hInstance =hInstance;
  84. wcx.hIcon =LoadIcon(NULL, IDI_APPLICATION); //LoadIcon 載入圖示供程式使用。
  85. wcx.hCursor =LoadCursor(NULL, IDC_ARROW); //LoadCursor 載入滑鼠游標供程式使用。
  86. wcx.hbrBackground= (HBRUSH) GetStockObject (GRAY_BRUSH) ; //GetStockObject取得一個圖形物件
  87. wcx.lpszMenuName = NULL;
  88. wcx.lpszClassName="MainClass";
  89. wcx.hIconSm =LoadIcon(NULL, IDI_APPLICATION);
  90. if(!RegisterClassEx(&wcx)){ //為程式視窗註冊視窗類別。
  91. MessageBox(NULL,"RegisterClass Error","ERROR",MB_OK);
  92. return 0;
  93. }
  94. //創建窗口
  95. hwnd=CreateWindow( //根據視窗類別建立一個視窗,回傳視窗代號到hwnd。
  96. "MainClass",
  97. "Test Create Window",
  98. WS_OVERLAPPEDWINDOW,
  99. CW_USEDEFAULT,
  100. CW_USEDEFAULT,
  101. 1024,768,
  102. (HWND)NULL,
  103. (HMENU)NULL,
  104. hInstance,
  105. NULL
  106. );
  107. if(hwnd==NULL){
  108. MessageBox(NULL,"CreateWindow Error","ERROR",MB_OK);
  109. return 0;
  110. }
  111. ShowWindow(hwnd,nCmdShow); //在螢幕上顯示視窗。
  112. UpdateWindow(hwnd); //指示視窗自我更新
  113. while(GetMessage(&Msg,NULL,0,0)>0){ //從訊息佇列中取得訊息
  114. TranslateMessage(&Msg); // 轉譯某些鍵盤訊息。
  115. DispatchMessage(&Msg); //將訊息發送給視窗訊息處理程式。
  116. }
  117. return Msg.wParam;
  118.  
  119. }
  120.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty