PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap15/ShowDib1/ShowDib1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 255 lines | 187 code | 56 blank | 12 comment | 14 complexity | 7c56d249bd58b612030f18df94b1cba2 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module ShowDib1;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. import core.sys.windows.commdlg;
  25. import resource;
  26. import DibFile;
  27. string appName = "ShowDib1";
  28. string description = "Show DIB #1";
  29. HINSTANCE hinst;
  30. extern (Windows)
  31. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  32. {
  33. int result;
  34. try
  35. {
  36. Runtime.initialize();
  37. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  38. Runtime.terminate();
  39. }
  40. catch (Throwable o)
  41. {
  42. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  43. result = 0;
  44. }
  45. return result;
  46. }
  47. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  48. {
  49. hinst = hInstance;
  50. HACCEL hAccel;
  51. HWND hwnd;
  52. MSG msg;
  53. WNDCLASS wndclass;
  54. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  55. wndclass.lpfnWndProc = &WndProc;
  56. wndclass.cbClsExtra = 0;
  57. wndclass.cbWndExtra = 0;
  58. wndclass.hInstance = hInstance;
  59. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  60. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  61. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  62. wndclass.lpszMenuName = appName.toUTF16z;
  63. wndclass.lpszClassName = appName.toUTF16z;
  64. if (!RegisterClass(&wndclass))
  65. {
  66. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  67. return 0;
  68. }
  69. hwnd = CreateWindow(appName.toUTF16z, // window class name
  70. description.toUTF16z, // window caption
  71. WS_OVERLAPPEDWINDOW, // window style
  72. CW_USEDEFAULT, // initial x position
  73. CW_USEDEFAULT, // initial y position
  74. CW_USEDEFAULT, // initial x size
  75. CW_USEDEFAULT, // initial y size
  76. NULL, // parent window handle
  77. NULL, // window menu handle
  78. hInstance, // program instance handle
  79. NULL); // creation parameters
  80. ShowWindow(hwnd, iCmdShow);
  81. UpdateWindow(hwnd);
  82. while (GetMessage(&msg, NULL, 0, 0))
  83. {
  84. TranslateMessage(&msg);
  85. DispatchMessage(&msg);
  86. }
  87. return msg.wParam;
  88. }
  89. __gshared wchar[MAX_PATH] szFileName = 0;
  90. __gshared wchar[MAX_PATH] szTitleName = 0;
  91. extern (Windows)
  92. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  93. {
  94. scope (failure) assert(0);
  95. static BITMAPFILEHEADER* pbmfh;
  96. static BITMAPINFO* pbmi;
  97. static BYTE* pBits;
  98. static int cxClient, cyClient, cxDib, cyDib;
  99. BOOL bSuccess;
  100. HDC hdc;
  101. PAINTSTRUCT ps;
  102. switch (message)
  103. {
  104. case WM_CREATE:
  105. DibFileInitialize(hwnd);
  106. return 0;
  107. case WM_SIZE:
  108. cxClient = LOWORD(lParam);
  109. cyClient = HIWORD(lParam);
  110. return 0;
  111. case WM_INITMENUPOPUP:
  112. EnableMenuItem(cast(HMENU)wParam, IDM_FILE_SAVE,
  113. pbmfh ? MF_ENABLED : MF_GRAYED);
  114. return 0;
  115. case WM_COMMAND:
  116. switch (LOWORD(wParam))
  117. {
  118. case IDM_FILE_OPEN:
  119. // Show the File Open dialog box
  120. if (!DibFileOpenDlg(hwnd, szFileName.ptr, szTitleName.ptr))
  121. return 0;
  122. // If there's an existing DIB, GC.free the memory
  123. if (pbmfh)
  124. {
  125. GC.free(pbmfh);
  126. pbmfh = NULL;
  127. }
  128. // Load the entire DIB into memory
  129. SetCursor(LoadCursor(NULL, IDC_WAIT));
  130. ShowCursor(TRUE);
  131. pbmfh = DibLoadImage(to!string(szFileName[]));
  132. ShowCursor(FALSE);
  133. SetCursor(LoadCursor(NULL, IDC_ARROW));
  134. // Invalidate the client area for later update
  135. InvalidateRect(hwnd, NULL, TRUE);
  136. if (pbmfh == NULL)
  137. {
  138. MessageBox(hwnd, "Cannot load DIB file",
  139. appName.toUTF16z, 0);
  140. return 0;
  141. }
  142. // Get pointers to the info structure & the bits
  143. pbmi = cast(BITMAPINFO*)(pbmfh + 1);
  144. pBits = cast(BYTE*)pbmfh + pbmfh.bfOffBits;
  145. // Get the DIB width and height
  146. if (pbmi.bmiHeader.biSize == BITMAPCOREHEADER.sizeof)
  147. {
  148. cxDib = (cast(BITMAPCOREHEADER*)pbmi).bcWidth;
  149. cyDib = (cast(BITMAPCOREHEADER*)pbmi).bcHeight;
  150. }
  151. else
  152. {
  153. cxDib = pbmi.bmiHeader.biWidth;
  154. cyDib = abs(pbmi.bmiHeader.biHeight);
  155. }
  156. return 0;
  157. case IDM_FILE_SAVE:
  158. // Show the File Save dialog box
  159. if (!DibFileSaveDlg(hwnd, szFileName.ptr, szTitleName.ptr))
  160. return 0;
  161. // Save the DIB to memory
  162. SetCursor(LoadCursor(NULL, IDC_WAIT));
  163. ShowCursor(TRUE);
  164. bSuccess = DibSaveImage(szFileName.ptr, pbmfh);
  165. ShowCursor(FALSE);
  166. SetCursor(LoadCursor(NULL, IDC_ARROW));
  167. if (!bSuccess)
  168. MessageBox(hwnd, "Cannot save DIB file",
  169. appName.toUTF16z, 0);
  170. return 0;
  171. default:
  172. }
  173. break;
  174. case WM_PAINT:
  175. hdc = BeginPaint(hwnd, &ps);
  176. if (pbmfh)
  177. SetDIBitsToDevice(hdc,
  178. 0, // xDst
  179. 0, // yDst
  180. cxDib, // cxSrc
  181. cyDib, // cySrc
  182. 0, // xSrc
  183. 0, // ySrc
  184. 0, // first scan line
  185. cyDib, // number of scan lines
  186. pBits,
  187. pbmi,
  188. DIB_RGB_COLORS);
  189. EndPaint(hwnd, &ps);
  190. return 0;
  191. case WM_DESTROY:
  192. if (pbmfh)
  193. GC.free(pbmfh);
  194. PostQuitMessage(0);
  195. return 0;
  196. default:
  197. }
  198. return DefWindowProc(hwnd, message, wParam, lParam);
  199. }