Example #1
0
        private void InitEventHandlers()
        {
            //Bring Front On Click Events
            this.MouseLeftButtonDown += (s, e) => { BringFront(); };

            if (this.Content != null)
                (this.Content as FrameworkElement).MouseLeftButtonDown += (s, e) => { BringFront(); };

            //Window Resize Events
            if (IsResizable)
            {
                MouseEventHandler mouseLeave = (s, e) => { _resizeMode = SLWindowResizeMode.None; };
                MouseButtonEventHandler mouseUp = (s, e) => { _resizeMode = SLWindowResizeMode.None; };
                MouseEventHandler mouseMove = (s, e) =>
                {
                    if (_resizeMode != SLWindowResizeMode.None)
                    {
                        _mouseResizeCurrentPosition = e.GetPosition(null);
                        Resize();
                    }
                };
                MouseButtonEventHandler mouseDown = (s, e) =>
                {
                    FrameworkElement sender = s as FrameworkElement;
                    _windowResizeStartPosition = new Point()
                    {
                        X = (this.RenderTransform as TranslateTransform).X,
                        Y = (this.RenderTransform as TranslateTransform).Y
                    };
                    _mouseResizeStartPosition = e.GetPosition(null);
                    _resizeStartHeight = this.Height;
                    _resizeStartWidth = this.Width;
                    if (sender == _resizeRight)
                    {
                        _resizeMode = SLWindowResizeMode.Right;
                    }
                    else if (sender == _resizeLeft)
                    {
                        _resizeMode = SLWindowResizeMode.Left;
                    }
                    else if (sender == _resizeBotton)
                    {
                        _resizeMode = SLWindowResizeMode.Bottom;
                    }
                };

                _resizeRight.MouseLeftButtonDown += mouseDown;
                _resizeLeft.MouseLeftButtonDown += mouseDown;
                _resizeBotton.MouseLeftButtonDown += mouseDown;

                this.MouseLeftButtonUp += mouseUp;

                RootView.MouseMove += mouseMove;
                RootView.MouseMove += mouseMove;
                RootView.MouseMove += mouseMove;
            }
            else
            {
                _resizeLeft.Cursor = _resizeRight.Cursor = _resizeBotton.Cursor = Cursors.Arrow;
            }

            //Window Bar Button Events
            _closeButton.MouseLeftButtonUp += (s, e) => { Close(); };
            _minimizeButton.MouseLeftButtonUp += (s, e) => { WindowState = SLWindowState.Minimized; };
            _maximizeButton.MouseLeftButtonUp += (s, e) =>
            {
                if (WindowState == SLWindowState.Normal)
                    WindowState = SLWindowState.Maximized;
                else
                    WindowState = SLWindowState.Normal;
            };

            _title.Text = Title;

            //Mouse Drag-Move Events
            RootView.MouseMove += (s, e) => { _mouseMoveCurrentPosition = e.GetPosition(null); DragMove(); };
            _titleBar.MouseLeftButtonDown += (s, e) =>
            {
                _windowMoveStartPosition = new Point()
                {
                    X = (this.RenderTransform as TranslateTransform).X,
                    Y = (this.RenderTransform as TranslateTransform).Y
                };
                _isMouseDown = true;
                _mouseMoveStartPosition = e.GetPosition(null);
                e.Handled = true;
                BringFront();
            };
            _titleBar.MouseLeftButtonUp += (s, e) => { _isMouseDown = false; };

            //TemplateApplied Event
            if (TemplateApplied != null)
            {
                TemplateApplied(this, new EventArgs());
            }
        }
Example #2
0
        private void ChangeWindowState(SLWindowState state)
        {
            SLWindowState currentState = WindowState;
            if (currentState != state)
            {
                SetValue(WindowStateProperty, state);
                if (currentState == SLWindowState.Normal)
                {
                    _memorizedPosition.X = (this.RenderTransform as TranslateTransform).X;
                    _memorizedPosition.Y = (this.RenderTransform as TranslateTransform).Y;
                    _memorizedWidth = this.Width;
                    _memorizedHeight = this.Height;
                }
                switch (state)
                {
                    case SLWindowState.Normal:
                        if (currentState == SLWindowState.Minimized)
                            _minimizedWindowCount--;

                        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                        VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                        (this.RenderTransform as TranslateTransform).X = _memorizedPosition.X;
                        (this.RenderTransform as TranslateTransform).Y = _memorizedPosition.Y;
                        this.Width = _memorizedWidth;
                        this.Height = _memorizedHeight;
                        break;

                    case SLWindowState.Maximized:
                        if (currentState == SLWindowState.Minimized)
                            _minimizedWindowCount--;

                        this.Width = double.NaN;
                        this.Height = double.NaN;
                        this.RenderTransform = new TranslateTransform();
                        VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                        break;

                    case SLWindowState.Minimized:
                        this.Width = MinWidth;
                        this.Height = 38;
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                        VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                        this.RenderTransform = new TranslateTransform();
                        (this.RenderTransform as TranslateTransform).X = MinWidth * _minimizedWindowCount + _minimizedWindowCount * 3;
                        _minimizedWindowCount++;
                        break;

                    default:
                        break;
                }

                if (WindowStateChanged != null)
                    WindowStateChanged(this, new EventArgs());
            }
        }