Example #1
0
        unsafe IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
        {
            switch (message)
            {
                #region Size / Move / Style events

            case WindowMessage.ACTIVATE:
                // See http://msdn.microsoft.com/en-us/library/ms646274(VS.85).aspx (WM_ACTIVATE notification):
                // wParam: The low-order word specifies whether the window is being activated or deactivated.
                bool new_focused_state = Focused;
                focused = (wParam.ToInt64() & 0xFFFF) != 0;

                if (new_focused_state != Focused && FocusedChanged != null)
                {
                    FocusedChanged(this, EventArgs.Empty);
                }
                break;

            case WindowMessage.ENTERMENULOOP:
            case WindowMessage.ENTERSIZEMOVE:
            case WindowMessage.EXITMENULOOP:
            case WindowMessage.EXITSIZEMOVE:
                break;

            case WindowMessage.ERASEBKGND:
                return(new IntPtr(1));

            case WindowMessage.WINDOWPOSCHANGED:
                WindowPosition *pos = (WindowPosition *)lParam;
                if (window != null && pos->hwnd == window.handle)
                {
                    Point new_location = new Point(pos->x, pos->y);
                    if (Location != new_location)
                    {
                        bounds.Location = new_location;
                        if (Move != null)
                        {
                            Move(this, EventArgs.Empty);
                        }
                    }

                    Size new_size = new Size(pos->cx, pos->cy);
                    if (Size != new_size)
                    {
                        bounds.Width  = pos->cx;
                        bounds.Height = pos->cy;

                        Win32Rectangle rect;
                        API.GetClientRect(handle, out rect);
                        client_rectangle = rect.ToRectangle();

                        API.SetWindowPos(window.handle, IntPtr.Zero,
                                         bounds.X, bounds.Y, bounds.Width, bounds.Height,
                                         SetWindowPosFlags.NOZORDER | SetWindowPosFlags.NOOWNERZORDER |
                                         SetWindowPosFlags.NOACTIVATE | SetWindowPosFlags.NOSENDCHANGING);
                        if (suppress_resize <= 0 && Resize != null)
                        {
                            Resize(this, EventArgs.Empty);
                        }
                    }
                }
                break;

            case WindowMessage.STYLECHANGED:
                if (wParam.ToInt64() == (long)GWL.STYLE)
                {
                    WindowStyle style = ((StyleStruct *)lParam)->New;
                    if ((style & WindowStyle.Popup) != 0)
                    {
                        hiddenBorder = true;
                    }
                    else if ((style & WindowStyle.ThickFrame) != 0)
                    {
                        hiddenBorder = false;
                    }
                }
                break;

            case WindowMessage.SIZE:
                SizeMessage state     = (SizeMessage)wParam.ToInt64();
                WindowState new_state = windowState;
                switch (state)
                {
                case SizeMessage.RESTORED: new_state = WindowState.Normal; break;

                case SizeMessage.MINIMIZED: new_state = WindowState.Minimized; break;

                case SizeMessage.MAXIMIZED: new_state = hiddenBorder ?
                                                        WindowState.Fullscreen : WindowState.Maximized;
                    break;
                }

                if (new_state != windowState)
                {
                    windowState = new_state;
                    if (WindowStateChanged != null)
                    {
                        WindowStateChanged(this, EventArgs.Empty);
                    }
                }
                break;

                #endregion

                #region Input events

            case WindowMessage.CHAR:
                key_press.KeyChar = (char)wParam.ToInt64();

                if (KeyPress != null)
                {
                    KeyPress(this, key_press);
                }
                break;

            case WindowMessage.MOUSEMOVE:
                Point point = new Point(
                    (short)((uint)lParam.ToInt32() & 0x0000FFFF),
                    (short)(((uint)lParam.ToInt32() & 0xFFFF0000) >> 16));
                mouse.Position = point;

                if (mouse_outside_window)
                {
                    // Once we receive a mouse move event, it means that the mouse has
                    // re-entered the window.
                    mouse_outside_window = false;
                    EnableMouseTracking();

                    if (MouseEnter != null)
                    {
                        MouseEnter(this, EventArgs.Empty);
                    }
                }
                break;

            case WindowMessage.MOUSELEAVE:
                mouse_outside_window = true;
                // Mouse tracking is disabled automatically by the OS

                if (MouseLeave != null)
                {
                    MouseLeave(this, EventArgs.Empty);
                }
                // Set all mouse buttons to off when user leaves window, prevents them being stuck down.
                for (MouseButton btn = 0; btn < MouseButton.LastButton; btn++)
                {
                    mouse[btn] = false;
                }
                break;

            case WindowMessage.MOUSEWHEEL:
                // This is due to inconsistent behavior of the WParam value on 64bit arch, whese
                // wparam = 0xffffffffff880000 or wparam = 0x00000000ff100000
                mouse.Wheel += ((long)wParam << 32 >> 48) / 120.0f;
                return(IntPtr.Zero);

            case WindowMessage.LBUTTONDOWN:
                mouse[MouseButton.Left] = true;
                break;

            case WindowMessage.MBUTTONDOWN:
                mouse[MouseButton.Middle] = true;
                break;

            case WindowMessage.RBUTTONDOWN:
                mouse[MouseButton.Right] = true;
                break;

            case WindowMessage.XBUTTONDOWN:
                keyboard[(((ulong)wParam.ToInt64() >> 16) & 0xFFFF) == 1 ? Key.XButton1 : Key.XButton2] = true;
                break;

            case WindowMessage.LBUTTONUP:
                mouse[MouseButton.Left] = false;
                break;

            case WindowMessage.MBUTTONUP:
                mouse[MouseButton.Middle] = false;
                break;

            case WindowMessage.RBUTTONUP:
                mouse[MouseButton.Right] = false;
                break;

            case WindowMessage.XBUTTONUP:
                keyboard[(((ulong)wParam.ToInt64() >> 16) & 0xFFFF) == 1 ? Key.XButton1 : Key.XButton2] = false;
                break;

            // Keyboard events:
            case WindowMessage.KEYDOWN:
            case WindowMessage.KEYUP:
            case WindowMessage.SYSKEYDOWN:
            case WindowMessage.SYSKEYUP:
                bool pressed = message == WindowMessage.KEYDOWN ||
                               message == WindowMessage.SYSKEYDOWN;

                // Shift/Control/Alt behave strangely when e.g. ShiftRight is held down and ShiftLeft is pressed
                // and released. It looks like neither key is released in this case, or that the wrong key is
                // released in the case of Control and Alt.
                // To combat this, we are going to release both keys when either is released. Hacky, but should work.
                // Win95 does not distinguish left/right key constants (GetAsyncKeyState returns 0).
                // In this case, both keys will be reported as pressed.

                bool extended = (lParam.ToInt64() & ExtendedBit) != 0;
                switch ((VirtualKeys)wParam)
                {
                case VirtualKeys.SHIFT:
                    // The behavior of this key is very strange. Unlike Control and Alt, there is no extended bit
                    // to distinguish between left and right keys. Moreover, pressing both keys and releasing one
                    // may result in both keys being held down (but not always).
                    bool lShiftDown = (API.GetKeyState((int)VirtualKeys.LSHIFT) >> 15) == 1;
                    bool rShiftDown = (API.GetKeyState((int)VirtualKeys.RSHIFT) >> 15) == 1;

                    if (!pressed || lShiftDown != rShiftDown)
                    {
                        Keyboard[Input.Key.ShiftLeft]  = lShiftDown;
                        Keyboard[Input.Key.ShiftRight] = rShiftDown;
                    }
                    return(IntPtr.Zero);

                case VirtualKeys.CONTROL:
                    if (extended)
                    {
                        keyboard[Input.Key.ControlRight] = pressed;
                    }
                    else
                    {
                        keyboard[Input.Key.ControlLeft] = pressed;
                    }
                    return(IntPtr.Zero);

                case VirtualKeys.MENU:
                    if (extended)
                    {
                        keyboard[Input.Key.AltRight] = pressed;
                    }
                    else
                    {
                        keyboard[Input.Key.AltLeft] = pressed;
                    }
                    return(IntPtr.Zero);

                case VirtualKeys.RETURN:
                    if (extended)
                    {
                        keyboard[Key.KeypadEnter] = pressed;
                    }
                    else
                    {
                        keyboard[Key.Enter] = pressed;
                    }
                    return(IntPtr.Zero);

                default:
                    Key tkKey;
                    if (!KeyMap.TryGetMappedKey((VirtualKeys)wParam, out tkKey))
                    {
                        Debug.Print("Virtual key {0} ({1}) not mapped.", (VirtualKeys)wParam, lParam.ToInt64());
                        break;
                    }
                    else
                    {
                        keyboard[tkKey] = pressed;
                    }
                    return(IntPtr.Zero);
                }
                break;

            case WindowMessage.SYSCHAR:
                return(IntPtr.Zero);

            case WindowMessage.KILLFOCUS:
                keyboard.ClearKeys();
                break;

                #endregion

                #region Creation / Destruction events

            case WindowMessage.CREATE:
                CreateStruct cs = (CreateStruct)Marshal.PtrToStructure(lParam, typeof(CreateStruct));
                if (cs.hwndParent == IntPtr.Zero)
                {
                    bounds.X      = cs.x;
                    bounds.Y      = cs.y;
                    bounds.Width  = cs.cx;
                    bounds.Height = cs.cy;

                    Win32Rectangle rect;
                    API.GetClientRect(handle, out rect);
                    client_rectangle = rect.ToRectangle();

                    invisible_since_creation = true;
                }
                break;

            case WindowMessage.CLOSE:
                System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs();

                if (Closing != null)
                {
                    Closing(this, e);
                }

                if (!e.Cancel)
                {
                    DestroyWindow();
                    break;
                }

                return(IntPtr.Zero);

            case WindowMessage.DESTROY:
                exists = false;

                API.UnregisterClass(ClassName, Instance);
                window.Dispose();

                if (Closed != null)
                {
                    Closed(this, EventArgs.Empty);
                }

                break;

                #endregion
            }
            return(API.DefWindowProc(handle, message, wParam, lParam));
        }
Example #2
0
 public void Close()
 {
     API.PostMessage(window.handle, WindowMessage.CLOSE, IntPtr.Zero, IntPtr.Zero);
 }