PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap14/Stretch/Stretch.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 144 lines | 113 code | 27 blank | 4 comment | 3 complexity | 0eb4a63d8e652d47f363e49d27fab093 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Stretch;
  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. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. import core.sys.windows.winbase;
  23. string appName = "Stretch";
  24. string description = "Stretch";
  25. HINSTANCE hinst;
  26. extern (Windows)
  27. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  28. {
  29. int result;
  30. try
  31. {
  32. Runtime.initialize();
  33. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  34. Runtime.terminate();
  35. }
  36. catch (Throwable o)
  37. {
  38. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  39. result = 0;
  40. }
  41. return result;
  42. }
  43. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  44. {
  45. hinst = hInstance;
  46. HACCEL hAccel;
  47. HWND hwnd;
  48. MSG msg;
  49. WNDCLASS wndclass;
  50. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  51. wndclass.lpfnWndProc = &WndProc;
  52. wndclass.cbClsExtra = 0;
  53. wndclass.cbWndExtra = 0;
  54. wndclass.hInstance = hInstance;
  55. wndclass.hIcon = LoadIcon(NULL, IDI_INFORMATION);
  56. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  57. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  58. wndclass.lpszMenuName = appName.toUTF16z;
  59. wndclass.lpszClassName = appName.toUTF16z;
  60. if (!RegisterClass(&wndclass))
  61. {
  62. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  63. return 0;
  64. }
  65. hwnd = CreateWindow(appName.toUTF16z, // window class name
  66. description.toUTF16z, // window caption
  67. WS_OVERLAPPEDWINDOW, // window style
  68. CW_USEDEFAULT, // initial x position
  69. CW_USEDEFAULT, // initial y position
  70. CW_USEDEFAULT, // initial x size
  71. CW_USEDEFAULT, // initial y size
  72. NULL, // parent window handle
  73. NULL, // window menu handle
  74. hInstance, // program instance handle
  75. NULL); // creation parameters
  76. ShowWindow(hwnd, iCmdShow);
  77. UpdateWindow(hwnd);
  78. while (GetMessage(&msg, NULL, 0, 0))
  79. {
  80. TranslateMessage(&msg);
  81. DispatchMessage(&msg);
  82. }
  83. return msg.wParam;
  84. }
  85. extern (Windows)
  86. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  87. {
  88. scope (failure) assert(0);
  89. static int cxClient, cyClient, cxSource, cySource;
  90. HDC hdcClient, hdcWindow;
  91. PAINTSTRUCT ps;
  92. switch (message)
  93. {
  94. case WM_CREATE:
  95. cxSource = GetSystemMetrics(SM_CXSIZEFRAME) +
  96. GetSystemMetrics(SM_CXSIZE);
  97. cySource = GetSystemMetrics(SM_CYSIZEFRAME) +
  98. GetSystemMetrics(SM_CYCAPTION);
  99. return 0;
  100. case WM_SIZE:
  101. cxClient = LOWORD(lParam);
  102. cyClient = HIWORD(lParam);
  103. return 0;
  104. case WM_PAINT:
  105. hdcClient = BeginPaint(hwnd, &ps);
  106. hdcWindow = GetWindowDC(hwnd);
  107. StretchBlt(hdcClient, 0, 0, cxClient, cyClient, hdcWindow, 0, 0, cxSource, cySource, MERGECOPY);
  108. ReleaseDC(hwnd, hdcWindow);
  109. EndPaint(hwnd, &ps);
  110. return 0;
  111. case WM_DESTROY:
  112. PostQuitMessage(0);
  113. return 0;
  114. default:
  115. }
  116. return DefWindowProc(hwnd, message, wParam, lParam);
  117. }