Esempio n. 1
0
        public static Window Create(string title = "window", int show = C.SW_SHOW, HWND parent = default)
        {
            var ms = Assembly.GetEntryAssembly().GetModules();
            // var hInstance = Marshal.GetHINSTANCE(ms[0]);
            var hInstance = default(HINSTANCE);

            var window = new Window(title, s_count++);

            var wc = new WNDCLASSEXW
            {
                cbSize        = (uint)Marshal.SizeOf(typeof(WNDCLASSEXW)),
                style         = C.CS_VREDRAW | C.CS_HREDRAW | C.CS_DBLCLKS,
                lpszClassName = window.m_className.ToString(),
                lpfnWndProc   = window.m_delegate,
                hInstance     = hInstance,
                // hCursor = user32.LoadCursorW(default, C.IDC_ARROW),
            };
            var register = user32.RegisterClassExW(ref wc);

            if (register == 0)
            {
                return(null);
            }

            var hwnd = user32.CreateWindowExW(0,
                                              window.m_className,
                                              window.m_title,
                                              C.WS_OVERLAPPEDWINDOW,
                                              C.CW_USEDEFAULT,
                                              C.CW_USEDEFAULT,
                                              C.CW_USEDEFAULT,
                                              C.CW_USEDEFAULT,
                                              parent,
Esempio n. 2
0
        static ushort CreateClassAtom()
        {
            ushort classAtom;

            fixed(char *lpszClassName = $"{nameof(UIService)}.X{EntryPointModule:X16}")
            {
                var wndClassEx = new WNDCLASSEXW {
                    cbSize        = SizeOf <WNDCLASSEXW>(),
                    style         = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS,
                    lpfnWndProc   = &ForwardWindowMessage,
                    cbClsExtra    = 0,
                    cbWndExtra    = 0,
                    hInstance     = EntryPointModule,
                    hIcon         = HICON.NULL,
                    hCursor       = GetDesktopCursor(),
                    hbrBackground = (HBRUSH)(COLOR_WINDOW + 1),
                    lpszMenuName  = null,
                    lpszClassName = (ushort *)lpszClassName,
                    hIconSm       = HICON.NULL
                };

                ThrowForLastErrorIfZero(classAtom = RegisterClassExW(&wndClassEx));
            }

            return(classAtom);
        }
Esempio n. 3
0
        protected unsafe ushort RegisterClass(string className)
        {
            this.wndProc = this.WndProcWrapper;

            fixed(char *cls = className)
            {
                var lpWndClass = new WNDCLASSEXW
                {
                    cbSize        = (uint)Marshal.SizeOf(typeof(WNDCLASSEXW)),
                    hInstance     = PInvoke.GetModuleHandle((PCWSTR)null),
                    lpfnWndProc   = this.wndProc,
                    lpszClassName = cls,
                };

                var atom = PInvoke.RegisterClassEx(lpWndClass);

                return(atom);
            }
        }
Esempio n. 4
0
        private ushort CreateClassAtom()
        {
            AssertNotDisposedOrDisposing(_state);

            ushort classAtom;

            {
                // lpszClassName should be less than 256 characters (this includes the null terminator)
                // Currently, we are well below this limit and should be hitting 74 characters + the null terminator

                var className = $"{GetType().FullName}.{EntryPointModule:X16}.{GetHashCode():X8}";
                Assert(AssertionsEnabled && (className.Length < byte.MaxValue));

                fixed(char *lpszClassName = className)
                {
                    var wndClassEx = new WNDCLASSEXW {
                        cbSize        = SizeOf <WNDCLASSEXW>(),
                        style         = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS,
                        lpfnWndProc   = &ForwardWindowMessage,
                        cbClsExtra    = 0,
                        cbWndExtra    = 0,
                        hInstance     = EntryPointModule,
                        hIcon         = HICON.NULL,
                        hCursor       = GetDesktopCursor(),
                        hbrBackground = (IntPtr)(COLOR_WINDOW + 1),
                        lpszMenuName  = null,
                        lpszClassName = (ushort *)lpszClassName,
                        hIconSm       = HICON.NULL
                    };

                    classAtom = RegisterClassExW(&wndClassEx);
                }

                ThrowForLastErrorIfZero(classAtom, nameof(RegisterClassExW));
            }
            return(classAtom);
        }
Esempio n. 5
0
    public static int Run(DXSample sample, HINSTANCE hInstance, int nCmdShow)
    {
        var useWarpDevice = false;

        // Parse the command line parameters
        foreach (var arg in Environment.GetCommandLineArgs())
        {
            if (Program.Matches(arg, "warp"))
            {
                useWarpDevice = true;
                continue;
            }
        }

        fixed(char *lpszClassName = "DXSampleClass")
        fixed(char *lpWindowName = sample.Name)
        {
            // Initialize the window class.
            var windowClass = new WNDCLASSEXW {
                cbSize        = (uint)sizeof(WNDCLASSEXW),
                style         = CS_HREDRAW | CS_VREDRAW,
                lpfnWndProc   = &WindowProc,
                hInstance     = hInstance,
                hCursor       = LoadCursorW(HINSTANCE.NULL, (ushort *)IDC_ARROW),
                lpszClassName = (ushort *)lpszClassName
            };

            _ = RegisterClassExW(&windowClass);

            // Create the window and store a handle to it.
            s_hwnd = CreateWindowExW(
                0,
                windowClass.lpszClassName,
                (ushort *)lpWindowName,
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                HWND.NULL,                       // We have no parent window.
                HMENU.NULL,                      // We aren't using menus.
                hInstance,
                ((IntPtr)GCHandle.Alloc(sample)).ToPointer()
                );
        }

        RECT windowRect;

        _ = GetClientRect(s_hwnd, &windowRect);

        // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
        var backgroundColor = new Vector4(0.0f, 0.2f, 0.4f, 1.0f);

        var size = new Size(windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);

        sample.OnInit(DXGI_FORMAT_UNKNOWN, backgroundColor, DXGI_FORMAT_UNKNOWN, 1.0f, 2, s_hwnd, true, size, useWarpDevice);

        _ = ShowWindow(s_hwnd, nCmdShow);

        // Main sample loop.
        MSG msg;

        DispatchPending(&msg);

        var lastFramesPerSecond = 0u;

        while (msg.message != WM_QUIT)
        {
            var delta = sample.OnBeginFrame();
            sample.OnUpdate(delta);

            var framesPerSecond = sample.FramesPerSecond;

            if (framesPerSecond != lastFramesPerSecond)
            {
                fixed(char *lpWindowName = $"{sample.Name} ({framesPerSecond} fps)")
                {
                    _ = SetWindowTextW(s_hwnd, (ushort *)lpWindowName);
                }

                lastFramesPerSecond = framesPerSecond;
            }

            if (sample.IsWindowVisible)
            {
                sample.OnRender();
            }

            DispatchPending(&msg);
        }

        sample.OnDestroy();

        // Return this part of the WM_QUIT message to Windows.
        return((int)msg.wParam);
Esempio n. 6
0
        static void Main(string[] args)
        {
            var manager = new D3DManager();

            var windowProc = new WNDPROC((Win32API.HWND _hwnd, WM uMsg, Win32API.WPARAM wParam, Win32API.LPARAM lParam) =>
            {
                if (ImGui.ImGui_ImplWin32_WndProcHandler(_hwnd.Value, (uint)uMsg, wParam.Value, lParam.Value) != IntPtr.Zero)
                {
                    return(1);
                }

                switch (uMsg)
                {
                case WM.DESTROY:
                    manager.Dispose();
                    User32.PostQuitMessage(0);
                    return(0);

                case WM.RESIZE:
                    // manager.Resize(lParam.LowWord, lParam.HiWord);
                    s_width  = lParam.LowWord;
                    s_height = lParam.HiWord;
                    return(0);

                case WM.PAINT:
                    {
                        PAINTSTRUCT ps = default;
                        var hdc        = User32.BeginPaint(_hwnd, ref ps);
                        User32.EndPaint(_hwnd, ref ps);
                        return(0);
                    }

                case WM.MOUSEMOVE:
                    {
                        s_mouse.X = lParam.LowWord;
                        s_mouse.Y = lParam.HiWord;
                        return(0);
                    }

                case WM.LBUTTONDOWN:
                    {
                        // User32.SetCapture(_hwnd);
                        s_mouse.Buttons |= ButtonFlags.Left;
                        return(0);
                    }

                case WM.LBUTTONUP:
                    {
                        s_mouse.Buttons &= ~ButtonFlags.Left;
                        if (s_mouse.Buttons == 0)
                        {
                            // User32.ReleaseCapture();
                        }
                        return(0);
                    }

                case WM.MBUTTONDOWN:
                    {
                        // SetCapture(hWnd);
                        s_mouse.Buttons |= ButtonFlags.Middle;
                        return(0);
                    }

                case WM.MBUTTONUP:
                    {
                        s_mouse.Buttons &= ~ButtonFlags.Middle;
                        if (s_mouse.Buttons == 0)
                        {
                            // ReleaseCapture();
                        }
                        return(0);
                    }

                case WM.RBUTTONDOWN:
                    {
                        // SetCapture(hWnd);
                        s_mouse.Buttons |= ButtonFlags.Right;
                        return(0);
                    }

                case WM.RBUTTONUP:
                    {
                        s_mouse.Buttons &= ~ButtonFlags.Right;
                        if (s_mouse.Buttons == 0)
                        {
                            // ReleaseCapture();
                        }
                        return(0);
                    }

                case WM.MOUSEWHEEL:
                    {
                        s_clearWheel = false;
                        var d        = wParam.HiWord;
                        if (d > 0)
                        {
                            s_mouse.Wheel = 1;
                        }
                        else if (d < 0)
                        {
                            s_mouse.Wheel = -1;
                        }
                        else
                        {
                            s_mouse.Wheel = 0;
                        }
                        return(0);
                    }
                }

                return(User32.DefWindowProcW(_hwnd, uMsg, wParam, lParam));
            });
            var ptr = Marshal.GetFunctionPointerForDelegate(windowProc);

            var windowClass = new WNDCLASSEXW();

            windowClass.cbSize        = Marshal.SizeOf(typeof(WNDCLASSEXW));
            windowClass.style         = CS.HREDRAW | CS.VREDRAW;
            windowClass.lpfnWndProc   = ptr;
            windowClass.cbClsExtra    = 0;
            windowClass.cbWndExtra    = 0;
            windowClass.lpszClassName = WINDOW_CLASS;
            var register = User32.RegisterClassExW(ref windowClass);

            if (register == 0)
            {
                Console.WriteLine("fail to RegisterClassExW");
                return;
            }

            var hwnd = User32.CreateWindowExW(
                0,
                WINDOW_CLASS,
                "SharpImGui",
                WS.OVERLAPPEDWINDOW,
                32, 32,
                640, 480,
        public static int Run(DXSample pSample, HINSTANCE hInstance, int nCmdShow)
        {
            var useWarpDevice = false;

            // Parse the command line parameters
            foreach (var arg in Environment.GetCommandLineArgs())
            {
                if (Program.Matches(arg, "warp"))
                {
                    useWarpDevice = true;
                    continue;
                }
            }

            fixed(char *lpszClassName = "DXSampleClass")
            fixed(char *lpWindowName = pSample.Name)
            {
                // Initialize the window class.
                var windowClass = new WNDCLASSEXW {
                    cbSize        = (uint)sizeof(WNDCLASSEXW),
                    style         = CS_HREDRAW | CS_VREDRAW,
                    lpfnWndProc   = &WindowProc,
                    hInstance     = hInstance,
                    hCursor       = LoadCursorW(IntPtr.Zero, (ushort *)IDC_ARROW),
                    lpszClassName = (ushort *)lpszClassName
                };

                _ = RegisterClassExW(&windowClass);

                // Create the window and store a handle to it.
                s_hwnd = CreateWindowExW(
                    0,
                    windowClass.lpszClassName,
                    (ushort *)lpWindowName,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    HWND.NULL,                              // We have no parent window.
                    HMENU.NULL,                             // We aren't using menus.
                    hInstance,
                    ((IntPtr)GCHandle.Alloc(pSample)).ToPointer()
                    );
            }

            RECT windowRect;

            _ = GetClientRect(s_hwnd, &windowRect);

            // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
            var backgroundColor = new Vector4(0.0f, 0.2f, 0.4f, 1.0f);

            var size = new Size((windowRect.right - windowRect.left), (windowRect.bottom - windowRect.top));

            pSample.OnInit(DXGI_FORMAT_UNKNOWN, backgroundColor, DXGI_FORMAT_UNKNOWN, 1.0f, 2, s_hwnd, true, size, useWarpDevice);

            _ = ShowWindow(s_hwnd, nCmdShow);

            // Main sample loop.
            MSG msg;

            do
            {
                // Process any messages in the queue.
                if (PeekMessageW(&msg, IntPtr.Zero, 0, 0, PM_REMOVE) != 0)
                {
                    _ = TranslateMessage(&msg);
                    _ = DispatchMessageW(&msg);
                }
            }while (msg.message != WM_QUIT);

            pSample.OnDestroy();

            // Return this part of the WM_QUIT message to Windows.
            return((int)msg.wParam);
        }
Esempio n. 8
0
 public static extern short RegisterClassExW([In] ref WNDCLASSEXW lpwc);
Esempio n. 9
0
        public static int Run(DXSample pSample, HINSTANCE hInstance, int nCmdShow)
        {
            // Parse the command line parameters
            pSample.ParseCommandLineArgs(Environment.GetCommandLineArgs());

            fixed(char *lpszClassName = "DXSampleClass")
            fixed(char *lpWindowName = pSample.Title)
            {
                // Requires an explicit cast until C# handles UnmanagedCallersOnly
                var wndProc = (delegate * stdcall <IntPtr, uint, nuint, nint, nint>)(delegate * managed <IntPtr, uint, nuint, nint, nint>) & WindowProc;

                // Initialize the window class.
                var windowClass = new WNDCLASSEXW {
                    cbSize        = (uint)sizeof(WNDCLASSEXW),
                    style         = CS_HREDRAW | CS_VREDRAW,
                    lpfnWndProc   = wndProc,
                    hInstance     = hInstance,
                    hCursor       = LoadCursorW(IntPtr.Zero, (ushort *)IDC_ARROW),
                    lpszClassName = (ushort *)lpszClassName
                };

                _ = RegisterClassExW(&windowClass);

                var windowRect = new RECT {
                    right  = unchecked ((int)pSample.Width),
                    bottom = unchecked ((int)pSample.Height)
                };

                _ = AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);

                // Create the window and store a handle to it.
                s_hwnd = CreateWindowExW(
                    0,
                    windowClass.lpszClassName,
                    (ushort *)lpWindowName,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    windowRect.right - windowRect.left,
                    windowRect.bottom - windowRect.top,
                    HWND.NULL,                              // We have no parent window.
                    HMENU.NULL,                             // We aren't using menus.
                    hInstance,
                    ((IntPtr)GCHandle.Alloc(pSample)).ToPointer()
                    );
            }

            // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
            pSample.OnInit();

            _ = ShowWindow(s_hwnd, nCmdShow);

            // Main sample loop.
            MSG msg;

            do
            {
                // Process any messages in the queue.
                if (PeekMessageW(&msg, IntPtr.Zero, 0, 0, PM_REMOVE) != 0)
                {
                    _ = TranslateMessage(&msg);
                    _ = DispatchMessageW(&msg);
                }
            }while (msg.message != WM_QUIT);

            pSample.OnDestroy();

            // Return this part of the WM_QUIT message to Windows.
            return((int)msg.wParam);
        }
Esempio n. 10
0
        public static int Run(Application application, HINSTANCE hInstance, int nCmdShow)
        {
            uint height;
            uint width;

            fixed(char *lpszClassName = "DXSampleClass")
            fixed(char *lpWindowName = application.Title)
            {
                // Initialize the window class.
                var windowClass = new WNDCLASSEXW
                {
                    cbSize        = (uint)sizeof(WNDCLASSEXW),
                    style         = CS_HREDRAW | CS_VREDRAW,
                    lpfnWndProc   = WindowProcHandle,
                    hInstance     = hInstance,
                    hCursor       = LoadCursorW(IntPtr.Zero, (ushort *)IDC_ARROW),
                    lpszClassName = (ushort *)lpszClassName
                };

                _ = RegisterClassExW(&windowClass);

                var windowRect = new Rectangle(0, 0, Width, Height);

                _ = AdjustWindowRect((RECT *)&windowRect, WS_OVERLAPPEDWINDOW, FALSE);

                height = (uint)(windowRect.Bottom - windowRect.Top);
                width  = (uint)(windowRect.Right - windowRect.Left);
                // Create the window and store a handle to it.
                Hwnd = CreateWindowExW(
                    0,
                    windowClass.lpszClassName,
                    (ushort *)lpWindowName,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    (int)width,
                    (int)height,
                    HWND.NULL,                              // We have no parent window.
                    HMENU.NULL,                             // We aren't using menus.
                    hInstance,
                    ((IntPtr)GCHandle.Alloc(application)).ToPointer()
                    );
            }

            // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
            application.Init(new ScreenData(height, width, Hwnd));

            _ = ShowWindow(Hwnd, nCmdShow);

            // Main sample loop.
            MSG msg;

            do
            {
                // Process any messages in the queue.
                if (PeekMessageW(&msg, IntPtr.Zero, 0, 0, PM_REMOVE) != 0)
                {
                    _ = TranslateMessage(&msg);
                    _ = DispatchMessageW(&msg);
                }
            }while (msg.message != WM_QUIT);

            application.Destroy();

            // Return this part of the WM_QUIT message to Windows.
            return((int)msg.wParam);
        }
Esempio n. 11
0
 private static extern ushort RegisterClassExW(
     ref WNDCLASSEXW wndClass
     );