PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap13/Print2/Print2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 268 lines | 205 code | 54 blank | 9 comment | 13 complexity | 0ce0f3e4b00032cb3b39fe92c4f35e5b MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Print2;
  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 : count, toUTFz;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "winspool.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.winspool;
  25. string appName = "Print2";
  26. string description = "Print Program 2";
  27. enum ID_TIMER = 1;
  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. HWND hwnd;
  50. MSG msg;
  51. WNDCLASS wndclass;
  52. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53. wndclass.lpfnWndProc = &WndProc;
  54. wndclass.cbClsExtra = 0;
  55. wndclass.cbWndExtra = 0;
  56. wndclass.hInstance = hInstance;
  57. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  58. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  60. wndclass.lpszMenuName = appName.toUTF16z;
  61. wndclass.lpszClassName = appName.toUTF16z;
  62. if (!RegisterClass(&wndclass))
  63. {
  64. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  65. return 0;
  66. }
  67. hwnd = CreateWindow(appName.toUTF16z, // window class name
  68. description.toUTF16z, // window caption
  69. WS_OVERLAPPEDWINDOW, // window style
  70. CW_USEDEFAULT, // initial x position
  71. CW_USEDEFAULT, // initial y position
  72. CW_USEDEFAULT, // initial x size
  73. CW_USEDEFAULT, // initial y size
  74. NULL, // parent window handle
  75. NULL, // window menu handle
  76. hInstance, // program instance handle
  77. NULL); // creation parameters
  78. ShowWindow(hwnd, iCmdShow);
  79. UpdateWindow(hwnd);
  80. while (GetMessage(&msg, NULL, 0, 0))
  81. {
  82. TranslateMessage(&msg);
  83. DispatchMessage(&msg);
  84. }
  85. return msg.wParam;
  86. }
  87. extern (Windows)
  88. int AbortProc(HDC hdcPrn, int iCode)
  89. {
  90. MSG msg;
  91. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  92. {
  93. TranslateMessage(&msg);
  94. DispatchMessage(&msg);
  95. }
  96. return TRUE;
  97. }
  98. void PageGDICalls(HDC hdcPrn, int cxPage, int cyPage)
  99. {
  100. string szTextStr = "Hello, Printer!";
  101. Rectangle(hdcPrn, 0, 0, cxPage, cyPage);
  102. MoveToEx(hdcPrn, 0, 0, NULL);
  103. LineTo(hdcPrn, cxPage, cyPage);
  104. MoveToEx(hdcPrn, cxPage, 0, NULL);
  105. LineTo(hdcPrn, 0, cyPage);
  106. SaveDC(hdcPrn);
  107. SetMapMode(hdcPrn, MM_ISOTROPIC);
  108. SetWindowExtEx(hdcPrn, 1000, 1000, NULL);
  109. SetViewportExtEx(hdcPrn, cxPage / 2, -cyPage / 2, NULL);
  110. SetViewportOrgEx(hdcPrn, cxPage / 2, cyPage / 2, NULL);
  111. Ellipse(hdcPrn, -500, 500, 500, -500);
  112. SetTextAlign(hdcPrn, TA_BASELINE | TA_CENTER);
  113. TextOut(hdcPrn, 0, 0, szTextStr.toUTF16z, szTextStr.count);
  114. RestoreDC(hdcPrn, -1);
  115. }
  116. extern (Windows)
  117. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  118. {
  119. scope (failure) assert(0);
  120. static int cxClient, cyClient;
  121. HDC hdc;
  122. HMENU hMenu;
  123. PAINTSTRUCT ps;
  124. switch (message)
  125. {
  126. case WM_CREATE:
  127. hMenu = GetSystemMenu(hwnd, false);
  128. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  129. AppendMenu(hMenu, 0, 1, "&Print");
  130. return 0;
  131. case WM_SIZE:
  132. cxClient = LOWORD(lParam);
  133. cyClient = HIWORD(lParam);
  134. return 0;
  135. case WM_SYSCOMMAND:
  136. if (wParam == 1)
  137. {
  138. if (!PrintMyPage(hwnd))
  139. MessageBox(hwnd, "Could not print page!",
  140. appName.toUTF16z, MB_OK | MB_ICONEXCLAMATION);
  141. return 0;
  142. }
  143. break;
  144. case WM_PAINT:
  145. hdc = BeginPaint(hwnd, &ps);
  146. PageGDICalls(hdc, cxClient, cyClient);
  147. EndPaint(hwnd, &ps);
  148. return 0;
  149. case WM_DESTROY:
  150. PostQuitMessage(0);
  151. return 0;
  152. default:
  153. }
  154. return DefWindowProc(hwnd, message, wParam, lParam);
  155. }
  156. bool PrintMyPage(HWND hwnd)
  157. {
  158. static DOCINFO di = DOCINFO(DOCINFO.sizeof, "Print2: Printing");
  159. bool bSuccess = true; // signaling success in advance is a pretty bad idea
  160. HDC hdcPrn;
  161. int xPage, yPage;
  162. hdcPrn = GetPrinterDC();
  163. if (hdcPrn is null)
  164. return false;
  165. scope (exit) DeleteDC(hdcPrn);
  166. xPage = GetDeviceCaps(hdcPrn, HORZRES);
  167. yPage = GetDeviceCaps(hdcPrn, VERTRES);
  168. EnableWindow(hwnd, FALSE);
  169. // @BUG@ WindowsAPI callbacks are not defined properly:
  170. // alias BOOL function(HDC, int) ABORTPROC;
  171. //
  172. // should be:
  173. // alias extern(Windows) BOOL function(HDC, int) ABORTPROC;
  174. SetAbortProc(hdcPrn, cast(BOOL function(HDC, int))&AbortProc);
  175. if (StartDoc(hdcPrn, &di) > 0)
  176. {
  177. if (StartPage(hdcPrn) > 0)
  178. {
  179. PageGDICalls(hdcPrn, xPage, yPage);
  180. if (EndPage(hdcPrn) > 0)
  181. EndDoc(hdcPrn);
  182. else
  183. bSuccess = false;
  184. }
  185. }
  186. else
  187. bSuccess = false;
  188. EnableWindow(hwnd, TRUE);
  189. return bSuccess;
  190. }
  191. HDC GetPrinterDC()
  192. {
  193. DWORD dwNeeded, dwReturned;
  194. HDC hdc;
  195. PRINTER_INFO_4* pinfo4;
  196. PRINTER_INFO_5* pinfo5;
  197. if (GetVersion() & 0x80000000) // Windows 98
  198. {
  199. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
  200. pinfo5 = cast(typeof(pinfo5))GC.malloc(dwNeeded);
  201. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, cast(PBYTE)pinfo5, dwNeeded, &dwNeeded, &dwReturned);
  202. hdc = CreateDC(NULL, pinfo5.pPrinterName, NULL, NULL);
  203. GC.free(pinfo5);
  204. }
  205. else // Windows NT
  206. {
  207. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &dwNeeded, &dwReturned);
  208. pinfo4 = cast(typeof(pinfo4))GC.malloc(dwNeeded);
  209. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, cast(PBYTE)pinfo4, dwNeeded, &dwNeeded, &dwReturned);
  210. hdc = CreateDC(NULL, pinfo4.pPrinterName, NULL, NULL);
  211. GC.free(pinfo4);
  212. }
  213. return hdc;
  214. }