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

/Samples/Chap17/FontRot/FontRot.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 235 lines | 177 code | 51 blank | 7 comment | 12 complexity | a85f8fdbf4b489a43f64ed45101d1a86 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module FontRot;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.random;
  12. import std.range;
  13. import std.string;
  14. import std.utf : count, toUTFz;
  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 EZFont;
  27. import resource;
  28. string appName = "FontRot";
  29. string description = "FontRot: Rotated Fonts";
  30. HINSTANCE hinst;
  31. extern (Windows)
  32. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  33. {
  34. int result;
  35. try
  36. {
  37. Runtime.initialize();
  38. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  39. Runtime.terminate();
  40. }
  41. catch (Throwable o)
  42. {
  43. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  44. result = 0;
  45. }
  46. return result;
  47. }
  48. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  49. {
  50. hinst = hInstance;
  51. HACCEL hAccel;
  52. HWND hwnd;
  53. MSG msg;
  54. WNDCLASS wndclass;
  55. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  56. wndclass.lpfnWndProc = &WndProc;
  57. wndclass.cbClsExtra = 0;
  58. wndclass.cbWndExtra = 0;
  59. wndclass.hInstance = hInstance;
  60. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  61. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  62. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  63. wndclass.lpszMenuName = appName.toUTF16z;
  64. wndclass.lpszClassName = appName.toUTF16z;
  65. if (!RegisterClass(&wndclass))
  66. {
  67. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  68. return 0;
  69. }
  70. hwnd = CreateWindow(appName.toUTF16z, // window class name
  71. description.toUTF16z, // window caption
  72. WS_OVERLAPPEDWINDOW, // window style
  73. CW_USEDEFAULT, // initial x position
  74. CW_USEDEFAULT, // initial y position
  75. CW_USEDEFAULT, // initial x size
  76. CW_USEDEFAULT, // initial y size
  77. NULL, // parent window handle
  78. NULL, // window menu handle
  79. hInstance, // program instance handle
  80. NULL); // creation parameters
  81. ShowWindow(hwnd, iCmdShow);
  82. UpdateWindow(hwnd);
  83. while (GetMessage(&msg, NULL, 0, 0))
  84. {
  85. TranslateMessage(&msg);
  86. DispatchMessage(&msg);
  87. }
  88. return msg.wParam;
  89. }
  90. extern (Windows)
  91. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  92. {
  93. scope (failure) assert(0);
  94. static DOCINFO di = DOCINFO(DOCINFO.sizeof, "Font Demo: Printing");
  95. static int cxClient, cyClient;
  96. static PRINTDLG pd = PRINTDLG(PRINTDLG.sizeof);
  97. BOOL fSuccess;
  98. HDC hdc, hdcPrn;
  99. int cxPage, cyPage;
  100. PAINTSTRUCT ps;
  101. switch (message)
  102. {
  103. case WM_COMMAND:
  104. switch (wParam)
  105. {
  106. case IDM_PRINT:
  107. // Get printer DC
  108. pd.hwndOwner = hwnd;
  109. pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  110. if (!PrintDlg(&pd))
  111. return 0;
  112. if (NULL == (hdcPrn = pd.hDC))
  113. {
  114. MessageBox(hwnd, "Cannot obtain Printer DC",
  115. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  116. return 0;
  117. }
  118. // Get size of printable area of page
  119. cxPage = GetDeviceCaps(hdcPrn, HORZRES);
  120. cyPage = GetDeviceCaps(hdcPrn, VERTRES);
  121. fSuccess = FALSE;
  122. // Do the printer page
  123. SetCursor(LoadCursor(NULL, IDC_WAIT));
  124. ShowCursor(TRUE);
  125. if ((StartDoc(hdcPrn, &di) > 0) && (StartPage(hdcPrn) > 0))
  126. {
  127. PaintRoutine(hwnd, hdcPrn, cxPage, cyPage);
  128. if (EndPage(hdcPrn) > 0)
  129. {
  130. fSuccess = TRUE;
  131. EndDoc(hdcPrn);
  132. }
  133. }
  134. DeleteDC(hdcPrn);
  135. ShowCursor(FALSE);
  136. SetCursor(LoadCursor(NULL, IDC_ARROW));
  137. if (!fSuccess)
  138. MessageBox(hwnd,
  139. "Error encountered during printing",
  140. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  141. return 0;
  142. case IDM_ABOUT:
  143. MessageBox(hwnd, "Font Demonstration Program\n(c) Charles Petzold, 1998",
  144. appName.toUTF16z, MB_ICONINFORMATION | MB_OK);
  145. return 0;
  146. default:
  147. }
  148. break;
  149. case WM_SIZE:
  150. cxClient = LOWORD(lParam);
  151. cyClient = HIWORD(lParam);
  152. return 0;
  153. case WM_PAINT:
  154. hdc = BeginPaint(hwnd, &ps);
  155. PaintRoutine(hwnd, hdc, cxClient, cyClient);
  156. EndPaint(hwnd, &ps);
  157. return 0;
  158. case WM_DESTROY:
  159. PostQuitMessage(0);
  160. return 0;
  161. default:
  162. }
  163. return DefWindowProc(hwnd, message, wParam, lParam);
  164. }
  165. void PaintRoutine(HWND hwnd, HDC hdc, int cxArea, int cyArea)
  166. {
  167. static string szString = " Rotation";
  168. HFONT hFont;
  169. int i;
  170. LOGFONT lf;
  171. hFont = EzCreateFont(hdc, "Times New Roman", 540, 0, 0, TRUE);
  172. GetObject(hFont, LOGFONT.sizeof, &lf);
  173. DeleteObject(hFont);
  174. SetBkMode(hdc, TRANSPARENT);
  175. SetTextAlign(hdc, TA_BASELINE);
  176. SetViewportOrgEx(hdc, cxArea / 2, cyArea / 2, NULL);
  177. for (i = 0; i < 12; i++)
  178. {
  179. lf.lfEscapement = lf.lfOrientation = i * 300;
  180. SelectObject(hdc, CreateFontIndirect(&lf));
  181. TextOut(hdc, 0, 0, szString.toUTF16z, szString.count);
  182. DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  183. }
  184. }