PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap15/DibSect/DibSect.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 289 lines | 212 code | 64 blank | 13 comment | 22 complexity | faa98de8d5a533f5ed903752848d351e MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module DibSect;
  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. string appName = "DibSect";
  27. string description = "DIB Section Display";
  28. HINSTANCE hinst;
  29. extern (Windows)
  30. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  31. {
  32. int result;
  33. try
  34. {
  35. Runtime.initialize();
  36. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  37. Runtime.terminate();
  38. }
  39. catch (Throwable o)
  40. {
  41. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  42. result = 0;
  43. }
  44. return result;
  45. }
  46. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  47. {
  48. hinst = hInstance;
  49. HACCEL hAccel;
  50. HWND hwnd;
  51. MSG msg;
  52. WNDCLASS wndclass;
  53. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  54. wndclass.lpfnWndProc = &WndProc;
  55. wndclass.cbClsExtra = 0;
  56. wndclass.cbWndExtra = 0;
  57. wndclass.hInstance = hInstance;
  58. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  59. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  60. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  61. wndclass.lpszMenuName = appName.toUTF16z;
  62. wndclass.lpszClassName = appName.toUTF16z;
  63. if (!RegisterClass(&wndclass))
  64. {
  65. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  66. return 0;
  67. }
  68. hwnd = CreateWindow(appName.toUTF16z, // window class name
  69. description.toUTF16z, // window caption
  70. WS_OVERLAPPEDWINDOW, // window style
  71. CW_USEDEFAULT, // initial x position
  72. CW_USEDEFAULT, // initial y position
  73. CW_USEDEFAULT, // initial x size
  74. CW_USEDEFAULT, // initial y size
  75. NULL, // parent window handle
  76. NULL, // window menu handle
  77. hInstance, // program instance handle
  78. NULL); // creation parameters
  79. ShowWindow(hwnd, iCmdShow);
  80. UpdateWindow(hwnd);
  81. while (GetMessage(&msg, NULL, 0, 0))
  82. {
  83. TranslateMessage(&msg);
  84. DispatchMessage(&msg);
  85. }
  86. return msg.wParam;
  87. }
  88. HBITMAP CreateDibSectionFromDibFile(string szFileName)
  89. {
  90. BITMAPFILEHEADER bmfh;
  91. BITMAPINFO* pbmi;
  92. BYTE* pBits;
  93. BOOL bSuccess;
  94. DWORD dwInfoSize, dwBytesRead;
  95. HANDLE hFile;
  96. HBITMAP hBitmap;
  97. // Open the file: read access, prohibit write access
  98. hFile = CreateFile(szFileName.toUTF16z, GENERIC_READ, FILE_SHARE_READ,
  99. NULL, OPEN_EXISTING, 0, NULL);
  100. if (hFile == INVALID_HANDLE_VALUE)
  101. return NULL;
  102. // Read in the BITMAPFILEHEADER
  103. bSuccess = ReadFile(hFile, &bmfh, BITMAPFILEHEADER.sizeof,
  104. &dwBytesRead, NULL);
  105. if (!bSuccess || (dwBytesRead != BITMAPFILEHEADER.sizeof)
  106. || (bmfh.bfType != *cast(WORD*) "BM"))
  107. {
  108. CloseHandle(hFile);
  109. return NULL;
  110. }
  111. // Allocate memory for the BITMAPINFO structure & read it in
  112. dwInfoSize = bmfh.bfOffBits - BITMAPFILEHEADER.sizeof;
  113. pbmi = cast(typeof(pbmi))GC.malloc(dwInfoSize);
  114. bSuccess = ReadFile(hFile, pbmi, dwInfoSize, &dwBytesRead, NULL);
  115. if (!bSuccess || (dwBytesRead != dwInfoSize))
  116. {
  117. GC.free(pbmi);
  118. CloseHandle(hFile);
  119. return NULL;
  120. }
  121. // Create the DIB Section
  122. hBitmap = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, cast(void**)&pBits, NULL, 0);
  123. if (hBitmap == NULL)
  124. {
  125. GC.free(pbmi);
  126. CloseHandle(hFile);
  127. return NULL;
  128. }
  129. // Read in the bitmap bits
  130. ReadFile(hFile, pBits, bmfh.bfSize - bmfh.bfOffBits, &dwBytesRead, NULL);
  131. GC.free(pbmi);
  132. CloseHandle(hFile);
  133. return hBitmap;
  134. }
  135. __gshared wchar[MAX_PATH] szFileName = 0;
  136. __gshared wchar[MAX_PATH] szTitleName = 0;
  137. extern (Windows)
  138. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  139. {
  140. scope (failure) assert(0);
  141. static HBITMAP hBitmap;
  142. static int cxClient, cyClient;
  143. static OPENFILENAME ofn;
  144. static string szFilter = "Bitmap Files (*.BMP)\0*.bmp\0All Files (*.*)\0*.*\0\0";
  145. BITMAP bitmap;
  146. HDC hdc, hdcMem;
  147. PAINTSTRUCT ps;
  148. switch (message)
  149. {
  150. case WM_CREATE:
  151. ofn.hwndOwner = hwnd;
  152. ofn.hInstance = NULL;
  153. ofn.lpstrFilter = szFilter.toUTF16z;
  154. ofn.lpstrCustomFilter = NULL;
  155. ofn.nMaxCustFilter = 0;
  156. ofn.nFilterIndex = 0;
  157. ofn.lpstrFile = szFileName.ptr;
  158. ofn.nMaxFile = MAX_PATH;
  159. ofn.lpstrFileTitle = szTitleName.ptr;
  160. ofn.nMaxFileTitle = MAX_PATH;
  161. ofn.lpstrInitialDir = NULL;
  162. ofn.lpstrTitle = NULL;
  163. ofn.Flags = 0;
  164. ofn.nFileOffset = 0;
  165. ofn.nFileExtension = 0;
  166. ofn.lpstrDefExt = "bmp";
  167. ofn.lCustData = 0;
  168. ofn.lpfnHook = NULL;
  169. ofn.lpTemplateName = NULL;
  170. return 0;
  171. case WM_SIZE:
  172. cxClient = LOWORD(lParam);
  173. cyClient = HIWORD(lParam);
  174. return 0;
  175. case WM_COMMAND:
  176. switch (LOWORD(wParam))
  177. {
  178. case IDM_FILE_OPEN:
  179. // Show the File Open dialog box
  180. if (!GetOpenFileName(&ofn))
  181. return 0;
  182. // If there's an existing bitmap, delete it
  183. if (hBitmap)
  184. {
  185. DeleteObject(hBitmap);
  186. hBitmap = NULL;
  187. }
  188. // Create the DIB Section from the DIB file
  189. SetCursor(LoadCursor(NULL, IDC_WAIT));
  190. ShowCursor(TRUE);
  191. hBitmap = CreateDibSectionFromDibFile(to!string(szFileName[]));
  192. ShowCursor(FALSE);
  193. SetCursor(LoadCursor(NULL, IDC_ARROW));
  194. // Invalidate the client area for later update
  195. InvalidateRect(hwnd, NULL, TRUE);
  196. if (hBitmap == NULL)
  197. {
  198. MessageBox(hwnd, "Cannot load DIB file",
  199. appName.toUTF16z, MB_OK | MB_ICONEXCLAMATION);
  200. }
  201. return 0;
  202. default:
  203. }
  204. break;
  205. case WM_PAINT:
  206. hdc = BeginPaint(hwnd, &ps);
  207. if (hBitmap)
  208. {
  209. GetObject(hBitmap, BITMAP.sizeof, &bitmap);
  210. hdcMem = CreateCompatibleDC(hdc);
  211. SelectObject(hdcMem, hBitmap);
  212. BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
  213. DeleteDC(hdcMem);
  214. }
  215. EndPaint(hwnd, &ps);
  216. return 0;
  217. case WM_DESTROY:
  218. if (hBitmap)
  219. DeleteObject(hBitmap);
  220. PostQuitMessage(0);
  221. return 0;
  222. default:
  223. }
  224. return DefWindowProc(hwnd, message, wParam, lParam);
  225. }