uint WindowProc(IntPtr hwnd, NativeAPI.WindowMessage uMsg, uint wParam, uint lParam) { switch (uMsg) { case NativeAPI.WindowMessage.DESTROY: NativeAPI.PostQuitMessage(0); break; case NativeAPI.WindowMessage.CLOSE: OnClose(); return 0; case NativeAPI.WindowMessage.SIZE: NativeAPI.Rectangle rect = new NativeAPI.Rectangle(); NativeAPI.GetClientRect(handle, out rect); if (!Fullscreen && (Width != rect.Right || Height != rect.Bottom)) { Width = rect.Right; Height = rect.Bottom; OnResize(Width, Height); } break; case NativeAPI.WindowMessage.ACTIVATE: if (wParam == 0) { Focused = false; OnFocusLost(); if (mouseCaptured) { NativeAPI.ClipCursor(IntPtr.Zero); NativeAPI.ShowCursor(true); } } else { Focused = true; OnFocusGained(); if (mouseCaptured) { NativeAPI.ShowCursor(false); lastX = Width / 2; lastY = Height / 2; NativeAPI.Rectangle crect = new NativeAPI.Rectangle(0, 0, Width, Height); NativeAPI.ClientToScreen(handle, ref crect.LeftTop); NativeAPI.ClientToScreen(handle, ref crect.RightBottom); MoveMouseCursorToMiddle(crect); NativeAPI.ClipCursor(ref crect); } } break; case NativeAPI.WindowMessage.CHAR: OnEvent(new InputEvents.TextEvent((char)wParam)); break; case NativeAPI.WindowMessage.KEYDOWN: case NativeAPI.WindowMessage.SYSKEYDOWN: if ((lParam & (1 << 30)) == 0) { InputEvents.Key keyDown = (wParam == NativeAPI.VK_SHIFT ? GetShiftState(true) : FromVirtualKeyCode(wParam, lParam)); bool altDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_MENU) >> 16) != 0; bool ctrlDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_CONTROL) >> 16) != 0; bool shiftDown = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_SHIFT) >> 16) != 0; OnEvent(new InputEvents.KeyPressedEvent(keyDown, altDown, ctrlDown, shiftDown)); } break; case NativeAPI.WindowMessage.KEYUP: case NativeAPI.WindowMessage.SYSKEYUP: InputEvents.Key keyUp = (wParam == NativeAPI.VK_SHIFT ? GetShiftState(false) : FromVirtualKeyCode(wParam, lParam)); bool altUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_MENU) >> 16) != 0; bool ctrlUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_CONTROL) >> 16) != 0; bool shiftUp = (NativeAPI.GetAsyncKeyState(NativeAPI.VK_SHIFT) >> 16) != 0; OnEvent(new InputEvents.KeyReleasedEvent(keyUp, altUp, ctrlUp, shiftUp)); break; case NativeAPI.WindowMessage.MOUSEWHEEL: OnEvent(new InputEvents.MouseWheelEvent((int)(wParam >> 16) / 120)); break; case NativeAPI.WindowMessage.LBUTTONDOWN: NativeAPI.SetCapture(hwnd); OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Left, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); break; case NativeAPI.WindowMessage.LBUTTONUP: OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Left, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); NativeAPI.ReleaseCapture(); break; case NativeAPI.WindowMessage.RBUTTONDOWN: NativeAPI.SetCapture(hwnd); OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Right, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); break; case NativeAPI.WindowMessage.RBUTTONUP: OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Right, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); NativeAPI.ReleaseCapture(); break; case NativeAPI.WindowMessage.MBUTTONDOWN: NativeAPI.SetCapture(hwnd); OnEvent(new InputEvents.MouseButtonPressedEvent(InputEvents.MouseButton.Middle, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); break; case NativeAPI.WindowMessage.MBUTTONUP: OnEvent(new InputEvents.MouseButtonReleasedEvent(InputEvents.MouseButton.Middle, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); NativeAPI.ReleaseCapture(); break; case NativeAPI.WindowMessage.XBUTTONDOWN: NativeAPI.SetCapture(hwnd); InputEvents.MouseButton xButtonDown = (wParam >> 16 == NativeAPI.VK_XBUTTON1 ? InputEvents.MouseButton.XButton1 : InputEvents.MouseButton.XButton2); OnEvent(new InputEvents.MouseButtonPressedEvent(xButtonDown, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); break; case NativeAPI.WindowMessage.XBUTTONUP: InputEvents.MouseButton xButtonUp = (wParam >> 16 == NativeAPI.VK_XBUTTON1 ? InputEvents.MouseButton.XButton1 : InputEvents.MouseButton.XButton2); OnEvent(new InputEvents.MouseButtonReleasedEvent(xButtonUp, (int)(lParam & 0xFFFF), (int)(lParam >> 16))); NativeAPI.ReleaseCapture(); break; case NativeAPI.WindowMessage.MOUSEMOVE: if (ignoreMouseMove) { ignoreMouseMove = false; } else if (Focused) { int newX = (int)(lParam & 0xFFFF); int newY = (int)(lParam >> 16); if (lastX != newX || lastY != newY) { OnEvent(new InputEvents.MouseMoveEvent(newX, newY)); lastX = newX; lastY = newY; } if (mouseCaptured) { NativeAPI.Rectangle mrect = new NativeAPI.Rectangle(0, 0, Width, Height); NativeAPI.ClientToScreen(handle, ref mrect.LeftTop); NativeAPI.ClientToScreen(handle, ref mrect.RightBottom); MoveMouseCursorToMiddle(mrect); } } break; } return NativeAPI.DefWindowProc(hwnd, uMsg, wParam, lParam); }
public void SetPositionSameAs(IPlatformWindow existingWindow) { WindowsWindow w = (WindowsWindow)existingWindow; NativeAPI.Rectangle rect = new NativeAPI.Rectangle(); NativeAPI.GetWindowRect(w.handle, out rect); NativeAPI.SetWindowPos(handle, IntPtr.Zero, rect.Left, rect.Top, 0, 0, NativeAPI.WindowPosFlags.NoSize); }
void GetWindowStyle(bool fullscreen, bool resizable, ref int width, ref int height, out int x, out int y, out NativeAPI.WindowStyle style, out NativeAPI.WindowStyleEx styleEx) { style = NativeAPI.WindowStyle.ClipChildren | NativeAPI.WindowStyle.ClipSiblings; styleEx = 0; if (fullscreen) { style |= NativeAPI.WindowStyle.Popup; styleEx |= NativeAPI.WindowStyleEx.Topmost | NativeAPI.WindowStyleEx.ApplicationWindow; x = 0; y = 0; } else { style |= NativeAPI.WindowStyle.Caption | NativeAPI.WindowStyle.SystemMenu | NativeAPI.WindowStyle.MinimizeBox; if (resizable) { style |= NativeAPI.WindowStyle.SizeBox | NativeAPI.WindowStyle.MaximizeBox; } styleEx |= NativeAPI.WindowStyleEx.WindowEdge; NativeAPI.Rectangle rect = new NativeAPI.Rectangle(0, 0, width, height); NativeAPI.AdjustWindowRectEx(ref rect, style, false, styleEx); x = NativeAPI.CW_USEDEFAULT; y = NativeAPI.CW_USEDEFAULT; width = rect.Right - rect.Left; height = rect.Bottom - rect.Top; } }
public void SetMouseCaptured(bool capture) { if (capture) { if (Focused) { NativeAPI.ShowCursor(false); } NativeAPI.GetCursorPos(out mousePos); lastX = Width / 2; lastY = Height / 2; if (Focused) { NativeAPI.Rectangle rect = new NativeAPI.Rectangle(0, 0, Width, Height); NativeAPI.ClientToScreen(handle, ref rect.LeftTop); NativeAPI.ClientToScreen(handle, ref rect.RightBottom); MoveMouseCursorToMiddle(rect); NativeAPI.ClipCursor(ref rect); } } else { if (Focused) { NativeAPI.ClipCursor(IntPtr.Zero); ignoreMouseMove = true; NativeAPI.SetCursorPos(mousePos.x, mousePos.y); NativeAPI.ShowCursor(true); } NativeAPI.ScreenToClient(handle, ref mousePos); lastX = mousePos.x; lastY = mousePos.y; } mouseCaptured = capture; }