PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap13/Print1/Print.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 243 lines | 191 code | 48 blank | 4 comment | 12 complexity | 6c3a035efc06ba5b3434587e4c5c32d1 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Print;
  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 = "Print1";
  26. string description = "Print Program 1";
  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. void PageGDICalls(HDC hdcPrn, int cxPage, int cyPage)
  88. {
  89. string szTextStr = "Hello, Printer!";
  90. Rectangle(hdcPrn, 0, 0, cxPage, cyPage);
  91. MoveToEx(hdcPrn, 0, 0, NULL);
  92. LineTo(hdcPrn, cxPage, cyPage);
  93. MoveToEx(hdcPrn, cxPage, 0, NULL);
  94. LineTo(hdcPrn, 0, cyPage);
  95. SaveDC(hdcPrn);
  96. SetMapMode(hdcPrn, MM_ISOTROPIC);
  97. SetWindowExtEx(hdcPrn, 1000, 1000, NULL);
  98. SetViewportExtEx(hdcPrn, cxPage / 2, -cyPage / 2, NULL);
  99. SetViewportOrgEx(hdcPrn, cxPage / 2, cyPage / 2, NULL);
  100. Ellipse(hdcPrn, -500, 500, 500, -500);
  101. SetTextAlign(hdcPrn, TA_BASELINE | TA_CENTER);
  102. TextOut(hdcPrn, 0, 0, szTextStr.toUTF16z, szTextStr.count);
  103. RestoreDC(hdcPrn, -1);
  104. }
  105. extern(Windows)
  106. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  107. {
  108. scope (failure) assert(0);
  109. static int cxClient, cyClient;
  110. HDC hdc;
  111. HMENU hMenu;
  112. PAINTSTRUCT ps;
  113. switch (message)
  114. {
  115. case WM_CREATE:
  116. hMenu = GetSystemMenu(hwnd, false);
  117. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  118. AppendMenu(hMenu, 0, 1, "&Print");
  119. return 0;
  120. case WM_SIZE:
  121. cxClient = LOWORD(lParam);
  122. cyClient = HIWORD(lParam);
  123. return 0;
  124. case WM_SYSCOMMAND:
  125. if (wParam == 1)
  126. {
  127. if (!PrintMyPage(hwnd))
  128. MessageBox(hwnd, "Could not print page!",
  129. appName.toUTF16z, MB_OK | MB_ICONEXCLAMATION);
  130. return 0;
  131. }
  132. break;
  133. case WM_PAINT:
  134. hdc = BeginPaint(hwnd, &ps);
  135. PageGDICalls(hdc, cxClient, cyClient);
  136. EndPaint(hwnd, &ps);
  137. return 0;
  138. case WM_DESTROY:
  139. PostQuitMessage(0);
  140. return 0;
  141. default:
  142. }
  143. return DefWindowProc(hwnd, message, wParam, lParam);
  144. }
  145. bool PrintMyPage(HWND hwnd)
  146. {
  147. static DOCINFO di = DOCINFO(DOCINFO.sizeof, "Print1: Printing");
  148. bool bSuccess = true; // signaling success in advance is a pretty bad idea
  149. HDC hdcPrn;
  150. int xPage, yPage;
  151. hdcPrn = GetPrinterDC();
  152. if (hdcPrn is null)
  153. return false;
  154. scope(exit) DeleteDC(hdcPrn);
  155. xPage = GetDeviceCaps(hdcPrn, HORZRES);
  156. yPage = GetDeviceCaps(hdcPrn, VERTRES);
  157. if (StartDoc(hdcPrn, &di) > 0)
  158. {
  159. if (StartPage(hdcPrn) > 0)
  160. {
  161. PageGDICalls(hdcPrn, xPage, yPage);
  162. if (EndPage(hdcPrn) > 0)
  163. EndDoc(hdcPrn);
  164. else
  165. bSuccess = false;
  166. }
  167. }
  168. else
  169. bSuccess = false;
  170. return bSuccess;
  171. }
  172. HDC GetPrinterDC()
  173. {
  174. DWORD dwNeeded, dwReturned;
  175. HDC hdc;
  176. PRINTER_INFO_4* pinfo4;
  177. PRINTER_INFO_5* pinfo5;
  178. if (GetVersion() & 0x80000000) // Windows 98
  179. {
  180. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
  181. pinfo5 = cast(typeof(pinfo5))GC.malloc(dwNeeded);
  182. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, cast(PBYTE)pinfo5, dwNeeded, &dwNeeded, &dwReturned);
  183. hdc = CreateDC(NULL, pinfo5.pPrinterName, NULL, NULL);
  184. GC.free(pinfo5);
  185. }
  186. else // Windows NT
  187. {
  188. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &dwNeeded, &dwReturned);
  189. pinfo4 = cast(typeof(pinfo4))GC.malloc(dwNeeded);
  190. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, cast(PBYTE)pinfo4, dwNeeded, &dwNeeded, &dwReturned);
  191. hdc = CreateDC(NULL, pinfo4.pPrinterName, NULL, NULL);
  192. GC.free(pinfo4);
  193. }
  194. return hdc;
  195. }