Advertisement
c0001

contest.d

Sep 17th, 2019
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.64 KB | None | 0 0
  1. import std.stdio;
  2. import std.string;
  3. import core.thread;
  4. import core.runtime;
  5. import core.sys.windows.windows;
  6.  
  7. pragma(lib, "gdi32.lib");
  8. pragma(lib, "user32.lib");
  9.  
  10. enum {
  11.     VIEW_WIDTH = 480,
  12.     VIEW_HEIGHT = 320,
  13. }
  14.  
  15. HWND hWnd;
  16.  
  17. extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  18.     int result;
  19.     try {
  20.         Runtime.initialize();
  21.         result = appWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
  22.         Runtime.terminate();
  23.     }
  24.     catch (Throwable e) {
  25.         MessageBoxA(null, e.toString().toStringz(), "Error", MB_OK | MB_ICONEXCLAMATION);
  26.         result = 0;
  27.     }
  28.     return result;
  29. }
  30.  
  31. int appWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  32.     writefln("[Main] Starting.");
  33.     scope (exit) writeln("[Main] Exiting.");
  34.  
  35.     auto wclsname = "classname".toStringz;
  36.     auto wtitlestr = "title".toStringz;
  37.     WNDCLASSEXA wndclass;
  38.         wndclass.cbSize = wndclass.sizeof;
  39.     wndclass.style         = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  40.     wndclass.lpfnWndProc   = cast(typeof(wndclass.lpfnWndProc)) &WindowProc;
  41.     wndclass.cbClsExtra    = 0;
  42.     wndclass.cbWndExtra    = 0;
  43.     wndclass.hInstance     = hInstance;
  44.     wndclass.hIcon         = LoadIconA(hInstance, "ICON");
  45.     wndclass.hCursor       = LoadCursorA(null, cast(const(char)*)IDC_ARROW);
  46.     wndclass.hbrBackground = GetStockObject(BLACK_BRUSH); //CreateSolidBrush(RGB(0, 0, 255)); // Must be transparent for client render thread to show while resizing
  47.     wndclass.lpszMenuName  = null;
  48.     wndclass.lpszClassName = wclsname;
  49.     if (!RegisterClassExA(&wndclass)) {
  50.         MessageBoxA(null, "Couldn't register Window Class!", wtitlestr, MB_ICONERROR);
  51.         return 0;
  52.     }
  53.     int dwStyle =
  54.         WS_OVERLAPPEDWINDOW |
  55.         CS_OWNDC |
  56.         WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  57.     int dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  58.     RECT windowRect = RECT(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
  59.     int windowWidth = VIEW_WIDTH;
  60.     int windowHeight = VIEW_HEIGHT;
  61.     writefln("create window %dx%d", VIEW_WIDTH, VIEW_HEIGHT);
  62.     if (AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle)) {
  63.         windowWidth = windowRect.right - windowRect.left;
  64.         windowHeight = windowRect.bottom - windowRect.top;
  65.         //writefln("Adjusted window size: %dx%d (%s)", windowWidth, windowHeight, windowRect);
  66.     }
  67.     hWnd = CreateWindowExA( dwExStyle,
  68.         wclsname, //windowClassName.toStringz,  // window class name
  69.         wtitlestr,       // window caption
  70.         dwStyle,           // window style
  71.         CW_USEDEFAULT,        // initial x position
  72.         CW_USEDEFAULT,        // initial y position
  73.         windowWidth,          // initial x size
  74.         windowHeight,         // initial y size
  75.         null /*HWND_DESKTOP*/,         // parent window handle
  76.         null,                 // window menu handle
  77.         hInstance,            // program instance handle
  78.         null                  // creation parameters
  79.     );
  80.     if (hWnd is null) {
  81.         MessageBoxA(null, "Couldn't create window.", wtitlestr, MB_ICONERROR);
  82.         return 0;
  83.     }
  84.     ShowWindow(hWnd, nCmdShow);
  85.     UpdateWindow(hWnd);
  86.  
  87.     MSG msg;
  88.     MAINLOOP: for (bool running = true; running; ) {
  89.         if (PeekMessageA(&msg, null, 0, 0, PM_REMOVE)) {
  90.             TranslateMessage(&msg);
  91.             DispatchMessageA(&msg);
  92.             MSGLOOP: switch (msg.message) {
  93.                 case WM_QUIT: {
  94.                     writefln("[Main] will quit");
  95.                     running = false;
  96.                     break MAINLOOP;
  97.                 }
  98.                 default: {
  99.                 }
  100.             }
  101.  
  102.         }
  103.     }
  104.     writeln("[Main] Main loop exited.");
  105.     return 0;
  106. }
  107.  
  108. extern(Windows) LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow {
  109.     switch (message) {
  110.         case WM_DESTROY: {
  111.             PostQuitMessage(0);
  112.             break;
  113.         }
  114.         default:
  115.             return DefWindowProcA(hWnd, message, wParam, lParam);
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement