PageRenderTime 85ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Samples/Chap14/Scramble/Scramble.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 110 lines | 87 code | 19 blank | 4 comment | 5 complexity | 6d3e94311d0c10b1bd6d35b0a38c2355 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Scramble;
  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;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.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. string appName = "Scramble";
  25. string description = "Scramble";
  26. HINSTANCE hinst;
  27. extern (Windows)
  28. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  29. {
  30. int result;
  31. try
  32. {
  33. Runtime.initialize();
  34. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  35. Runtime.terminate();
  36. }
  37. catch (Throwable o)
  38. {
  39. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  40. result = 0;
  41. }
  42. return result;
  43. }
  44. enum NUM = 80;
  45. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  46. {
  47. static int[4][NUM] iKeep;
  48. HDC hdcScr, hdcMem;
  49. int cx, cy;
  50. HBITMAP hBitmap;
  51. HWND hwnd;
  52. int i, j, x1, y1, x2, y2;
  53. if (LockWindowUpdate(hwnd = GetDesktopWindow()))
  54. {
  55. hdcScr = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
  56. hdcMem = CreateCompatibleDC(hdcScr);
  57. cx = GetSystemMetrics(SM_CXSCREEN) / 10;
  58. cy = GetSystemMetrics(SM_CYSCREEN) / 10;
  59. hBitmap = CreateCompatibleBitmap(hdcScr, cx, cy);
  60. SelectObject(hdcMem, hBitmap);
  61. for (i = 0; i < 2; i++)
  62. {
  63. for (j = 0; j < NUM; j++)
  64. {
  65. if (i == 0)
  66. {
  67. iKeep[j][0] = x1 = cx * (uniform(0, 10));
  68. iKeep[j][1] = y1 = cy * (uniform(0, 10));
  69. iKeep[j][2] = x2 = cx * (uniform(0, 10));
  70. iKeep[j][3] = y2 = cy * (uniform(0, 10));
  71. }
  72. else
  73. {
  74. x1 = iKeep[NUM - 1 - j][0];
  75. y1 = iKeep[NUM - 1 - j][1];
  76. x2 = iKeep[NUM - 1 - j][2];
  77. y2 = iKeep[NUM - 1 - j][3];
  78. }
  79. BitBlt(hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY);
  80. BitBlt(hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY);
  81. BitBlt(hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY);
  82. Sleep(10);
  83. }
  84. }
  85. DeleteDC(hdcMem);
  86. ReleaseDC(hwnd, hdcScr);
  87. DeleteObject(hBitmap);
  88. LockWindowUpdate(NULL);
  89. }
  90. return FALSE;
  91. }