当前位置:   article > 正文

c++windows编程加载图片_c++ window.h 可不可以导入图片

c++ window.h 可不可以导入图片
  1. // yidongtupian.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "yidongtupian.h"
  5. #include "windows.h"
  6. #include <stdio.h>
  7. #define MAX_LOADSTRING 100
  8. HINSTANCE hInst;
  9. HBITMAP girl[4],bg;
  10. HDC hdc,mdc,bufdc;
  11. HWND hWnd;
  12. DWORD tPre,tNow;
  13. int num,dir,x,y;
  14. ATOM MyRegisterClass(HINSTANCE hInstance);
  15. BOOL InitInstance(HINSTANCE, int);
  16. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  17. void MyPaint(HDC hdc);
  18. int APIENTRY WinMain(HINSTANCE hInstance,
  19. HINSTANCE hPrevInstance,
  20. LPSTR lpCmdLine,
  21. int nCmdShow)
  22. {
  23. MSG msg;
  24. MyRegisterClass(hInstance);
  25. if (!InitInstance (hInstance, nCmdShow))
  26. {
  27. return FALSE;
  28. }
  29. GetMessage(&msg,NULL,NULL,NULL);
  30. while( msg.message!=WM_QUIT )
  31. {
  32. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  33. {
  34. TranslateMessage(&msg);
  35. DispatchMessage(&msg);
  36. }
  37. else
  38. {
  39. tNow=GetTickCount();
  40. if (tNow-tPre>=40)
  41. {
  42. MyPaint(hdc);
  43. }
  44. }
  45. }
  46. return msg.wParam;
  47. }
  48. ATOM MyRegisterClass(HINSTANCE hInstance)
  49. {
  50. WNDCLASSEX wcex;
  51. wcex.cbSize=sizeof(WNDCLASSEX);
  52. wcex.style= CS_HREDRAW | CS_VREDRAW;
  53. wcex.lpfnWndProc= (WNDPROC)WndProc;
  54. wcex.cbClsExtra=0;
  55. wcex.cbWndExtra=0;
  56. wcex.hInstance=hInstance;
  57. wcex.hIcon=NULL;
  58. wcex.hCursor=NULL;
  59. wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
  60. wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  61. wcex.lpszMenuName=NULL;
  62. wcex.lpszClassName=TEXT("canvas");
  63. wcex.hIconSm=NULL;
  64. return RegisterClassEx(&wcex);
  65. }
  66. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  67. {
  68. HBITMAP bmp;
  69. hInst=hInstance;
  70. hWnd=CreateWindow(TEXT("canvas"),TEXT("绘图窗口"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  71. if (!hWnd)
  72. {
  73. return FALSE;
  74. }
  75. MoveWindow(hWnd,10,10,640,480,true);
  76. ShowWindow(hWnd, nCmdShow);
  77. UpdateWindow(hWnd);
  78. hdc = GetDC(hWnd);
  79. mdc = CreateCompatibleDC(hdc);
  80. bufdc = CreateCompatibleDC(hdc);
  81. bmp = CreateCompatibleBitmap(hdc,640,480);
  82. SelectObject(mdc,bmp);
  83. x = 300;
  84. y = 260;
  85. dir = 0;
  86. num = 0;
  87. girl[0] =(HBITMAP)LoadImage(NULL,TEXT("C:/Users/dell/Desktop/新建文件夹/girl0.bmp"),IMAGE_BITMAP,59,59,LR_LOADFROMFILE);
  88. girl[1] =(HBITMAP)LoadImage(NULL,TEXT("C:/Users/dell/Desktop/新建文件夹/girl1.bmp"),IMAGE_BITMAP,59,59,LR_LOADFROMFILE);
  89. girl[2] =(HBITMAP)LoadImage(NULL,TEXT("C:/Users/dell/Desktop/新建文件夹/girl2.bmp"),IMAGE_BITMAP,59,59,LR_LOADFROMFILE);
  90. girl[3] =(HBITMAP)LoadImage(NULL,TEXT("C:/Users/dell/Desktop/新建文件夹/girl3.bmp"),IMAGE_BITMAP,59,59,LR_LOADFROMFILE);
  91. bg =(HBITMAP)LoadImage(NULL,TEXT("C:/Users/dell/Desktop/新建文件夹/bg.bmp"),IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
  92. MyPaint(hdc);
  93. return TRUE;
  94. }
  95. void MyPaint(HDC hdc)
  96. {
  97. int w,h;
  98. SelectObject(bufdc,bg);
  99. BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
  100. SelectObject(bufdc,girl[dir]);
  101. switch(dir)
  102. {
  103. case 0:
  104. w = 55;
  105. h = 74;
  106. break;
  107. case 1:
  108. w = 53;
  109. h = 77;
  110. break;
  111. case 2:
  112. w = 60;
  113. h = 74;
  114. break;
  115. case 3:
  116. w = 60;
  117. h = 74;
  118. break;
  119. }
  120. BitBlt(mdc,x,y,w,h,bufdc,num*w,h,SRCAND);
  121. BitBlt(mdc,x,y,w,h,bufdc,num*w,0,SRCPAINT);
  122. BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
  123. tPre=GetTickCount();
  124. num++;
  125. if (num==8)
  126. {
  127. num=0;
  128. }
  129. }
  130. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)
  131. {
  132. switch(message)
  133. {
  134. case WM_KEYDOWN:
  135. switch (wParam)
  136. {
  137. case VK_ESCAPE:
  138. PostQuitMessage( 0 );
  139. break;
  140. case VK_UP:
  141. switch(dir)
  142. {
  143. case 0:
  144. y -= 1;
  145. break;
  146. case 1:
  147. x -= 1;
  148. y -= 1;
  149. break;
  150. case 2:
  151. x += 1;
  152. y -= 1;
  153. break;
  154. case 3:
  155. x += 1;
  156. y -= 1;
  157. break;
  158. }
  159. if(y < 0)
  160. y = 0;
  161. dir = 0;
  162. break;
  163. case VK_DOWN:
  164. switch(dir)
  165. {
  166. case 0:
  167. x += 1;
  168. y += 1;
  169. break;
  170. case 1:
  171. y += 1;
  172. break;
  173. case 2:
  174. x += 1;
  175. y += 1;
  176. break;
  177. case 3:
  178. x += 1;
  179. y += 1;
  180. break;
  181. }
  182. if(y > 375)
  183. y = 375;
  184. dir = 1;
  185. break;
  186. case VK_LEFT:
  187. switch(dir)
  188. {
  189. case 0:
  190. x -= 1;
  191. break;
  192. case 1:
  193. x -= 1;
  194. y += 1;
  195. break;
  196. case 2:
  197. x -= 1;
  198. break;
  199. case 3:
  200. x -= 1;
  201. break;
  202. }
  203. if(x < 0)
  204. x = 0;
  205. dir = 2;
  206. break;
  207. case VK_RIGHT:
  208. switch(dir)
  209. {
  210. case 0:
  211. x += 1;
  212. break;
  213. case 1:
  214. x += 1;
  215. y += 1;
  216. break;
  217. case 2:
  218. x += 1;
  219. break;
  220. case 3:
  221. x += 1;
  222. break;
  223. }
  224. if(x > 575)
  225. x = 575;
  226. dir = 3;
  227. break;
  228. }
  229. break;
  230. case WM_DESTROY:
  231. int i;
  232. DeleteDC(mdc);
  233. DeleteDC(bufdc);
  234. for (i=0;i<4;i++)
  235. {
  236. DeleteObject(girl[i]);
  237. }
  238. DeleteObject(bg);
  239. ReleaseDC(hWnd,hdc);
  240. PostQuitMessage(0);
  241. break;
  242. default:
  243. return DefWindowProc(hWnd,message,wParam,lParam);
  244. }
  245. return 0;
  246. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/281771
推荐阅读
相关标签
  

闽ICP备14008679号