private void InitializeFromWindowsForms(GameContext uiContext) { uiControl = (Control)uiContext.Control; pointerClock.Restart(); if (UseRawInput) { BindRawInputKeyboard(uiControl); } else { EnsureMapKeys(); defaultWndProc = Win32Native.GetWindowLong(new HandleRef(this, uiControl.Handle), Win32Native.WindowLongType.WndProc); // This is needed to prevent garbage collection of the delegate. inputWndProc = WndProc; var inputWndProcPtr = Marshal.GetFunctionPointerForDelegate(inputWndProc); Win32Native.SetWindowLong(new HandleRef(this, uiControl.Handle), Win32Native.WindowLongType.WndProc, inputWndProcPtr); } uiControl.GotFocus += (_, e) => OnUiControlGotFocus(); uiControl.LostFocus += (_, e) => OnUiControlLostFocus(); uiControl.MouseMove += (_, e) => OnMouseMoveEvent(new Vector2(e.X, e.Y)); uiControl.MouseDown += (_, e) => { uiControl.Focus(); OnMouseInputEvent(new Vector2(e.X, e.Y), ConvertMouseButton(e.Button), InputEventType.Down); }; uiControl.MouseUp += (_, e) => OnMouseInputEvent(new Vector2(e.X, e.Y), ConvertMouseButton(e.Button), InputEventType.Up); uiControl.MouseWheel += (_, e) => OnMouseInputEvent(new Vector2(e.X, e.Y), MouseButton.Middle, InputEventType.Wheel, e.Delta); uiControl.MouseCaptureChanged += (_, e) => OnLostMouseCaptureWinForms(); uiControl.SizeChanged += UiControlOnSizeChanged; ControlWidth = uiControl.ClientSize.Width; ControlHeight = uiControl.ClientSize.Height; }
private MessageFilterHook(IntPtr hwnd) { this.currentFilters = new List<IMessageFilter>(); this.hwnd = hwnd; this.defaultWndProc = Win32Native.GetWindowLong(new HandleRef((object) this, hwnd), Win32Native.WindowLongType.WndProc); this.newWndProc = new Win32Native.WndProc(this.WndProc); this.newWndProcPtr = Marshal.GetFunctionPointerForDelegate((Delegate) this.newWndProc); Win32Native.SetWindowLong(new HandleRef((object) this, hwnd), Win32Native.WindowLongType.WndProc, this.newWndProcPtr); }
private MessageFilterHook(IntPtr hwnd) { this.currentFilters = new List <IMessageFilter>(); this.hwnd = hwnd; this.defaultWndProc = Win32Native.GetWindowLong(new HandleRef((object)this, hwnd), Win32Native.WindowLongType.WndProc); this.newWndProc = new Win32Native.WndProc(this.WndProc); this.newWndProcPtr = Marshal.GetFunctionPointerForDelegate((Delegate)this.newWndProc); Win32Native.SetWindowLong(new HandleRef((object)this, hwnd), Win32Native.WindowLongType.WndProc, this.newWndProcPtr); }
/// <summary> /// Initializes a new instance of the <see cref="MessageFilterHook" /> class. /// </summary> /// <param name="windowHandle">The handle of the window whose <c>WndProc</c> function has to be hooked.</param> private MessageFilterHook(IntPtr windowHandle) { this.windowHandle = windowHandle; defaultWndProc = Win32Native.GetWindowLong(windowHandle, Win32Native.WindowLongType.WndProc); newWndProc = new Win32Native.WndProc(WndProc); newWndProcPtr = Marshal.GetFunctionPointerForDelegate((Delegate)this.newWndProc); currentFilters = new List <IMessageFilter>(); Win32Native.SetWindowLong(windowHandle, Win32Native.WindowLongType.WndProc, this.newWndProcPtr); }
/// <summary> /// Initializes a new instance of the <see cref="Win32.MessageFilterHook" /> class. /// </summary> /// <param name="hwnd">The HWND.</param> private MessageFilterHook(IntPtr hwnd) { this.currentFilters = new List <IMessageFilter>(); this.hwnd = hwnd; // Retrieve the previous WndProc associated with this window handle defaultWndProc = Win32Native.GetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc); // Create a pointer to the new WndProc newWndProc = WndProc; newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc); // Set our own private wndproc in order to catch NCDestroy message Win32Native.SetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc, newWndProcPtr); }
/// <summary> /// Initializes a new instance of the <see cref="MessageFilterHook" /> class. /// </summary> /// <param name="hwnd">The HWND.</param> private MessageFilterHook(IntPtr hwnd) { this.currentFilters = new List<IMessageFilter>(); this.hwnd = hwnd; // Retrieve the previous WndProc associated with this window handle defaultWndProc = Win32Native.GetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc); // Create a pointer to the new WndProc newWndProc = WndProc; newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc); // Set our own private wndproc in order to catch NCDestroy message Win32Native.SetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc, newWndProcPtr); }
public KeyboardWinforms(InputSourceWinforms source, Control uiControl) { Source = source; this.uiControl = uiControl; richTextBox = new RichTextBox { Location = new Point(-100, -100), Size = new Size(80, 80) }; // Assign custom window procedure to this text box wndProcDelegate = WndProc; var windowProc = Marshal.GetFunctionPointerForDelegate(wndProcDelegate); oldWndProc = Win32Native.SetWindowLong(richTextBox.Handle, Win32Native.WindowLongType.WndProc, windowProc); }
/// <summary> /// Private rendering loop /// </summary> public void Run(RenderCallback renderCallback) { _wndProc = new Win32Native.WndProc(WndProc); // Set our own private wndproc in order to catch NCDestroy message Win32Native.SetWindowLong(new HandleRef(this, _windowHandle), Win32Native.WindowLongType.WndProc, _wndProc); // Show the form _form.Show(); // Main rendering loop); while (_isAlive) { if (UseCustomDoEvents) { // Previous code not compatible with Application.AddMessageFilter but faster then DoEvents Win32Native.NativeMessage msg; while (Win32Native.PeekMessage(out msg, _windowHandle, 0, 0, 0) != 0) { if (Win32Native.GetMessage(out msg, _windowHandle, 0, 0) == -1) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "An error happened in rendering loop while processing windows messages. Error: {0}", Marshal.GetLastWin32Error())); } Win32Native.TranslateMessage(ref msg); Win32Native.DispatchMessage(ref msg); } } else { // Revert back to Application.DoEvents in order to support Application.AddMessageFilter // Seems that DoEvents is compatible with Mono unlike Application.Run that was not running // correctly. Application.DoEvents(); } if (_isAlive) { renderCallback(); } } _form.Disposed -= _form_Disposed; }
public override void Initialize(InputManager inputManager) { input = inputManager; uiControl.LostFocus += UIControlOnLostFocus; // Hook window proc defaultWndProc = Win32Native.GetWindowLong(uiControl.Handle, Win32Native.WindowLongType.WndProc); // This is needed to prevent garbage collection of the delegate. inputWndProc = WndProc; var inputWndProcPtr = Marshal.GetFunctionPointerForDelegate(inputWndProc); Win32Native.SetWindowLong(uiControl.Handle, Win32Native.WindowLongType.WndProc, inputWndProcPtr); // Do not register keyboard devices when using raw input instead keyboard = new KeyboardWinforms(this, uiControl); RegisterDevice(keyboard); mouse = new MouseWinforms(this, uiControl); RegisterDevice(mouse); }
/// <summary> /// Private rendering loop /// </summary> public void Run(RenderCallback renderCallback) { _wndProc = new Win32Native.WndProc(WndProc); // Set our own private wndproc in order to catch NCDestroy message Win32Native.SetWindowLong(new HandleRef(this, _windowHandle), Win32Native.WindowLongType.WndProc, _wndProc); // Show the form _form.Show(); // Main rendering loop); while (_isAlive) { if (UseCustomDoEvents) { // Previous code not compatible with Application.AddMessageFilter but faster then DoEvents Win32Native.NativeMessage msg; while (Win32Native.PeekMessage(out msg, _windowHandle, 0, 0, 0) != 0) { if (Win32Native.GetMessage(out msg, _windowHandle, 0, 0) == -1) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "An error happened in rendering loop while processing windows messages. Error: {0}", Marshal.GetLastWin32Error())); } Win32Native.TranslateMessage(ref msg); Win32Native.DispatchMessage(ref msg); } } else { // Revert back to Application.DoEvents in order to support Application.AddMessageFilter // Seems that DoEvents is compatible with Mono unlike Application.Run that was not running // correctly. Application.DoEvents(); } if (_isAlive) renderCallback(); } _form.Disposed -= _form_Disposed; }
private void InitializeFromWindowsForms(GameContext uiContext) { uiControl = (Control) uiContext.Control; pointerClock.Restart(); if (UseRawInput) { BindRawInputKeyboard(uiControl); } else { EnsureMapKeys(); defaultWndProc = Win32Native.GetWindowLong(new HandleRef(this, uiControl.Handle), Win32Native.WindowLongType.WndProc); // This is needed to prevent garbage collection of the delegate. inputWndProc = WndProc; var inputWndProcPtr = Marshal.GetFunctionPointerForDelegate(inputWndProc); Win32Native.SetWindowLong(new HandleRef(this, uiControl.Handle), Win32Native.WindowLongType.WndProc, inputWndProcPtr); } uiControl.GotFocus += (_, e) => OnUiControlGotFocus(); uiControl.LostFocus += (_, e) => OnUiControlLostFocus(); uiControl.MouseMove += (_, e) => OnMouseMoveEvent(new Vector2(e.X, e.Y)); uiControl.MouseDown += (_, e) => { uiControl.Focus(); OnMouseInputEvent(new Vector2(e.X, e.Y), ConvertMouseButton(e.Button), InputEventType.Down); }; uiControl.MouseUp += (_, e) => OnMouseInputEvent(new Vector2(e.X, e.Y), ConvertMouseButton(e.Button), InputEventType.Up); uiControl.MouseWheel += (_, e) => OnMouseInputEvent(new Vector2(e.X, e.Y), MouseButton.Middle, InputEventType.Wheel, e.Delta); uiControl.MouseCaptureChanged += (_, e) => OnLostMouseCaptureWinForms(); uiControl.SizeChanged += UiControlOnSizeChanged; ControlWidth = uiControl.ClientSize.Width; ControlHeight = uiControl.ClientSize.Height; }