PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap03/HelloWin/HelloWin.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 124 lines | 97 code | 23 blank | 4 comment | 3 complexity | 1563ea9905ebb8569715c07e36fd4354 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module HelloWin;
  6. import core.runtime;
  7. import std.string;
  8. import std.utf;
  9. auto toUTF16z(S)(S s)
  10. {
  11. return toUTFz!(const(wchar)*)(s);
  12. }
  13. pragma(lib, "gdi32.lib");
  14. pragma(lib, "winmm.lib");
  15. import core.sys.windows.mmsystem;
  16. import core.sys.windows.windef;
  17. import core.sys.windows.winuser;
  18. import core.sys.windows.wingdi;
  19. extern(Windows)
  20. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  21. {
  22. int result;
  23. try
  24. {
  25. Runtime.initialize();
  26. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  27. Runtime.terminate();
  28. }
  29. catch(Throwable o)
  30. {
  31. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  32. result = 0;
  33. }
  34. return result;
  35. }
  36. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  37. {
  38. string appName = "HelloWin";
  39. HWND hwnd;
  40. MSG msg;
  41. WNDCLASS wndclass;
  42. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  43. wndclass.lpfnWndProc = &WndProc;
  44. wndclass.cbClsExtra = 0;
  45. wndclass.cbWndExtra = 0;
  46. wndclass.hInstance = hInstance;
  47. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  48. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  49. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  50. wndclass.lpszMenuName = NULL;
  51. wndclass.lpszClassName = appName.toUTF16z;
  52. if(!RegisterClass(&wndclass))
  53. {
  54. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  55. return 0;
  56. }
  57. hwnd = CreateWindow(appName.toUTF16z, // window class name
  58. "The Hello Program", // window caption
  59. WS_OVERLAPPEDWINDOW, // window style
  60. CW_USEDEFAULT, // initial x position
  61. CW_USEDEFAULT, // initial y position
  62. CW_USEDEFAULT, // initial x size
  63. CW_USEDEFAULT, // initial y size
  64. NULL, // parent window handle
  65. NULL, // window menu handle
  66. hInstance, // program instance handle
  67. NULL); // creation parameters
  68. ShowWindow(hwnd, iCmdShow);
  69. UpdateWindow(hwnd);
  70. while (GetMessage(&msg, NULL, 0, 0))
  71. {
  72. TranslateMessage(&msg);
  73. DispatchMessage(&msg);
  74. }
  75. return msg.wParam;
  76. }
  77. extern(Windows)
  78. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  79. {
  80. scope (failure) assert(0);
  81. HDC hdc;
  82. PAINTSTRUCT ps;
  83. RECT rect;
  84. switch (message)
  85. {
  86. case WM_CREATE:
  87. PlaySound("hellowin.wav", NULL, SND_FILENAME | SND_ASYNC);
  88. return 0;
  89. case WM_PAINT:
  90. hdc = BeginPaint(hwnd, &ps);
  91. scope(exit) EndPaint(hwnd, &ps);
  92. GetClientRect(hwnd, &rect);
  93. DrawText(hdc, "Hello, Windows!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  94. return 0;
  95. case WM_DESTROY:
  96. PostQuitMessage(0);
  97. return 0;
  98. default:
  99. }
  100. return DefWindowProc(hwnd, message, wParam, lParam);
  101. }