PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap17/ChosFont/ChosFont.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 232 lines | 173 code | 50 blank | 9 comment | 5 complexity | 7510ce22f93b0c37193e9abd7db0ddb4 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module ChosFont;
  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, "comdlg32.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.commdlg;
  25. import resource;
  26. string appName = "ChosFont";
  27. string description = "ChooseFont";
  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. HACCEL hAccel;
  50. HWND hwnd;
  51. MSG msg;
  52. WNDCLASS wndclass;
  53. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  54. wndclass.lpfnWndProc = &WndProc;
  55. wndclass.cbClsExtra = 0;
  56. wndclass.cbWndExtra = 0;
  57. wndclass.hInstance = hInstance;
  58. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  59. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  60. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  61. wndclass.lpszMenuName = appName.toUTF16z;
  62. wndclass.lpszClassName = appName.toUTF16z;
  63. if (!RegisterClass(&wndclass))
  64. {
  65. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  66. return 0;
  67. }
  68. hwnd = CreateWindow(appName.toUTF16z, // window class name
  69. description.toUTF16z, // window caption
  70. WS_OVERLAPPEDWINDOW, // window style
  71. CW_USEDEFAULT, // initial x position
  72. CW_USEDEFAULT, // initial y position
  73. CW_USEDEFAULT, // initial x size
  74. CW_USEDEFAULT, // initial y size
  75. NULL, // parent window handle
  76. NULL, // window menu handle
  77. hInstance, // program instance handle
  78. NULL); // creation parameters
  79. ShowWindow(hwnd, iCmdShow);
  80. UpdateWindow(hwnd);
  81. while (GetMessage(&msg, NULL, 0, 0))
  82. {
  83. TranslateMessage(&msg);
  84. DispatchMessage(&msg);
  85. }
  86. return msg.wParam;
  87. }
  88. extern (Windows)
  89. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  90. {
  91. scope (failure) assert(0);
  92. static CHOOSEFONT cf;
  93. static int cyChar;
  94. static LOGFONT lf;
  95. dstring szText = "ABCDE abcde ÀÁÂÃÄÅ àáâãäå";
  96. HDC hdc;
  97. int y;
  98. PAINTSTRUCT ps;
  99. string szBuffer;
  100. TEXTMETRIC tm;
  101. switch (message)
  102. {
  103. case WM_CREATE:
  104. // Get height
  105. cyChar = HIWORD(GetDialogBaseUnits());
  106. // Initialize the LOGFONT structure
  107. GetObject(GetStockObject(SYSTEM_FONT), lf.sizeof, &lf);
  108. // Initialize the CHOOSEFONT structure
  109. cf.hwndOwner = hwnd;
  110. cf.hDC = NULL;
  111. cf.lpLogFont = &lf;
  112. cf.iPointSize = 0;
  113. cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS;
  114. cf.rgbColors = 0;
  115. cf.lCustData = 0;
  116. cf.lpfnHook = NULL;
  117. cf.lpTemplateName = NULL;
  118. cf.hInstance = NULL;
  119. cf.lpszStyle = NULL;
  120. cf.nFontType = 0;
  121. cf.nSizeMin = 0;
  122. cf.nSizeMax = 0;
  123. return 0;
  124. case WM_COMMAND:
  125. switch (LOWORD(wParam))
  126. {
  127. case IDM_FONT:
  128. if (ChooseFont(&cf))
  129. InvalidateRect(hwnd, NULL, TRUE);
  130. return 0;
  131. default:
  132. }
  133. return 0;
  134. case WM_PAINT:
  135. {
  136. hdc = BeginPaint(hwnd, &ps);
  137. // Display sample using selected font
  138. SelectObject(hdc, CreateFontIndirect(&lf));
  139. GetTextMetrics(hdc, &tm);
  140. SetTextColor(hdc, cf.rgbColors);
  141. TextOut(hdc, 0, y = tm.tmExternalLeading, to!string(szText).toUTF16z, szText.count);
  142. // Display LOGFONT structure fields using system font
  143. DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  144. SetTextColor(hdc, 0);
  145. szBuffer = format("lfHeight = %s", lf.lfHeight);
  146. TextOut(hdc, 0, y += tm.tmHeight, szBuffer.toUTF16z, szBuffer.count);
  147. szBuffer = format("lfWidth = %s", lf.lfWidth);
  148. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  149. szBuffer = format("lfEscapement = %s", lf.lfEscapement);
  150. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  151. szBuffer = format("lfOrientation = %s", lf.lfOrientation);
  152. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  153. szBuffer = format("lfWeight = %s", lf.lfWeight);
  154. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  155. szBuffer = format("lfItalic = %s", lf.lfItalic);
  156. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  157. szBuffer = format("lfUnderline = %s", lf.lfUnderline);
  158. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  159. szBuffer = format("lfStrikeOut = %s", lf.lfStrikeOut);
  160. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  161. szBuffer = format("lfCharSet = %s", lf.lfCharSet);
  162. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  163. szBuffer = format("lfOutPrecision = %s", lf.lfOutPrecision);
  164. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  165. szBuffer = format("lfClipPrecision = %s", lf.lfClipPrecision);
  166. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  167. szBuffer = format("lfQuality = %s", lf.lfQuality);
  168. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  169. szBuffer = format("lfPitchAndFamily = 0x%02X", lf.lfPitchAndFamily);
  170. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  171. szBuffer = format("lfFaceName = %s", lf.lfFaceName);
  172. TextOut(hdc, 0, y += cyChar, szBuffer.toUTF16z, szBuffer.count);
  173. EndPaint(hwnd, &ps);
  174. return 0;
  175. }
  176. case WM_DESTROY:
  177. PostQuitMessage(0);
  178. return 0;
  179. default:
  180. }
  181. return DefWindowProc(hwnd, message, wParam, lParam);
  182. }