示例#1
0
        public virtual IntPtr WindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch ((WM)msg)
            {
            case WM.ACTIVATE:
            {
                // Initialize offset on window first activation
                // Note: this only applies for non-layered style window
                if (!_window.AllowsTransparency &&
                    _window.WindowStyle != WindowStyle.None &&
                    _offset == null)
                {
                    var offset = GetEdgeOffset();

                    _offset = offset.Value;

                    EdgeOffsetChanged(this, new EdgeOffsetChangedEventArgs(offset));
                }

                break;
            }

            case WM.SYSCOMMAND:
            {
                // To obtain the correct result when testing the value of wParam,
                // an application must combine the value 0xFFF0 with the wParam
                // value by using the bitwise AND operator.
                SC command = (SC)(wParam.ToInt32() & 0xFFF0);

                _sizing = command == SC.SIZE;
                _moving = command == SC.MOVE;

                break;
            }

            case WM.SIZING:
            {
                _sizingEdge = (WMSZ)wParam.ToInt32();
                break;
            }

            case WM.EXITSIZEMOVE:
            {
                _sizingEdge = WMSZ.NONE;
                break;
            }

            case WM.WINDOWPOSCHANGING:
            {
                WINDOWPOS windowPos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));

                if (Helpers.WindowChanged(windowPos))
                {
                    switch (ActualState)
                    {
                    case WindowState.Minimized:

                        break;

                    case WindowState.Maximized:

                        Unsnap();

                        break;

                    default:

                        // Proceed to snap detection if the current system settings
                        // allow snapping or if window is currently snapped
                        if (CanSnap() || _snapped)
                        {
                            // Get the list of monitors that intersect with the window area
                            List <Monitor> monitors = SafeNativeMethods.GetDisplayMonitors(new RECT
                                {
                                    left   = windowPos.x,
                                    top    = windowPos.y,
                                    right  = windowPos.x + windowPos.cx,
                                    bottom = windowPos.y + windowPos.cy
                                });

                            Rect location = new Rect(
                                Dpi.ToLogicalX(windowPos.x),
                                Dpi.ToLogicalY(windowPos.y),
                                Dpi.ToLogicalX(windowPos.cx),
                                Dpi.ToLogicalY(windowPos.cy));

                            SnapResult snapResult = DetectSnap.IsSnapped(ref location, monitors, Offset);

                            if (snapResult.IsSnapped)
                            {
                                Snap();
                            }
                            else if (Unsnapping)
                            {
                                Unsnap();
                            }
                        }

                        break;
                    }
                }

                break;
            }
            }

            return(IntPtr.Zero);
        }