PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap10/IconDemo/IconDemo.d

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