void Swap(FrameworkElement element)
        {
            BitmapImage bmp = new BitmapImage();

            using (var fileStream = new MemoryStream())
            {
                RenderTargetBitmap bitmap = new RenderTargetBitmap((Int32)SystemScaling.WpfUnitsToRealPixels(element.ActualWidth), (Int32)SystemScaling.WpfUnitsToRealPixels(element.ActualHeight), 96, 96, PixelFormats.Pbgra32);
                bitmap.Render(element);

                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(fileStream);

                bmp.BeginInit();
                bmp.CacheOption  = BitmapCacheOption.OnLoad;
                bmp.StreamSource = fileStream;
                bmp.EndInit();
            }

            var img = new Image()
            {
                /*Width = element.ActualWidth,
                 * Height = element.ActualHeight,*/
                Source = bmp
            };

            RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);
            _originalContent         = AssociatedObject.Content as FrameworkElement;
            AssociatedObject.Content = img;
            img.Loaded += (sneder, args) => AssociatedObject.SizeChanged += AssociatedObject_SizeChanged;

            //Debug.WriteLine("Swap complete");
        }
Пример #2
0
        private void DragMoveGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AppBarDockMode targetMode = DockMode;
            var            timer      = new System.Timers.Timer(10);

            Window highlightWindow = new Window()
            {
                Topmost       = true,
                ShowInTaskbar = false,
                WindowStyle   = WindowStyle.None,
                ResizeMode    = ResizeMode.NoResize,
                Background    = new SolidColorBrush(Colors.Red)
            };

            //highlightWindow.Show();
            timer.Elapsed += (sneder, args) =>
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    if (Mouse.LeftButton == MouseButtonState.Pressed)
                    {
                        var curPos   = System.Windows.Forms.Cursor.Position;
                        var winPoint = PointToScreen(new Point(0, 0));
                        if ((curPos.X > winPoint.X) &&
                            (curPos.Y > winPoint.Y) &&
                            (curPos.X <= SystemScaling.WpfUnitsToRealPixels(Left + ActualWidth)) &&
                            (curPos.Y <= SystemScaling.WpfUnitsToRealPixels(Top + ActualHeight))
                            )
                        {
                            targetMode = DockMode;
                            highlightWindow.Hide();
                        }
                        else
                        {
                            highlightWindow.Show();

                            var screen        = System.Windows.Forms.Screen.FromPoint(curPos);
                            double horizontal = (double)(curPos.X) / (double)(screen.Bounds.Width);
                            double vertical   = (double)(curPos.Y) / (double)(screen.Bounds.Height);

                            if (horizontal > 0.5)
                            {
                                if (vertical > 0.5)
                                {
                                    if (horizontal > vertical)
                                    {
                                        targetMode = AppBarDockMode.Right;
                                    }
                                    else
                                    {
                                        targetMode = AppBarDockMode.Bottom;
                                    }
                                }
                                else
                                {
                                    if (vertical > horizontal)
                                    {
                                        targetMode = AppBarDockMode.Right;
                                    }
                                    else
                                    {
                                        targetMode = AppBarDockMode.Top;
                                    }
                                }
                            }
                            else
                            {
                                if (vertical > 0.5)
                                {
                                    if (horizontal > vertical)
                                    {
                                        targetMode = AppBarDockMode.Left;
                                    }
                                    else
                                    {
                                        targetMode = AppBarDockMode.Bottom;
                                    }
                                }
                                else
                                {
                                    if (vertical > horizontal)
                                    {
                                        targetMode = AppBarDockMode.Left;
                                    }
                                    else
                                    {
                                        targetMode = AppBarDockMode.Top;
                                    }
                                }
                            }

                            if (targetMode == AppBarDockMode.Left)
                            {
                                highlightWindow.Left   = screen.WorkingArea.Left;
                                highlightWindow.Top    = screen.WorkingArea.Top;
                                highlightWindow.Width  = DockedWidthOrHeight;
                                highlightWindow.Height = screen.WorkingArea.Height;
                            }
                            else if (targetMode == AppBarDockMode.Top)
                            {
                                highlightWindow.Left   = screen.WorkingArea.Left;
                                highlightWindow.Top    = screen.WorkingArea.Top;
                                highlightWindow.Width  = screen.WorkingArea.Width;
                                highlightWindow.Height = DockedWidthOrHeight;
                            }
                            else if (targetMode == AppBarDockMode.Right)
                            {
                                highlightWindow.Left   = screen.WorkingArea.Right - DockedWidthOrHeight;
                                highlightWindow.Top    = screen.WorkingArea.Top;
                                highlightWindow.Width  = DockedWidthOrHeight;
                                highlightWindow.Height = screen.WorkingArea.Height;
                            }
                            else
                            {
                                highlightWindow.Left   = screen.WorkingArea.Left;
                                highlightWindow.Top    = screen.WorkingArea.Bottom - DockedWidthOrHeight;
                                highlightWindow.Width  = screen.WorkingArea.Width;
                                highlightWindow.Height = DockedWidthOrHeight;
                            }
                        }
                    }
                    else
                    {
                        DockMode = targetMode;
                        highlightWindow.Close();
                        timer.Stop();
                    }
                }));
            };

            if (IsUnlocked)
            {
                timer.Start();
            }
        }