PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap05/DevCaps1/DevCaps1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 191 lines | 155 code | 31 blank | 5 comment | 1 complexity | a38a31eb70de782b9f8e2cbf699fe49f MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module DevCaps1;
  6. import core.runtime;
  7. import std.algorithm : min, max;
  8. import std.conv;
  9. import std.math;
  10. import std.string;
  11. import std.utf : count, toUTFz;
  12. auto toUTF16z(S)(S s)
  13. {
  14. return toUTFz!(const(wchar)*)(s);
  15. }
  16. pragma(lib, "gdi32.lib");
  17. pragma(lib, "winmm.lib");
  18. import core.sys.windows.mmsystem;
  19. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. struct DeviceCaps
  23. {
  24. int index;
  25. string label;
  26. string desc;
  27. }
  28. enum devCaps =
  29. [
  30. DeviceCaps(HORZSIZE, "HORZSIZE", "Width in millimeters:"),
  31. DeviceCaps(VERTSIZE, "VERTSIZE", "Height in millimeters:"),
  32. DeviceCaps(HORZRES, "HORZRES", "Width in pixels:"),
  33. DeviceCaps(VERTRES, "VERTRES", "Height in raster lines:"),
  34. DeviceCaps(BITSPIXEL, "BITSPIXEL", "Color bits per pixel:"),
  35. DeviceCaps(PLANES, "PLANES", "Number of color planes:"),
  36. DeviceCaps(NUMBRUSHES, "NUMBRUSHES", "Number of device brushes:"),
  37. DeviceCaps(NUMPENS, "NUMPENS", "Number of device pens:"),
  38. DeviceCaps(NUMMARKERS, "NUMMARKERS", "Number of device markers:"),
  39. DeviceCaps(NUMFONTS, "NUMFONTS", "Number of device fonts:"),
  40. DeviceCaps(NUMCOLORS, "NUMCOLORS", "Number of device colors:"),
  41. DeviceCaps(PDEVICESIZE, "PDEVICESIZE", "Size of device structure:"),
  42. DeviceCaps(ASPECTX, "ASPECTX", "Relative width of pixel:"),
  43. DeviceCaps(ASPECTY, "ASPECTY", "Relative height of pixel:"),
  44. DeviceCaps(ASPECTXY, "ASPECTXY", "Relative diagonal of pixel:"),
  45. DeviceCaps(LOGPIXELSX, "LOGPIXELSX", "Horizontal dots per inch:"),
  46. DeviceCaps(LOGPIXELSY, "LOGPIXELSY", "Vertical dots per inch:"),
  47. DeviceCaps(SIZEPALETTE, "SIZEPALETTE", "Number of palette entries:"),
  48. DeviceCaps(NUMRESERVED, "NUMRESERVED", "Reserved palette entries:"),
  49. DeviceCaps(COLORRES, "COLORRES", "Actual color resolution:")
  50. ];
  51. extern (Windows)
  52. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  53. {
  54. int result;
  55. try
  56. {
  57. Runtime.initialize();
  58. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  59. Runtime.terminate();
  60. }
  61. catch (Throwable o)
  62. {
  63. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  64. result = 0;
  65. }
  66. return result;
  67. }
  68. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  69. {
  70. string appName = "DevCaps1";
  71. HWND hwnd;
  72. MSG msg;
  73. WNDCLASS wndclass;
  74. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  75. wndclass.lpfnWndProc = &WndProc;
  76. wndclass.cbClsExtra = 0;
  77. wndclass.cbWndExtra = 0;
  78. wndclass.hInstance = hInstance;
  79. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  80. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  81. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  82. wndclass.lpszMenuName = NULL;
  83. wndclass.lpszClassName = appName.toUTF16z;
  84. if(!RegisterClass(&wndclass))
  85. {
  86. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  87. return 0;
  88. }
  89. hwnd = CreateWindow(appName.toUTF16z, // window class name
  90. "Device Capabilities", // window caption
  91. WS_OVERLAPPEDWINDOW, // window style
  92. CW_USEDEFAULT, // initial x position
  93. CW_USEDEFAULT, // initial y position
  94. CW_USEDEFAULT, // initial x size
  95. CW_USEDEFAULT, // initial y size
  96. NULL, // parent window handle
  97. NULL, // window menu handle
  98. hInstance, // program instance handle
  99. NULL); // creation parameters
  100. ShowWindow(hwnd, iCmdShow);
  101. UpdateWindow(hwnd);
  102. while(GetMessage(&msg, NULL, 0, 0))
  103. {
  104. TranslateMessage(&msg);
  105. DispatchMessage(&msg);
  106. }
  107. return msg.wParam;
  108. }
  109. extern(Windows)
  110. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  111. {
  112. scope (failure) assert(0);
  113. static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
  114. HDC hdc;
  115. int x, y, iVertPos, iHorzPos, iPaintStart, iPaintEnd;
  116. PAINTSTRUCT ps;
  117. SCROLLINFO si;
  118. TEXTMETRIC tm;
  119. switch(message)
  120. {
  121. case WM_CREATE:
  122. {
  123. hdc = GetDC(hwnd);
  124. scope(exit) ReleaseDC(hwnd, hdc);
  125. GetTextMetrics(hdc, &tm);
  126. cxChar = tm.tmAveCharWidth;
  127. cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  128. cyChar = tm.tmHeight + tm.tmExternalLeading;
  129. // Save the width of the three columns
  130. iMaxWidth = 40 * cxChar + 22 * cxCaps;
  131. return 0;
  132. }
  133. case WM_SIZE:
  134. {
  135. cxClient = LOWORD(lParam);
  136. cyClient = HIWORD(lParam);
  137. return 0;
  138. }
  139. case WM_PAINT:
  140. {
  141. hdc = BeginPaint(hwnd, &ps);
  142. scope(exit) EndPaint(hwnd, &ps);
  143. foreach (index, caps; devCaps)
  144. {
  145. TextOut(hdc, 0, cyChar * index, caps.label.toUTF16z, caps.label.count);
  146. TextOut(hdc, 14 * cxCaps, cyChar * index, caps.desc.toUTF16z, caps.desc.count);
  147. SetTextAlign(hdc, TA_RIGHT | TA_TOP);
  148. auto value = format("%5s", GetDeviceCaps(hdc, caps.index));
  149. TextOut(hdc, 14 * cxCaps + 35 * cxChar, cyChar * index, value.toUTF16z, value.count);
  150. SetTextAlign(hdc, TA_LEFT | TA_TOP);
  151. }
  152. return 0;
  153. }
  154. case WM_DESTROY:
  155. PostQuitMessage(0);
  156. return 0;
  157. default:
  158. }
  159. return DefWindowProc(hwnd, message, wParam, lParam);
  160. }