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

/Samples/Chap07/Connect/Connect.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 167 lines | 131 code | 32 blank | 4 comment | 7 complexity | f44c7445f6f2e7d05da5915db2a4c8db MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Connect;
  6. import core.runtime;
  7. import core.thread;
  8. import std.conv;
  9. import std.math;
  10. import std.range;
  11. import std.string;
  12. import std.utf;
  13. auto toUTF16z(S)(S s)
  14. {
  15. return toUTFz!(const(wchar)*)(s);
  16. }
  17. pragma(lib, "gdi32.lib");
  18. import core.sys.windows.windef;
  19. import core.sys.windows.winuser;
  20. import core.sys.windows.wingdi;
  21. extern (Windows)
  22. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  23. {
  24. int result;
  25. try
  26. {
  27. Runtime.initialize();
  28. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  29. Runtime.terminate();
  30. }
  31. catch (Throwable o)
  32. {
  33. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  34. result = 0;
  35. }
  36. return result;
  37. }
  38. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  39. {
  40. string appName = "Connect";
  41. HWND hwnd;
  42. MSG msg;
  43. WNDCLASS wndclass;
  44. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  45. wndclass.lpfnWndProc = &WndProc;
  46. wndclass.cbClsExtra = 0;
  47. wndclass.cbWndExtra = 0;
  48. wndclass.hInstance = hInstance;
  49. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  50. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  51. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  52. wndclass.lpszMenuName = NULL;
  53. wndclass.lpszClassName = appName.toUTF16z;
  54. if (!RegisterClass(&wndclass))
  55. {
  56. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  57. return 0;
  58. }
  59. hwnd = CreateWindow(appName.toUTF16z, // window class name
  60. "Connect-the-Points Mouse Demo", // window caption
  61. WS_OVERLAPPEDWINDOW, // window style
  62. CW_USEDEFAULT, // initial x position
  63. CW_USEDEFAULT, // initial y position
  64. CW_USEDEFAULT, // initial x size
  65. CW_USEDEFAULT, // initial y size
  66. NULL, // parent window handle
  67. NULL, // window menu handle
  68. hInstance, // program instance handle
  69. NULL); // creation parameters
  70. ShowWindow(hwnd, iCmdShow);
  71. UpdateWindow(hwnd);
  72. while (GetMessage(&msg, NULL, 0, 0))
  73. {
  74. TranslateMessage(&msg);
  75. DispatchMessage(&msg);
  76. }
  77. return msg.wParam;
  78. }
  79. extern (Windows)
  80. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  81. {
  82. scope (failure) assert(0);
  83. enum MAXPOINTS = 1000;
  84. static POINT[MAXPOINTS] pt;
  85. static int iCount;
  86. HDC hdc;
  87. int i, j;
  88. PAINTSTRUCT ps;
  89. switch (message)
  90. {
  91. case WM_LBUTTONDOWN:
  92. iCount = 0;
  93. InvalidateRect(hwnd, NULL, TRUE);
  94. return 0;
  95. case WM_MOUSEMOVE:
  96. {
  97. if (wParam & MK_LBUTTON && iCount < 1000)
  98. {
  99. pt[iCount].x = LOWORD(lParam);
  100. pt[iCount].y = HIWORD(lParam);
  101. iCount++;
  102. hdc = GetDC(hwnd);
  103. SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0);
  104. ReleaseDC(hwnd, hdc);
  105. }
  106. return 0;
  107. }
  108. case WM_LBUTTONUP:
  109. InvalidateRect(hwnd, NULL, FALSE);
  110. return 0;
  111. case WM_PAINT:
  112. {
  113. hdc = BeginPaint(hwnd, &ps);
  114. SetCursor(LoadCursor(NULL, IDC_WAIT));
  115. ShowCursor(TRUE);
  116. for (i = 0; i < iCount - 1; i++)
  117. {
  118. for (j = i + 1; j < iCount; j++)
  119. {
  120. MoveToEx(hdc, pt[i].x, pt[i].y, NULL);
  121. LineTo(hdc, pt[j].x, pt[j].y);
  122. }
  123. }
  124. ShowCursor(FALSE);
  125. SetCursor(LoadCursor(NULL, IDC_ARROW));
  126. EndPaint(hwnd, &ps);
  127. return 0;
  128. }
  129. case WM_DESTROY:
  130. PostQuitMessage(0);
  131. return 0;
  132. default:
  133. }
  134. return DefWindowProc(hwnd, message, wParam, lParam);
  135. }