PageRenderTime 106ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap15/DibConv/DibConv.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 283 lines | 214 code | 57 blank | 12 comment | 21 complexity | 1ebacf08e3ba377bdea6c45bc1fd50dd MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module DibConv;
  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.stdio;
  14. import std.utf;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.lib");
  20. pragma(lib, "comdlg32.lib");
  21. import core.sys.windows.windef;
  22. import core.sys.windows.winuser;
  23. import core.sys.windows.wingdi;
  24. import core.sys.windows.winbase;
  25. import core.sys.windows.commdlg;
  26. import resource;
  27. string appName = "DibConv";
  28. string description = "DIB to DDB Conversion";
  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. HBITMAP CreateBitmapObjectFromDibFile(HDC hdc, string szFileName)
  90. {
  91. BITMAPFILEHEADER* pbmfh;
  92. BOOL bSuccess;
  93. DWORD dwFileSize, dwHighSize, dwBytesRead;
  94. HANDLE hFile;
  95. HBITMAP hBitmap;
  96. // Open the file: read access, prohibit write access
  97. hFile = CreateFile(szFileName.toUTF16z, GENERIC_READ, FILE_SHARE_READ, NULL,
  98. OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  99. if (hFile == INVALID_HANDLE_VALUE)
  100. return NULL;
  101. // Read in the whole file
  102. dwFileSize = GetFileSize(hFile, &dwHighSize);
  103. if (dwHighSize)
  104. {
  105. CloseHandle(hFile);
  106. return NULL;
  107. }
  108. pbmfh = cast(typeof(pbmfh))GC.malloc(dwFileSize);
  109. if (!pbmfh)
  110. {
  111. CloseHandle(hFile);
  112. return NULL;
  113. }
  114. bSuccess = ReadFile(hFile, pbmfh, dwFileSize, &dwBytesRead, NULL);
  115. CloseHandle(hFile);
  116. // Verify the file
  117. if (!bSuccess || (dwBytesRead != dwFileSize)
  118. || (pbmfh.bfType != *cast(WORD*) "BM")
  119. || (pbmfh.bfSize != dwFileSize))
  120. {
  121. GC.free(pbmfh);
  122. return NULL;
  123. }
  124. // Create the DDB
  125. hBitmap = CreateDIBitmap(hdc,
  126. cast(BITMAPINFOHEADER*)(pbmfh + 1),
  127. CBM_INIT,
  128. cast(BYTE*)pbmfh + pbmfh.bfOffBits,
  129. cast(BITMAPINFO*)(pbmfh + 1),
  130. DIB_RGB_COLORS);
  131. GC.free(pbmfh);
  132. return hBitmap;
  133. }
  134. __gshared wchar[MAX_PATH] szFileName = 0;
  135. __gshared wchar[MAX_PATH] szTitleName = 0;
  136. extern (Windows)
  137. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  138. {
  139. scope (failure) assert(0);
  140. static HBITMAP hBitmap;
  141. static int cxClient, cyClient;
  142. static OPENFILENAME ofn;
  143. enum szFilter = "Bitmap Files (*.BMP)\0*.bmp\0All Files (*.*)\0*.*\0\0";
  144. BITMAP bitmap;
  145. HDC hdc, hdcMem;
  146. PAINTSTRUCT ps;
  147. switch (message)
  148. {
  149. case WM_CREATE:
  150. ofn.hwndOwner = hwnd;
  151. ofn.hInstance = NULL;
  152. ofn.lpstrFilter = szFilter;
  153. ofn.lpstrCustomFilter = NULL;
  154. ofn.nMaxCustFilter = 0;
  155. ofn.nFilterIndex = 0;
  156. ofn.lpstrFile = szFileName.ptr;
  157. ofn.nMaxFile = MAX_PATH;
  158. ofn.lpstrFileTitle = szTitleName.ptr;
  159. ofn.nMaxFileTitle = MAX_PATH;
  160. ofn.lpstrInitialDir = NULL;
  161. ofn.lpstrTitle = NULL;
  162. ofn.Flags = 0;
  163. ofn.nFileOffset = 0;
  164. ofn.nFileExtension = 0;
  165. ofn.lpstrDefExt = "bmp";
  166. ofn.lCustData = 0;
  167. ofn.lpfnHook = NULL;
  168. ofn.lpTemplateName = NULL;
  169. return 0;
  170. case WM_SIZE:
  171. cxClient = LOWORD(lParam);
  172. cyClient = HIWORD(lParam);
  173. return 0;
  174. case WM_COMMAND:
  175. switch (LOWORD(wParam))
  176. {
  177. case IDM_FILE_OPEN:
  178. // Show the File Open dialog box
  179. if (!GetOpenFileName(&ofn))
  180. return 0;
  181. // If there's an existing DIB, delete it
  182. if (hBitmap)
  183. {
  184. DeleteObject(hBitmap);
  185. hBitmap = NULL;
  186. }
  187. // Create the DDB from the DIB
  188. SetCursor(LoadCursor(NULL, IDC_WAIT));
  189. ShowCursor(TRUE);
  190. hdc = GetDC(hwnd);
  191. hBitmap = CreateBitmapObjectFromDibFile(hdc, to!string(szFileName[]));
  192. ReleaseDC(hwnd, hdc);
  193. ShowCursor(FALSE);
  194. SetCursor(LoadCursor(NULL, IDC_ARROW));
  195. // Invalidate the client area for later update
  196. InvalidateRect(hwnd, NULL, TRUE);
  197. if (hBitmap == NULL)
  198. {
  199. MessageBox(hwnd, "Cannot load DIB file", 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,
  213. hdcMem, 0, 0, SRCCOPY);
  214. DeleteDC(hdcMem);
  215. }
  216. EndPaint(hwnd, &ps);
  217. return 0;
  218. case WM_DESTROY:
  219. if (hBitmap)
  220. DeleteObject(hBitmap);
  221. PostQuitMessage(0);
  222. return 0;
  223. default:
  224. }
  225. return DefWindowProc(hwnd, message, wParam, lParam);
  226. }