Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. // Win32Project_Chess.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Win32Project_Chess.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. #define IMAGES_FOLDER L"C:\\cppChess\\"
  10. const auto IMAGES_CHESS_BOARD = IMAGES_FOLDER"board.png";
  11. #define IMAGES_CHESS_PIECES IMAGES_FOLDER"pieces.png"
  12.  
  13. //using namespace Gdiplus;
  14.  
  15. // Global Variables:
  16. HINSTANCE hInst; // current instance
  17. WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  18. WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  19.  
  20. // Forward declarations of functions included in this code module:
  21. ATOM MyRegisterClass(HINSTANCE hInstance);
  22. BOOL InitInstance(HINSTANCE, int);
  23. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  24. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  25.  
  26. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  27. _In_opt_ HINSTANCE hPrevInstance,
  28. _In_ LPWSTR lpCmdLine,
  29. _In_ int nCmdShow)
  30. {
  31. UNREFERENCED_PARAMETER(hPrevInstance);
  32. UNREFERENCED_PARAMETER(lpCmdLine);
  33.  
  34. // TODO: Place code here.
  35.  
  36. // Initialize global strings
  37. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  38. LoadStringW(hInstance, IDC_WIN32PROJECT_CHESS, szWindowClass, MAX_LOADSTRING);
  39. MyRegisterClass(hInstance);
  40.  
  41. ULONG_PTR gdipToken;
  42. Gdiplus::GdiplusStartupInput gdipStartupInput;
  43. //Gdiplus::GdiplusStartupOutput gdipStartupOutput;
  44. Gdiplus::GdiplusStartup(
  45. &gdipToken,
  46. &gdipStartupInput,
  47. nullptr // NULL
  48. );
  49.  
  50. // Perform application initialization:
  51. if (!InitInstance (hInstance, nCmdShow))
  52. {
  53. return FALSE;
  54. }
  55.  
  56. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT_CHESS));
  57.  
  58. MSG msg;
  59. // Main message loop:
  60. while (GetMessage(&msg, nullptr, 0, 0))
  61. {
  62. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  63. {
  64. TranslateMessage(&msg);
  65. DispatchMessage(&msg);
  66. }
  67. }
  68.  
  69. Gdiplus::GdiplusShutdown(gdipToken);
  70.  
  71. return (int) msg.wParam;
  72. }
  73.  
  74.  
  75.  
  76. //
  77. // FUNCTION: MyRegisterClass()
  78. //
  79. // PURPOSE: Registers the window class.
  80. //
  81. ATOM MyRegisterClass(HINSTANCE hInstance)
  82. {
  83. WNDCLASSEXW wcex;
  84.  
  85. wcex.cbSize = sizeof(WNDCLASSEX);
  86.  
  87. wcex.style = CS_HREDRAW | CS_VREDRAW;
  88. wcex.lpfnWndProc = WndProc;
  89. wcex.cbClsExtra = 0;
  90. wcex.cbWndExtra = 0;
  91. wcex.hInstance = hInstance;
  92. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT_CHESS));
  93. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  94. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  95. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT_CHESS);
  96. wcex.lpszClassName = szWindowClass;
  97. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  98.  
  99. return RegisterClassExW(&wcex);
  100. }
  101.  
  102. //
  103. // FUNCTION: InitInstance(HINSTANCE, int)
  104. //
  105. // PURPOSE: Saves instance handle and creates main window
  106. //
  107. // COMMENTS:
  108. //
  109. // In this function, we save the instance handle in a global variable and
  110. // create and display the main program window.
  111. //
  112. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  113. {
  114. hInst = hInstance; // Store instance handle in our global variable
  115.  
  116. HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME,
  117. CW_USEDEFAULT, 0, 900, 900, nullptr, nullptr, hInstance, nullptr);
  118.  
  119. if (!hWnd)
  120. {
  121. return FALSE;
  122. }
  123.  
  124. ShowWindow(hWnd, nCmdShow);
  125. UpdateWindow(hWnd);
  126.  
  127. return TRUE;
  128. }
  129.  
  130. //
  131. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  132. //
  133. // PURPOSE: Processes messages for the main window.
  134. //
  135. // WM_COMMAND - process the application menu
  136. // WM_PAINT - Paint the main window
  137. // WM_DESTROY - post a quit message and return
  138. //
  139. //
  140. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  141. {
  142. switch (message)
  143. {
  144. case WM_COMMAND:
  145. {
  146. int wmId = LOWORD(wParam);
  147. // Parse the menu selections:
  148. switch (wmId)
  149. {
  150. case IDM_ABOUT:
  151. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  152. break;
  153. case IDM_EXIT:
  154. DestroyWindow(hWnd);
  155. break;
  156. default:
  157. return DefWindowProc(hWnd, message, wParam, lParam);
  158. }
  159. }
  160. break;
  161. case WM_PAINT:
  162. {
  163. PAINTSTRUCT ps;
  164. HDC hdc = BeginPaint(hWnd, &ps);
  165. // TODO: Add any drawing code that uses hdc here...
  166.  
  167. {
  168. auto gdipGraphics = Gdiplus::Graphics::FromHWND(hWnd);
  169. //Gdiplus::Graphics* gdipGraphics = new Gdiplus::Graphics(hWnd);
  170.  
  171. auto imageBoard = Gdiplus::Image::FromFile(IMAGES_FOLDER"board.png");
  172. //Gdiplus::Image imageBoard(L"C:\\cppChess\\board.png");
  173. Gdiplus::Rect drawRect(10, 10, 800, 800);
  174. //gdipGraphics.DrawImage(&imageBoard, drawRect);
  175. //gdipGraphics->DrawImage(imageBoard, drawRect);
  176. gdipGraphics->DrawImage(imageBoard, 10, 10, 800, 800);
  177. }
  178. EndPaint(hWnd, &ps);
  179. }
  180. break;
  181. case WM_LBUTTONDOWN:
  182. {
  183. auto gdipGraphics = Gdiplus::Graphics::FromHWND(hWnd);
  184.  
  185. //auto figurice = Gdiplus::Image::FromFile(L"C:\\cppChess\\pieces.png");
  186. auto figurice = Gdiplus::Image::FromFile(IMAGES_FOLDER"pieces.png");
  187. Gdiplus::Rect drawRect(20+1*100, 20, 80, 80); // iscrtavanje u gornje lijevo polje (bijelo)
  188. Gdiplus::Rect pieceCoords;
  189. //if (pieceType == PIECE_TYPE::KNIGHT)
  190. if (true) // DEBUG
  191. {
  192. pieceCoords.X = 520; pieceCoords.Y = 170+1;
  193. pieceCoords.Width = 155; pieceCoords.Height = 170;
  194. }
  195. gdipGraphics->DrawImage(figurice, drawRect,
  196. pieceCoords.X, pieceCoords.Y, pieceCoords.Height, pieceCoords.Width,
  197. Gdiplus::Unit::UnitPixel);
  198. }
  199. break;
  200. case WM_DESTROY:
  201. PostQuitMessage(0);
  202. break;
  203. default:
  204. return DefWindowProc(hWnd, message, wParam, lParam);
  205. }
  206. return 0;
  207. }
  208.  
  209. // Message handler for about box.
  210. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  211. {
  212. UNREFERENCED_PARAMETER(lParam);
  213. switch (message)
  214. {
  215. case WM_INITDIALOG:
  216. return (INT_PTR)TRUE;
  217.  
  218. case WM_COMMAND:
  219. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  220. {
  221. EndDialog(hDlg, LOWORD(wParam));
  222. return (INT_PTR)TRUE;
  223. }
  224. break;
  225. }
  226. return (INT_PTR)FALSE;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement