Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.string;
- import core.thread;
- import core.runtime;
- import core.sys.windows.windows;
- pragma(lib, "gdi32.lib");
- pragma(lib, "user32.lib");
- enum {
- VIEW_WIDTH = 480,
- VIEW_HEIGHT = 320,
- }
- HWND hWnd;
- extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- int result;
- try {
- Runtime.initialize();
- result = appWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
- Runtime.terminate();
- }
- catch (Throwable e) {
- MessageBoxA(null, e.toString().toStringz(), "Error", MB_OK | MB_ICONEXCLAMATION);
- result = 0;
- }
- return result;
- }
- int appWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- writefln("[Main] Starting.");
- scope (exit) writeln("[Main] Exiting.");
- auto wclsname = "classname".toStringz;
- auto wtitlestr = "title".toStringz;
- WNDCLASSEXA wndclass;
- wndclass.cbSize = wndclass.sizeof;
- wndclass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = cast(typeof(wndclass.lpfnWndProc)) &WindowProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIconA(hInstance, "ICON");
- wndclass.hCursor = LoadCursorA(null, cast(const(char)*)IDC_ARROW);
- wndclass.hbrBackground = GetStockObject(BLACK_BRUSH); //CreateSolidBrush(RGB(0, 0, 255)); // Must be transparent for client render thread to show while resizing
- wndclass.lpszMenuName = null;
- wndclass.lpszClassName = wclsname;
- if (!RegisterClassExA(&wndclass)) {
- MessageBoxA(null, "Couldn't register Window Class!", wtitlestr, MB_ICONERROR);
- return 0;
- }
- int dwStyle =
- WS_OVERLAPPEDWINDOW |
- CS_OWNDC |
- WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
- int dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
- RECT windowRect = RECT(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
- int windowWidth = VIEW_WIDTH;
- int windowHeight = VIEW_HEIGHT;
- writefln("create window %dx%d", VIEW_WIDTH, VIEW_HEIGHT);
- if (AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle)) {
- windowWidth = windowRect.right - windowRect.left;
- windowHeight = windowRect.bottom - windowRect.top;
- //writefln("Adjusted window size: %dx%d (%s)", windowWidth, windowHeight, windowRect);
- }
- hWnd = CreateWindowExA( dwExStyle,
- wclsname, //windowClassName.toStringz, // window class name
- wtitlestr, // window caption
- dwStyle, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- windowWidth, // initial x size
- windowHeight, // initial y size
- null /*HWND_DESKTOP*/, // parent window handle
- null, // window menu handle
- hInstance, // program instance handle
- null // creation parameters
- );
- if (hWnd is null) {
- MessageBoxA(null, "Couldn't create window.", wtitlestr, MB_ICONERROR);
- return 0;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- MSG msg;
- MAINLOOP: for (bool running = true; running; ) {
- if (PeekMessageA(&msg, null, 0, 0, PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessageA(&msg);
- MSGLOOP: switch (msg.message) {
- case WM_QUIT: {
- writefln("[Main] will quit");
- running = false;
- break MAINLOOP;
- }
- default: {
- }
- }
- }
- }
- writeln("[Main] Main loop exited.");
- return 0;
- }
- extern(Windows) LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow {
- switch (message) {
- case WM_DESTROY: {
- PostQuitMessage(0);
- break;
- }
- default:
- return DefWindowProcA(hWnd, message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement