Example #1
0
        private IntPtr CreateMessageOnlyWindow()
        {
            wndClass               = new WNDCLASSEX();
            wndClass.cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX));
            callbackDelegate       = WndProc;
            wndClass.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
            wndClass.lpszClassName = "isclass";
            wndClass.cbWndExtra    = 0;
            wndClass.hIcon         = IntPtr.Zero;
            wndClass.hCursor       = IntPtr.Zero;
            wndClass.hIconSm       = IntPtr.Zero;
            wndClass.hbrBackground = IntPtr.Zero;
            wndClass.hInstance     = Process.GetCurrentProcess().Handle;
            wndClass.lpszMenuName  = null;
            ushort ret = RegisterClassEx(ref wndClass);

            if (ret == 0)
            {
                ISLogger.Write("Failed to create window class: win32 error " + Marshal.GetLastWin32Error());
                return(IntPtr.Zero);
            }

            IntPtr window = CreateWindowEx(0, wndClass.lpszClassName, "ismsg", 0, 0, 0, 0, 0, HWND_MESSAGE, IntPtr.Zero, Process.GetCurrentProcess().Handle, IntPtr.Zero);

            if (window == IntPtr.Zero)
            {
                ISLogger.Write("Failed to create message only window - " + Marshal.GetLastWin32Error());
                return(IntPtr.Zero);
            }

            return(window);
        }
Example #2
0
        protected Application()
        {
            _wndProc = ProcessWindowMessage;
            var wndClassEx = new WNDCLASSEX
            {
                Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                WindowProc            = _wndProc,
                InstanceHandle        = HInstance,
                CursorHandle          = LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
                BackgroundBrushHandle = IntPtr.Zero,
                IconHandle            = IntPtr.Zero,
                ClassName             = WndClassName,
            };

            var atom = RegisterClassEx(ref wndClassEx);

            if (atom == 0)
            {
                throw new InvalidOperationException(
                          $"Failed to register window class. Error: {Marshal.GetLastWin32Error()}"
                          );
            }

            // Create main window.
            MainWindow = new Window("Vortice", 800, 600);
        }
        private void PlatformConstruct()
        {
            _wndProc = ProcessWindowMessage;
            var wndClassEx = new WNDCLASSEX
            {
                Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                WindowProc            = _wndProc,
                InstanceHandle        = HInstance,
                CursorHandle          = User32.LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
                BackgroundBrushHandle = IntPtr.Zero,
                IconHandle            = IntPtr.Zero,
                ClassName             = WndClassName,
            };

            var atom = User32.RegisterClassEx(ref wndClassEx);

            if (atom == 0)
            {
                throw new InvalidOperationException(
                          $"Failed to register window class. Error: {Marshal.GetLastWin32Error()}"
                          );
            }

            // Create main window.
            MainWindow = new Window("Vortice Tutorial 15 - Primitives", 1280, 720);
        }
Example #4
0
        static GlowWindowHost()
        {
            //Setup
            IntPtr hInstance = Marshal.GetHINSTANCE(typeof(GlowWindowHost).Module);
            IntPtr wndProc   = Marshal.GetFunctionPointerForDelegate(glowWindowWndProc);

            GlowWindowClassAtom = new WindowClassAtom();

            //Create WndClassEx structure
            WNDCLASSEX wcex = new WNDCLASSEX()
            {
                cbSize        = Marshal.SizeOf <WNDCLASSEX>(),
                lpfnWndProc   = wndProc,
                hInstance     = hInstance,
                lpszClassName = GlowWindowClassName,
            };

            //Register window class
            ushort classAtom = User32.RegisterClassEx(ref wcex);

            if (classAtom == 0)
            {
                throw new InvalidOperationException($"Unable to create \"{GlowWindowClassName}\" class atom.");
            }
            GlowWindowClassAtom.ClassAtom = classAtom;
            GlowWindowClassAtom.Lock();
        }
Example #5
0
        public MessageWindow(CS classStyle, WS style, WS_EX exStyle, Rect location, string name, WndProc callback)
        {
            _wndProcCallback = callback;
            _className       = "MessageWindowClass+" + Guid.NewGuid().ToString();
            var wc = new WNDCLASSEX
            {
                cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX)),
                style         = classStyle,
                lpfnWndProc   = s_WndProc,
                hInstance     = NativeMethods.GetModuleHandle(null),
                hbrBackground = NativeMethods.GetStockObject(StockObject.NULL_BRUSH),
                lpszMenuName  = "",
                lpszClassName = _className,
            };

            NativeMethods.RegisterClassEx(ref wc);
            var gcHandle = default(GCHandle);

            try
            {
                gcHandle = GCHandle.Alloc(this);
                var pinnedThisPtr = (IntPtr)gcHandle;
                Handle = NativeMethods.CreateWindowEx(exStyle, _className, name, style, (int)location.X, (int)location.Y, (int)location.Width,
                                                      (int)location.Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pinnedThisPtr);
            }
            finally
            {
                gcHandle.Free();
            }
        }
Example #6
0
        static void InitGlobal()
        {
            if (classAtom == 0)
            {
                hInstance = UnsafeNativeMethods.GetModuleHandleW(null);

                var wc = new WNDCLASSEX();
                wc.cbSize = Marshal.SizeOf(wc);
                wc.style  = ClassStyles.CS_VREDRAW | ClassStyles.CS_HREDRAW;

                var procPtr = Marshal.GetFunctionPointerForDelegate(WndProc);
                wc.lpfnWndProc = procPtr;

                wc.cbClsExtra    = 0;
                wc.cbWndExtra    = 0;
                wc.hInstance     = hInstance;
                wc.hIcon         = IntPtr.Zero;
                wc.hCursor       = IntPtr.Zero;
                wc.hbrBackground = IntPtr.Zero;
                wc.lpszMenuName  = null;
                wc.lpszClassName = Guid.NewGuid().ToString("n");
                wc.hIconSm       = IntPtr.Zero;

                classAtom = UnsafeNativeMethods.RegisterClassExW(ref wc);
                if (classAtom == 0)
                {
                    throw new Win32Exception();
                }
            }
        }
Example #7
0
        private void RegFormClass()
        {
            IntPtr     hInstance = Marshal.GetHINSTANCE(this.GetType().Module); //(IntPtr)Kernel.GetModuleHandle(null);
            WNDCLASSEX vWndCls   = WNDCLASSEX.Build();

            if (!User.GetClassInfoEx(hInstance, "HCPopupForm", ref vWndCls))
            {
                vWndCls = WNDCLASSEX.Build();  //vWndCls.cbSize = 48;
                vWndCls.lpszClassName = "HCPopupForm";
                vWndCls.style         = User.CS_VREDRAW | User.CS_HREDRAW | User.CS_DBLCLKS | User.CS_DROPSHADOW;
                vWndCls.hInstance     = hInstance;
                vWndCls.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(WndProc);
                vWndCls.cbClsExtra    = 0;
                vWndCls.cbWndExtra    = 8;
                vWndCls.hIcon         = IntPtr.Zero;
                vWndCls.hIconSm       = IntPtr.Zero;
                vWndCls.hCursor       = IntPtr.Zero;
                vWndCls.hbrBackground = (IntPtr)GDI.GetStockObject(0);
                vWndCls.lpszMenuName  = null;

                if (User.RegisterClassEx(ref vWndCls) == 0)
                {
                    throw new Exception("异常:注册HCPopupForm错误");
                }
            }
        }
Example #8
0
        private void VerifyHwndDestruction()
        {
            // Run through each class atom and attempt to query info on the class
            // If that fails, we know it's already been cleaned up.
            // Note that we don't need to verify the windows are gone since a
            // class can't be unregistered until all its windows are destroyed.

            int        returnValue;
            WNDCLASSEX wndclass = new WNDCLASSEX();

            IntPtr hInstance = GetModuleHandle(null);

            if (hInstance == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            for (int i = 0; i < _classAtoms.Count; i++)
            {
                returnValue = GetClassInfoEx(hInstance, new IntPtr(_classAtoms[i]), wndclass);

                // A return value of 0 means that UnregisterClass failed
                DRT.Assert(returnValue == 0, "Error: Window class wasn't unregistered properly");
            }
        }
Example #9
0
        private void RegFormClass()
        {
            IntPtr     hInstance = (IntPtr)Kernel.GetModuleHandle(null);
            WNDCLASSEX vWndCls   = new WNDCLASSEX();

            if (!User.GetClassInfoEx(hInstance, "HCPopupForm", out vWndCls))
            {
                vWndCls.cbSize        = 48;
                vWndCls.lpszClassName = "HCPopupForm";
                vWndCls.style         = User.CS_VREDRAW | User.CS_HREDRAW | User.CS_DBLCLKS | User.CS_DROPSHADOW;
                vWndCls.hInstance     = hInstance;
                vWndCls.lpfnWndProc   = WndProc;
                vWndCls.cbClsExtra    = 0;
                vWndCls.cbWndExtra    = 8;
                vWndCls.hIcon         = IntPtr.Zero;
                vWndCls.hIconSm       = IntPtr.Zero;
                vWndCls.hCursor       = IntPtr.Zero;
                vWndCls.hbrBackground = (IntPtr)GDI.GetStockObject(0);
                vWndCls.lpszMenuName  = null;

                if (User.RegisterClassEx(ref vWndCls) == 0)
                {
                    throw new Exception("异常:注册HCPopupForm错误!");
                }
            }
        }
        public WindowManager(
            [Import] Lazy <IDispatchManager> dispatchManager
            )
        {
            _lpClassName     = (WCHAR *)(Marshal.StringToHGlobalUni($"TerraFX.Interop.Provider.Win32.UI.Window.{EntryModuleHandle}"));
            _lpWindowName    = (WCHAR *)(Marshal.StringToHGlobalUni($"TerraFX Win32 Window"));
            _dispatchManager = dispatchManager;

            var wndClassEx = new WNDCLASSEX()
            {
                cbSize        = unchecked ((uint)(Marshal.SizeOf <WNDCLASSEX>())),
                style         = CS.VREDRAW | CS.HREDRAW,
                lpfnWndProc   = WndProc,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = EntryModuleHandle,
                hIcon         = HICON.NULL,
                hCursor       = HCURSOR.NULL,
                hbrBackground = (IntPtr)(COLOR.WINDOW + 1),
                lpszMenuName  = LPWSTR.NULL,
                lpszClassName = _lpClassName,
                hIconSm       = HICON.NULL
            };

            var classAtom = RegisterClassEx(ref wndClassEx);

            if (classAtom == 0)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(RegisterClassEx));
            }

            _classAtom = classAtom;
        }
        public Boolean CreateWindow(String className)
        {
            // intentionally here, prevents garbage collection
            this.windowProc = this.OnWindowProc;

            this.hInstance = GetModuleHandle(null);

            var wndClassEx = new WNDCLASSEX();

            wndClassEx.cbSize      = Marshal.SizeOf(typeof(WNDCLASSEX));
            wndClassEx.lpfnWndProc = this.windowProc;
            wndClassEx.hInstance   = this.hInstance;

            wndClassEx.lpszClassName = className;

            var atom = RegisterClassEx(ref wndClassEx);

            if (0 == atom)
            {
                Tracer.Trace($"RegisterClassEx failed with error {Marshal.GetLastWin32Error()}");
                return(false);
            }

            this.classAtom = new IntPtr(atom);

            this.WindowHandle = CreateWindowEx(0, this.classAtom, IntPtr.Zero, 0, 0, 0, 0, 0, new IntPtr(HWND_MESSAGE), IntPtr.Zero, this.hInstance, IntPtr.Zero);
            if (IntPtr.Zero == this.WindowHandle)
            {
                Tracer.Trace($"CreateWindowEx failed with error {Marshal.GetLastWin32Error()}");
                return(false);
            }

            return(true);
        }
Example #12
0
        private unsafe void PlatformConstruct()
        {
            fixed(char *lpszClassName = WindowClassName)
            {
                var wndClassEx = new WNDCLASSEX
                {
                    Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                    Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                    WindowProc            = &ProcessWindowMessage,
                    InstanceHandle        = HInstance,
                    CursorHandle          = LoadCursorW(IntPtr.Zero, IDC_ARROW),
                    BackgroundBrushHandle = IntPtr.Zero,
                    IconHandle            = IntPtr.Zero,
                    ClassName             = (ushort *)lpszClassName
                };

                ushort atom = RegisterClassExW(&wndClassEx);

                if (atom == 0)
                {
                    throw new InvalidOperationException(
                              $"Failed to register window class. Error: {Marshal.GetLastWin32Error()}"
                              );
                }
            }

            if (!Headless)
            {
                // Create main window.
                MainWindow = new Window("Vortice", 800, 600);
            }
        }
Example #13
0
            public static WNDCLASSEX Build()
            {
                var nw = new WNDCLASSEX();

                nw.cbSize = (UInt32)Marshal.SizeOf(typeof(WNDCLASSEX));
                return(nw);
            }
    public MessageWindow(CS classStyle, WS style, WS_EX exStyle, Rect location, string name, WndProc callback)
    {
        this._wndProcCallback = callback;
        this._className       = "MessageWindowClass+" + Guid.NewGuid().ToString();
        WNDCLASSEX wndclassex = new WNDCLASSEX
        {
            cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX)),
            style         = classStyle,
            lpfnWndProc   = MessageWindow.s_WndProc,
            hInstance     = NativeMethods.GetModuleHandle(null),
            hbrBackground = NativeMethods.GetStockObject(StockObject.NULL_BRUSH),
            lpszMenuName  = "",
            lpszClassName = this._className
        };

        NativeMethods.RegisterClassEx(ref wndclassex);
        GCHandle value = default(GCHandle);

        try
        {
            value = GCHandle.Alloc(this);
            IntPtr lpParam = (IntPtr)value;
            this.Handle = NativeMethods.CreateWindowEx(exStyle, this._className, name, style, (int)location.X, (int)location.Y, (int)location.Width, (int)location.Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, lpParam);
        }
        finally
        {
            value.Free();
        }
    }
Example #15
0
            public static WNDCLASSEX Create()
            {
                var nw = new WNDCLASSEX();

                nw.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
                return(nw);
            }
        public void CreateWindow(String className = "VurdalakovMessageOnlyWindow")
        {
            // intentionally here, prevents garbage collection
            this.windowProc = this.OnWindowProc;

            this.hInstance = GetModuleHandle(null);

            WNDCLASSEX wndClassEx = new WNDCLASSEX();
            wndClassEx.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
            wndClassEx.lpfnWndProc = this.windowProc;
            wndClassEx.hInstance = this.hInstance;
            wndClassEx.lpszClassName = className;

            UInt16 atom = RegisterClassEx(ref wndClassEx);
            if (0 == atom)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "RegisterClassEx failed");
            }

            this.classAtom = new IntPtr(atom);

            this.WindowHandle = CreateWindowEx(0, this.classAtom, IntPtr.Zero, 0, 0, 0, 0, 0, new IntPtr(HWND_MESSAGE), IntPtr.Zero, this.hInstance, IntPtr.Zero);
            if (IntPtr.Zero == this.WindowHandle)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateWindowEx failed");
            }
        }
Example #17
0
 public WindowClass()
 {
     _wcex               = new WNDCLASSEX();
     _wcex.cbSize        = Marshal.SizeOf(_wcex);
     _wcex.hInstance     = Marshal.GetHINSTANCE(typeof(WindowClass <TWindow>).Module);
     _wcex.lpszClassName = typeof(WindowClass <TWindow>).FullName;
     _wcex.lpfnWndProc   = WndProc;
 }
Example #18
0
        /// <summary>Get the window class description for a window class name</summary>
        /// <param name="hinstance">A handle to the instance of the application that created the class. To retrieve information about classes
        /// defined by the system (such as buttons or list boxes),set this parameter to NULL.</param>
        /// <param name="class_name">The name of the window class to look up</param>
        /// <returns>Returns the window class data if the window class is registered</returns>
        public static WNDCLASSEX?GetClassInfo(IntPtr hinstance, string class_name, out int atom)
        {
            var wc = new WNDCLASSEX {
                cbSize = Marshal.SizeOf <WNDCLASSEX>()
            };

            atom = GetClassInfoEx_(hinstance, class_name, ref wc);
            return(atom != 0 ? wc : (WNDCLASSEX?)null);
        }
            public static WNDCLASSEX Create()
            {
                var result = new WNDCLASSEX
                {
                    cbSize = (UInt32)Marshal.SizeOf(typeof(WNDCLASSEX))
                };

                return(result);
            }
Example #20
0
    static void Main()
    {
        bool       exit = false;
        MSG        msg;
        WNDCLASSEX win = new WNDCLASSEX();

        win.cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX));
        win.style         = (int)(1 | 2);
        win.hbrBackground = (IntPtr)1 + 1;
        win.cbClsExtra    = 0;
        win.cbWndExtra    = 0;
        win.hInstance     = System.Diagnostics.Process.GetCurrentProcess().Handle;
        win.hIcon         = IntPtr.Zero;
        win.hCursor       = LoadCursor(IntPtr.Zero, (int)32515);
        win.lpszMenuName  = null;
        win.lpszClassName = "Demo";
        win.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(WindowProcPointer);
        win.hIconSm       = IntPtr.Zero;
        IntPtr hwnd = CreateWindowEx(0, RegisterClassEx(ref win), "Demo", 0xcf0000 | 0x10000000, 0, 0, 800, 480, IntPtr.Zero, IntPtr.Zero, win.hInstance, IntPtr.Zero);
        IntPtr hdc  = GetDC(hwnd);
        PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR();

        pfd.dwFlags = 0x00000001u;
        SetPixelFormat(hdc, ChoosePixelFormat(hdc, ref pfd), ref pfd);
        wglMakeCurrent(hdc, wglCreateContext(hdc));
        Console.WriteLine("Graphics Processing Unit: " + Marshal.PtrToStringAnsi(glGetString(0x1F01)));
        glInit();
        wglSwapIntervalEXT(0);
        uint p = glCreateProgram();
        uint s = glCreateShader(0x8B30);

        glShaderSource(s, 1, new[] { FragmentShader }, new[] { FragmentShader.Length });
        glCompileShader(s);
        glAttachShader(p, s);
        glLinkProgram(p);
        glUseProgram(p);
        int   time  = glGetUniformLocation(p, "iTime");
        float start = Environment.TickCount * 0.001f;

        while (!exit)
        {
            while (PeekMessage(out msg, 0, 0, 0, 0x0001))
            {
                if (msg.message == 0x0012)
                {
                    exit = true;
                }
                TranslateMessage(ref msg);
                DispatchMessage(ref msg);
            }
            glUniform1f(time, (Environment.TickCount * 0.001f) - start);
            glRects(-1, -1, 1, 1);
            wglSwapLayerBuffers(hdc, 0x00000001);
        }
        DestroyWindow(hwnd);
    }
Example #21
0
    public static short RegisterClassEx(ref WNDCLASSEX lpwcx)
    {
        short num = NativeMethods._RegisterClassEx(ref lpwcx);

        if (num == 0)
        {
            HRESULT.ThrowLastError();
        }
        return(num);
    }
Example #22
0
        public static void Main(string[] args)
        {
            IntPtr hInstance = GetModuleHandle(null);

            WNDCLASSEX wc = new WNDCLASSEX
            {
                cbSize        = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)),
                style         = 0,
                lpfnWndProc   = WindowProc,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = hInstance,
                hIcon         = LoadIcon(IntPtr.Zero, IDI_APPLICATION),
                hCursor       = LoadCursor(IntPtr.Zero, IDC_ARROW),
                hbrBackground = (IntPtr)(COLOR_WINDOW + 1),
                lpszMenuName  = null,
                lpszClassName = typeof(Program).FullName,
                hIconSm       = LoadIcon(IntPtr.Zero, IDI_APPLICATION)
            };

            var windowClass = RegisterClassEx(ref wc);

            if (windowClass == 0)
            {
                throw new InvalidOperationException($"RegisterClassEx failed.");
            }

            var hWnd = CreateWindowEx(
                WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
                typeof(Program).FullName,
                "Hello World!",
                WS_MINIMIZEBOX | WS_SYSMENU | WS_OVERLAPPED | WS_CAPTION,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                800,
                600,
                IntPtr.Zero,
                IntPtr.Zero,
                hInstance,
                IntPtr.Zero);

            if (hWnd == IntPtr.Zero)
            {
                throw new InvalidOperationException($"CreateWindowEx failed.");
            }

            ShowWindow(hWnd, SW_SHOWNORMAL);

            while (GetMessage(out var message, IntPtr.Zero, 0, 0))
            {
                TranslateMessage(ref message);
                DispatchMessage(ref message);
            }
        }
Example #23
0
        public override void Start()
        {
            //https://stackoverflow.com/questions/8980873/implementing-a-win32-message-loop-and-creating-a-window-object-with-p-invoke
            _messageWindowProc = WndProc;
            w = new WNDCLASSEX
            {
                cbSize        = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)),
                lpszClassName = "OTB_Message_Watcher_Class",
                lpfnWndProc   = _messageWindowProc
            };

            NativeMethods.RegisterClassEx(ref w);
            IntPtr hInstance = NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

            //new IntPtr(-3) = MESSAGE ONLY
            _messageWindowHandle = NativeMethods.CreateWindowEx(0, w.lpszClassName, w.lpszClassName, 0, 0, 0, 0, 0, new IntPtr(-3), IntPtr.Zero, hInstance /*Can I make this 0?*/, IntPtr.Zero);
            NativeMethods.AddClipboardFormatListener(_messageWindowHandle);


            _windowsMouseHookHandle = IntPtr.Zero;
            _user32LibraryHandle    = IntPtr.Zero;
            _mouseHookProc          = LowLevelMouseProc; // we must keep alive _hookProc, because GC is not aware about SetWindowsHookEx behaviour.

            _windowsKeyboardHookHandle = IntPtr.Zero;
            _keyboardHookProc          = LowLevelKeyboardProc; // we must keep alive _hookProc, because GC is not aware about SetWindowsHookEx behaviour.


            _user32LibraryHandle = NativeMethods.LoadLibrary("User32");
            if (_user32LibraryHandle == IntPtr.Zero)
            {
                int errorCode = Marshal.GetLastWin32Error();
                throw new Win32Exception(errorCode, $"Failed to load library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
            }

            _windowsMouseHookHandle = NativeMethods.SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProc, _user32LibraryHandle, 0);
            if (_windowsMouseHookHandle == IntPtr.Zero)
            {
                int errorCode = Marshal.GetLastWin32Error();
                throw new Win32Exception(errorCode, $"Failed to adjust mouse hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
            }

            _windowsKeyboardHookHandle = NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, _keyboardHookProc, _user32LibraryHandle, 0);
            if (_windowsKeyboardHookHandle == IntPtr.Zero)
            {
                int errorCode = Marshal.GetLastWin32Error();
                throw new Win32Exception(errorCode, $"Failed to adjust keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
            }

            //init initial cursor position
            if (NativeMethods.GetCursorPos(out Point p))
            {
                MouseState.Position = new MousePoint(p.X, p.Y);
            }
        }
Example #24
0
        void Run()
        {
            var moduleHandle = GetModuleHandle(null);

            var wndClass = new WNDCLASSEX
            {
                Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                WindowProc            = WndProc,
                InstanceHandle        = moduleHandle,
                CursorHandle          = LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
                BackgroundBrushHandle = IntPtr.Zero,
                IconHandle            = IntPtr.Zero,
                ClassName             = "WndClass",
            };

            RegisterClassEx(ref wndClass);

            var win32window = new Win32Window(wndClass.ClassName, "RTUGame", 800, 600);
            var mainWindow  = new AppWindow(win32window);

            windows.Add(mainWindow.Win32Window.Handle, mainWindow);

            mainWindow.Show();

            while (!quitRequested)
            {
                while (PeekMessage(out var msg, IntPtr.Zero, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(ref msg);
                    DispatchMessage(ref msg);

                    if (msg.Value == (uint)WindowMessage.Quit)
                    {
                        quitRequested = true;
                        goto lable_stop;
                    }
                }

                foreach (var window in windows.Values)
                {
                    window.UpdateAndDraw();
                }
            }
lable_stop:

            foreach (var window in windows.Values)
            {
                window.Dispose();
            }
            return;
        }
Example #25
0
        public static ushort RegisterClass(WNDCLASSEX wnd_class)
        {
            // RegisterClass only sets last error if there is an error
            Win32.SetLastError(Win32.ERROR_SUCCESS);
            var atom = RegisterClassEx_(ref wnd_class);
            var err  = Marshal.GetLastWin32Error();

            if (err != Win32.ERROR_SUCCESS)
            {
                throw new Win32Exception();
            }
            return(atom);
        }
        /// <summary>
        /// Create a messageLoop-only Window (invsible) to treat WM_* messages
        /// </summary>
        /// <returns></returns>
        public bool CreateRawMessageWindow()
        {
            delegWndProc = myWndProc;

            WNDCLASSEX wind_class = new WNDCLASSEX();

            wind_class.cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX));
            wind_class.style         = 0;
            wind_class.hbrBackground = IntPtr.Zero;
            wind_class.cbClsExtra    = 0;
            wind_class.cbWndExtra    = 0;
            wind_class.hInstance     = Marshal.GetHINSTANCE(this.GetType().Module); // alternative: Process.GetCurrentProcess().Handle;
            wind_class.hIcon         = IntPtr.Zero;
            wind_class.hCursor       = IntPtr.Zero;
            wind_class.lpszMenuName  = null;
            wind_class.lpszClassName = "RI_MsgLoop";
            wind_class.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(delegWndProc);
            wind_class.hIconSm       = IntPtr.Zero;
            ushort regResult = Win32API.RegisterClassEx(ref wind_class);

            if (regResult == 0)
            {
                uint error = Win32API.GetLastError();
                return(false);
            }
            string wndClass = wind_class.lpszClassName;

            //This version worked and resulted in a non-zero hWnd
            //_hWnd = CreateWindowEx(0, regResult, "Hello Win32", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 400, IntPtr.Zero, IntPtr.Zero, wind_class.hInstance, IntPtr.Zero);

            IntPtr HWND_MESSAGE = new IntPtr(-3);

            _RawMessageWnd_hWnd = Win32API.CreateWindowEx(0, regResult, "DemulShooter_RawInputWnd", 0, 0, 0, 0, 0, HWND_MESSAGE, IntPtr.Zero, wind_class.hInstance, IntPtr.Zero);


            if (_RawMessageWnd_hWnd == ((IntPtr)0))
            {
                uint error = Win32API.GetLastError();
                return(false);
            }
            return(true);

            //The explicit message pump is not necessary, messages are obviously dispatched by the framework.
            //However, if the while loop is implemented, the functions are called... Windows mysteries...
            //MSG msg;
            //while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
            //{
            //    TranslateMessage(ref msg);
            //    DispatchMessage(ref msg);
            //}
        }
Example #27
0
        //Window create
        public AVCustomWindow(string windowTitle, int windowWidth, int windowHeight, bool windowVisible)
        {
            try
            {
                //Set window variables
                this.windowWidth  = windowWidth;
                this.windowHeight = windowHeight;

                //Create window procedure
                windowProc        = WindowProcessMessage;
                windowProcPointer = Marshal.GetFunctionPointerForDelegate(windowProc);

                //Create window class
                WNDCLASSEX windowClassEx = new WNDCLASSEX
                {
                    cbSize        = WNDCLASSEX.classSize,
                    style         = 0,
                    lpfnWndProc   = windowProcPointer,
                    cbClsExtra    = 0,
                    cbWndExtra    = 0,
                    hInstance     = IntPtr.Zero,
                    hIcon         = IntPtr.Zero,
                    hCursor       = IntPtr.Zero,
                    hbrBackground = IntPtr.Zero,
                    lpszMenuName  = windowTitle,
                    lpszClassName = Environment.TickCount.ToString(),
                    hIconSm       = IntPtr.Zero
                };

                //Register window class
                RegisterClassEx(ref windowClassEx);

                //Create window
                WindowStyles   windowStyles   = WindowStyles.WS_OVERLAPPEDWINDOW;
                WindowStylesEx windowStylesEx = WindowStylesEx.WS_EX_LEFT;
                windowParent = CreateWindowEx(windowStylesEx, windowClassEx.lpszClassName, windowClassEx.lpszMenuName, windowStyles, 0, 0, this.windowHeight, this.windowWidth, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

                //Show window
                if (windowVisible)
                {
                    ShowWindow(windowParent, WindowShowCommand.Show);
                }

                Debug.WriteLine("Created custom window: " + windowParent);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to create custom window: " + ex.Message);
            }
        }
Example #28
0
        private static IntPtr GetHandleWindow(VideoMode videoMode)
        {
            var cn = "test" + new Random().Next(0, 1000); // tmp

            var hInstance = new IntPtr(Kernel.GetModuleHandle(null));

            WNDCLASSEX wc = new WNDCLASSEX()
            {
                cbSize        = Marshal.SizeOf(typeof(WNDCLASSEX)),
                style         = 0,
                lpfnWndProc   = (int)Marshal.GetFunctionPointerForDelegate(myWndProcDelegate),
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = hInstance,
                hIcon         = IntPtr.Zero,
                hCursor       = IntPtr.Zero,
                hbrBackground = new IntPtr(COLOR_BACKGROUND),
                lpszMenuName  = null,
                lpszClassName = cn,
                hIconSm       = IntPtr.Zero
            };

            if (RegisterClassEx(ref wc) == 0)
            {
                MessageBox(IntPtr.Zero, "Window Registration Failed!", "Error!",
                           MB_ICONEXCLAMATION | MB_OK);
                throw new Exception();
            }

            IntPtr hWnd = (IntPtr)CreateWindowEx(
                WS_EX_TOOLWINDOW, // Remove window from Alt Tab
                cn,
                null,
                WS_BORDER & ~WS_OVERLAPPEDWINDOW, // PWTD Make rather a fake fullscreen windowed (without border)s // DOLATER Remove borders without disturb good mecanism
                0, 0,
                (int)videoMode.Width, (int)videoMode.Height,
                IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero);


            if (hWnd == IntPtr.Zero)
            {
                MessageBox(IntPtr.Zero, "Window Creation Failed!", "Error!",
                           MB_ICONEXCLAMATION | MB_OK);
                throw new Exception();
            }

            return(hWnd);
        }
        private static ushort RegisterClass()
        {
            var wndclass = WNDCLASSEX.Create();

            wndclass.lpszClassName = WindowClassName;
            wndclass.lpfnWndProc   = StaticWndProc;

            var result = RegisterClassEx(ref wndclass);

            if (result == 0)
            {
                throw new Win32Exception("RegisterClass error.");
            }

            return(result);
        }
Example #30
0
        void Run()
        {
            D3D11.D3D11CreateDevice(null, DriverType.Hardware, DeviceCreationFlags.None, null, out device, out deviceContext);

            var moduleHandle = GetModuleHandle(null);

            var wndClass = new WNDCLASSEX
            {
                Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                WindowProc            = WndProc,
                InstanceHandle        = moduleHandle,
                CursorHandle          = LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
                BackgroundBrushHandle = IntPtr.Zero,
                IconHandle            = IntPtr.Zero,
                ClassName             = "WndClass",
            };

            RegisterClassEx(ref wndClass);

            var win32window = new Win32Window(wndClass.ClassName, "Vortice ImGui", 800, 600);
            var mainWindow  = new MainWindow(win32window, device, deviceContext);

            windows.Add(mainWindow.Win32Window.Handle, mainWindow);

            mainWindow.Show();

            while (!quitRequested)
            {
                if (PeekMessage(out var msg, IntPtr.Zero, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(ref msg);
                    DispatchMessage(ref msg);

                    if (msg.Value == (uint)WindowMessage.Quit)
                    {
                        quitRequested = true;
                        break;
                    }
                }

                foreach (var window in windows.Values)
                {
                    window.UpdateAndDraw();
                }
            }
        }
Example #31
0
        private static bool InitApplication(IntPtr hinstance)
        {
            WNDCLASSEX wcx = new WNDCLASSEX();

            wcx.cbSize = Marshal.SizeOf(wcx);
            wcx.style  = (int)(ClassStyles.VerticalRedraw | ClassStyles.HorizontalRedraw);

            unsafe
            {
                //IntPtr address = MainWndProc; -- this is not necessary to put inside a Unsafe context
                IntPtr address2 = Marshal.GetFunctionPointerForDelegate((Delegate)(WndProc)MainWndProc);
                wcx.lpfnWndProc = address2;
            }

            wcx.cbClsExtra = 0;
            wcx.cbWndExtra = 0;
            wcx.hInstance  = hinstance;
            wcx.hIcon      = WinAPI.LoadIcon(
                IntPtr.Zero, new IntPtr(10));
            //wndClass.hCursor = WinAPI.LoadCursor(IntPtr.Zero, (int)IdcStandardCursor.IDC_ARROW);
            wcx.hCursor       = WinAPI.LoadCursor(IntPtr.Zero, (int)Win32_IDC_Constants.IDC_ARROW);
            wcx.hbrBackground = WinAPI.GetStockObject(StockObjects.WHITE_BRUSH);
            wcx.lpszMenuName  = "MainMenu";
            wcx.lpszClassName = "MainWClass";
            //     wcx.hIconSm = LoadImage(hinstance, // small class icon
            //MAKEINTRESOURCE(5),
            //IMAGE_ICON,
            //GetSystemMetrics(SM_CXSMICON),
            //GetSystemMetrics(SM_CYSMICON),
            //LR_DEFAULTCOLOR);

            // it might be as this:
            //   problems with p/invoke CreateWindowEx() and RegisterClassEx()
            //  http://social.msdn.microsoft.com/Forums/vstudio/en-US/8580a805-383b-4b17-8bd8-514da4a5f3a4/problems-with-pinvoke-createwindowex-and-registerclassex
            // ATOM?
            UInt16 ret = WinAPI.RegisterClassEx2(ref wcx);

            if (ret != 0)
            {
                string message = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                Console.WriteLine("Failed to call RegisterClasEx, error = {0}", message);
            }
            //return WinAPI.RegisterClassEx(ref wcx) != 0;
            atom = ret;
            return(ret != 0);
        }
Example #32
0
        public MessageWindow(CS classStyle, WS style, WS_EX exStyle, Rect location, string name, WndProc callback)
        {
            // A null callback means just use DefWindowProc.
            _wndProcCallback = callback;
            _className = "MessageWindowClass+" + Guid.NewGuid().ToString();

            var wc = new WNDCLASSEX
            {
                cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)),
                style = classStyle,
                lpfnWndProc = s_WndProc,
                hInstance = NativeMethods.GetModuleHandle(null),
                hbrBackground = NativeMethods.GetStockObject(StockObject.NULL_BRUSH),
                lpszMenuName = "",
                lpszClassName = _className,
            };

            NativeMethods.RegisterClassEx(ref wc);

            GCHandle gcHandle = default(GCHandle);
            try
            {
                gcHandle = GCHandle.Alloc(this);
                IntPtr pinnedThisPtr = (IntPtr)gcHandle;

                Handle = NativeMethods.CreateWindowEx(
                    exStyle,
                    _className,
                    name,
                    style,
                    (int)location.X,
                    (int)location.Y,
                    (int)location.Width,
                    (int)location.Height,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    pinnedThisPtr);
            }
            finally
            {
                gcHandle.Free();
            }
        }
        static void Main()
        {
            Module[] ms = Assembly.GetEntryAssembly().GetModules();
            IntPtr hInst = Marshal.GetHINSTANCE(ms[0]);

            WNDCLASSEX wc = new WNDCLASSEX();
            wc.cbSize = (uint)Marshal.SizeOf(wc);
            wc.style = 0;
            wc.lpfnWndProc = WindowProcedure;
            wc.cbClsExtra = 0;
            wc.cbWndExtra = 0;
            wc.hInstance = hInst;
            wc.hIcon = (IntPtr)null;
            wc.hCursor = (IntPtr)null;
            wc.hbrBackground = (IntPtr)(5 + 1);	// COLOR_WINDOW=5
            wc.lpszMenuName = null;
            wc.lpszClassName = "TestClass";
            wc.hIcon = (IntPtr)null;

            RegisterClassEx(ref wc);

            IntPtr hWnd = CreateWindowEx(
                0x00040000,				// WS_EX_APPWINDOW
                "TestClass",
                "WindowsProgram",
                (uint)(0x00000000 | 0x00C00000 | 0x00080000 | 0x00040000 | 0x00020000L | 0x00010000L) ,// WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
                unchecked((int)0x80000000), unchecked((int)0x80000000), // CW_USEDEFAULT
                unchecked((int)0x80000000), unchecked((int)0x80000000), // CW_USEDEFAULT
                (IntPtr)null, (IntPtr)null, hInst, (IntPtr)null
            );

            ShowWindow(hWnd, 5);	// SW_SHOW

            MSG msg;
            while (GetMessage(out msg, (IntPtr)null, 0, 0) != 0)
            {
                Console.Write("(" +  msg.message.ToString("X") + ",");
                DispatchMessage(ref msg);
                TranslateMessage(ref msg);
            }
        }
 public static extern UInt16 RegisterClassEx(ref WNDCLASSEX lpwcx);
Example #35
0
        public void RegisterClass(ClassCreateParams ccp)
        {
            WNDCLASSEX wc = new WNDCLASSEX()
            {
                cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)),
                lpszClassName = ccp.Name,
                lpfnWndProc = ccp.WndProc,
                style = (int)ccp.Style,
                cbClsExtra = ccp.ExtraClassBytes,
                cbWndExtra = ccp.ExtraWindowBytes,
                hbrBackground = ccp.BackgroundBrush,
                hInstance = Marshal.GetHINSTANCE(this.GetType().Module)
            };

            if (RegisterClassEx(ref wc) != 0)
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Example #36
0
        // 里々からのメッセージ受信処理
        private void GetMsgFromSATORI()
        {
            try
            {
                // ウィンドウクラス登録
                WNDCLASSEX wcex = new WNDCLASSEX();
                wcex.cbSize = Marshal.SizeOf(wcex);
                wcex.hInstance =
                    (int)Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
                wcex.lpszClassName = ClassName;
                wcex.lpfnWndProc = MsgFromSATORI_WndProc;
                wcex.style = 0;
                wcex.hIcon = 0;
                wcex.hIconSm = 0;
                wcex.hCursor = 0;
                wcex.lpszMenuName = 0;
                wcex.cbClsExtra = 0;
                wcex.cbWndExtra = 0;
                wcex.hbrBackground = 0;
                int a = RegisterClassEx(ref wcex);

                // ウィンドウ作成
                MsgFromSATORI_hWnd =
                    CreateWindowEx(0, wcex.lpszClassName, wcex.lpszClassName,
                    0, 0, 0, 0, 0, 0, 0, wcex.hInstance, 0);

                if (MsgFromSATORI_hWnd != 0)
                {
                    // メッセージ取得
                    int rtn;
                    Message mmm = new Message();
                    while (true)
                    {
                        rtn = GetMessage(ref mmm, MsgFromSATORI_hWnd, 0, 0);
                        if (rtn <= 0) break;
                        // ウィンドウプロシージャへのメッセージ送信
                        try { TranslateMessage(ref mmm); }
                        catch (Exception) { }
                        try { DispatchMessage(ref mmm); }
                        catch (Exception) { }
                    }
                }
            }
            catch (Exception) { }
            finally
            {
                // ウィンドウ破棄
                try { DestroyWindow(MsgFromSATORI_hWnd); }
                catch (Exception) { }
                // ウィンドウクラスのハンドル初期化
                MsgFromSATORI_hWnd = 0;
            }
        }
Example #37
0
 private static extern int RegisterClassEx(ref WNDCLASSEX wcex);
Example #38
0
 public static extern short RegisterClassEx(ref WNDCLASSEX lpwcx);
Example #39
0
 public static extern ushort RegisterClassEx(WNDCLASSEX wc_d);
		public static int RegisterClassEx(WNDCLASSEX lpwcx) { return default(int); }
Example #41
0
 public static extern bool GetClassInfoEx(
     IntPtr hInstance,
     string lpClassName,
     ref WNDCLASSEX lpWndClass);
Example #42
0
 public static extern ATOM RegisterClassEx(WNDCLASSEX wc);
Example #43
0
 static extern bool GetClassInfoEx(IntPtr hinst, string lpszClass, out WNDCLASSEX lpwcx);
Example #44
0
File: WinAPI.cs Project: hiiru/lab
 public static extern int RegisterClassEx(ref WNDCLASSEX pcWndClassEx);
Example #45
0
        /// <summary>
        /// Starts the game.
        /// </summary>
        /// <param name="title">The title text.</param>
        public void Start(string title)
        {
            var instance = GetModuleHandle(null);

            var windowClass = new WNDCLASSEX
            {
                Size = (uint)Marshal.SizeOf<WNDCLASSEX>(),
                Style = CS.OWNDC,
                WndProc = this.WndProc,
                Instance = instance,
                Icon = LoadIcon(instance, IDI_APPLICATION),
                Cursor = Cursor.Arrow.Handle,
                ClassName = this.GetType().Name,
            };

            if (windowClass.Icon == NULL)
            {
                windowClass.Icon = LoadIcon(NULL, IDI_APPLICATION);
            }

            try
            {
                RegisterClassEx(ref windowClass);

                this.window = CreateWindowEx(
                    0,
                    windowClass.ClassName,
                    title,
                    WS.CanResize,
                    int.MinValue, int.MinValue,
                    int.MinValue, int.MinValue,
                    NULL,
                    NULL,
                    windowClass.Instance,
                    NULL);

                var monitor = new MONITORINFO { Size = Marshal.SizeOf<MONITORINFO>() };
                GetMonitorInfo(MonitorFromWindow(this.window, MONITOR.DEFAULTTOPRIMARY), ref monitor);

                var initialW = (monitor.Work.R - monitor.Work.L) * 3 / 4;
                var initialH = (monitor.Work.B - monitor.Work.T) * 3 / 4;
                var initialX = monitor.Work.L + ((monitor.Work.R - monitor.Work.L - initialW) / 2);
                var initialY = monitor.Work.T + ((monitor.Work.B - monitor.Work.T - initialH) / 2);

                MoveWindow(this.window, initialX, initialY, initialW, initialH, false);

                var timer = Stopwatch.StartNew();

                while (true)
                {
                    var active = IsActive;

                    if (active)
                    {
                        BeginFrame(this.clientW, this.clientH);

                        this.OnRender();

                        InvokePendingActions();
                    }

                    MSG msg;

                    while (PeekMessage(out msg, NULL, 0, 0, PM.REMOVE))
                    {
                        switch (msg.Message)
                        {
                            case WM.QUIT:
                                return;

                            default:
                                TranslateMessage(ref msg);
                                DispatchMessage(ref msg);
                                break;
                        }
                    }

                    if (active)
                    {
                        if (FrameCount == 0)
                        {
                            this.OnUpdate();
                            UpdateCount++;
                        }
                        else
                        {
                            UpdateTweening += FrameInterval;

                            while (UpdateTweening > UpdateInterval)
                            {
                                UpdateTime += UpdateInterval;
                                UpdateTweening -= UpdateInterval;

                                this.OnUpdate();
                                UpdateCount++;
                            }
                        }

                        gl.Finish(); // <- Reduces stuttering by keeping the frame rate consistent.
                        gl.Present();

                        if (Game.Settings.UseFrameRateLimit)
                        {
                            var elapsed = timer.Elapsed;

                            if (elapsed < minimumFrameInterval)
                            {
                                Thread.Sleep(minimumFrameInterval - elapsed);
                            }
                        }

                        EndFrame(timer.Elapsed);
                        timer.Restart();
                    }

                    if (IsActive)
                    {
                        if (DisplayMode.Set(Game.Settings.DisplayMode))
                        {
                            if (DisplayMode.Current != null)
                            {
                                SetWindowLongPtr(this.window, GWLP.STYLE, unchecked(new IntPtr((int)WS.NoBorder)));
                                SetWindowPos(this.window, NULL, DisplayMode.Current.X, DisplayMode.Current.Y, 0, 0, SWP.NOSIZE | SWP.NOZORDER | SWP.FRAMECHANGED);
                                ShowWindow(this.window, SW.MAXIMIZE);
                            }
                            else
                            {
                                ShowWindow(this.window, SW.RESTORE);
                                SetWindowLongPtr(this.window, GWLP.STYLE, unchecked(new IntPtr((int)WS.CanResize)));
                                SetWindowPos(this.window, NULL, initialX, initialY, initialW, initialH, SWP.NOZORDER | SWP.FRAMECHANGED);
                            }
                        }

                        if (Game.Settings.UseVerticalSync)
                        {
                            if (this.swapInterval != 1)
                            {
                                this.swapInterval = 1;
                                gl.SwapInterval(this.swapInterval);
                            }
                        }
                        else
                        {
                            if (this.swapInterval != 0)
                            {
                                this.swapInterval = 0;
                                gl.SwapInterval(this.swapInterval);
                            }
                        }

                        this.UpdateCursor();
                    }
                    else
                    {
                        if (DisplayMode.Set(null))
                        {
                            ShowWindow(this.window, SW.MINIMIZE);
                        }

                        this.UpdateCursor();
                        WaitMessage();
                    }
                }
            }
            finally
            {
                Cursor.Free();
                DisplayMode.Set(null);
                UnregisterClass(windowClass.ClassName, windowClass.Instance);
            }
        }
Example #46
0
        public EVRPlayListener(MainForm parent)
        {
            m_parent = parent;
            m_KeepMe = new WndProc(CustomWndProc);
            IntPtr hInstance = Marshal.GetHINSTANCE(this.GetType().Module);

            WNDCLASSEX wndClassEx = new WNDCLASSEX();
            wndClassEx.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
            wndClassEx.style = ClassStyle.GLOBALCLASS;
            wndClassEx.cbClsExtra = 0;
            wndClassEx.cbWndExtra = 0;
            wndClassEx.hbrBackground = IntPtr.Zero;
            wndClassEx.hCursor = IntPtr.Zero;
            wndClassEx.hIcon = IntPtr.Zero;
            wndClassEx.hIconSm = IntPtr.Zero;
            wndClassEx.lpszClassName = EVR_CLASS;
            wndClassEx.lpszMenuName = null;
            wndClassEx.hInstance = hInstance;
            wndClassEx.lpfnWndProc = m_KeepMe;

            ushort atom = RegisterClassEx(ref wndClassEx);

            //WNDCLASSEX lpwcx;
            //bool isReg = GetClassInfoEx(hInstance, EVR_CLASS, out lpwcx);

            if (atom == 0)
            {
                int error = Marshal.GetLastWin32Error();
                throw new Win32Exception(error);
            }            

            // Create window
            //m_hwnd = CreateWindowEx(
            //    WindowStyle.EX_NOACTIVATE,
            //    atom,
            //    EVR_WIN,
            //    0,
            //    0,
            //    0,
            //    0,
            //    0,
            //    IntPtr.Zero,
            //    IntPtr.Zero,
            //    Marshal.GetHINSTANCE(parent.GetType().Module),
            //    IntPtr.Zero
            //);

            m_hwnd = CreateWindowEx(
                WindowStyle.EX_NOACTIVATE,
                EVR_CLASS,
                EVR_WIN,
                0,
                0,
                0,
                0,
                0,
                IntPtr.Zero,
                IntPtr.Zero,
                hInstance,
                IntPtr.Zero
            );            
                
            if (m_hwnd == IntPtr.Zero)
            {
                int error = Marshal.GetLastWin32Error();
                throw new Win32Exception(error);
            }
        }
Example #47
0
 //Use this function to make a new one with cbSize already filled in.
 //For example:
 //var WndClss = WNDCLASSEX.Build()
 public static WNDCLASSEX Build()
 {
     var nw = new WNDCLASSEX();
     nw.cbSize = Marshal.SizeOf(typeof (WNDCLASSEX));
     return nw;
 }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(GLVersion openGLVersion, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, width, height, bitDepth, parameter);

            //	Create a new window class, as basic as possible.                
            WNDCLASSEX wndClass = new WNDCLASSEX();
            wndClass.Init();
            wndClass.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw | ClassStyles.OwnDC;
            wndClass.lpfnWndProc = wndProcDelegate;
            wndClass.cbClsExtra = 0;
            wndClass.cbWndExtra = 0;
            wndClass.hInstance = IntPtr.Zero;
            wndClass.hIcon = IntPtr.Zero;
            wndClass.hCursor = IntPtr.Zero;
            wndClass.hbrBackground = IntPtr.Zero;
            wndClass.lpszMenuName = null;
            wndClass.lpszClassName = "SharpGLRenderWindow";
            wndClass.hIconSm = IntPtr.Zero;
            Win32.RegisterClassEx(ref wndClass);

            //	Create the window. Position and size it.
            windowHandle = Win32.CreateWindowEx(0,
                          "SharpGLRenderWindow",
                          "",
                          WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_POPUP,
                          0, 0, width, height,
                          IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            //	Get the window device context.
            DeviceContextHandle = Win32.GetDC(windowHandle);

            //	Setup a pixel format.
            PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR();
            pfd.Init();
            pfd.nVersion = 1;
            pfd.dwFlags = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER;
            pfd.iPixelType = Win32.PFD_TYPE_RGBA;
            pfd.cColorBits = (byte)bitDepth;
            pfd.cDepthBits = 16;
            pfd.cStencilBits = 8;
            pfd.iLayerType = Win32.PFD_MAIN_PLANE;

            //	Match an appropriate pixel format 
            int iPixelformat;
            if ((iPixelformat = Win32.ChoosePixelFormat(DeviceContextHandle, pfd)) == 0)
                return false;

            //	Sets the pixel format
            if (Win32.SetPixelFormat(DeviceContextHandle, iPixelformat, pfd) == 0)
            {
                return false;
            }

            //	Create the render context.
            RenderContextHandle = Win32.wglCreateContext(DeviceContextHandle);

            //  Make the context current.
            MakeCurrent();

            //  Update the context if required.
            UpdateContextVersion();

            //  Return success.
            return true;
        }