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

/Samples/Chap13/FormFeed/FormFeed.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 99 lines | 78 code | 17 blank | 4 comment | 7 complexity | 3c350c62677349045f1d24d78f67dc5c MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module FormFeed;
  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, "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 = "FormFeed";
  26. string description = "FormFeed";
  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. HDC GetPrinterDC()
  47. {
  48. DWORD dwNeeded, dwReturned;
  49. HDC hdc;
  50. PRINTER_INFO_4* pinfo4;
  51. PRINTER_INFO_5* pinfo5;
  52. if (GetVersion() & 0x80000000) // Windows 98
  53. {
  54. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
  55. pinfo5 = cast(typeof(pinfo5))GC.malloc(dwNeeded);
  56. EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, cast(PBYTE)pinfo5, dwNeeded, &dwNeeded, &dwReturned);
  57. hdc = CreateDC(NULL, pinfo5.pPrinterName, NULL, NULL);
  58. GC.free(pinfo5);
  59. }
  60. else // Windows NT
  61. {
  62. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &dwNeeded, &dwReturned);
  63. pinfo4 = cast(typeof(pinfo4))GC.malloc(dwNeeded);
  64. EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, cast(PBYTE)pinfo4, dwNeeded, &dwNeeded, &dwReturned);
  65. hdc = CreateDC(NULL, pinfo4.pPrinterName, NULL, NULL);
  66. GC.free(pinfo4);
  67. }
  68. return hdc;
  69. }
  70. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  71. {
  72. static DOCINFO di = DOCINFO(DOCINFO.sizeof, "FormFeed");
  73. HDC hdcPrint = GetPrinterDC();
  74. if (hdcPrint != NULL)
  75. {
  76. if (StartDoc(hdcPrint, &di) > 0)
  77. if (StartPage(hdcPrint) > 0 && EndPage(hdcPrint) > 0)
  78. EndDoc(hdcPrint);
  79. DeleteDC(hdcPrint);
  80. }
  81. return 0;
  82. }