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"); }
//string ResolveMsiShortcut(string file) //{ // StringBuilder product = new StringBuilder(MaxGuidLength + 1); // StringBuilder feature = new StringBuilder(MaxFeatureLength + 1); // StringBuilder component = new StringBuilder(MaxGuidLength + 1); // MsiGetShortcutTarget(file, product, feature, component); // int pathLength = MaxPathLength; // StringBuilder path = new StringBuilder(pathLength); // InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength); // if (installState == InstallState.Local) // { // return path.ToString(); // } // else // { // return null; // } //} new public void Show() { //Ststem.Drawing.Point var screen = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Cursor.Position); Left = SystemScaling.RealPixelsToWpfUnits(screen.WorkingArea.Left); Top = SystemScaling.RealPixelsToWpfUnits(screen.WorkingArea.Top); Height = SystemScaling.RealPixelsToWpfUnits(screen.WorkingArea.Height); base.Show(); BeginStoryboard((Storyboard)Resources["ShowMenu"]); }
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(); } }
private static void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e) { //Debug.WriteLine("ListBoxSelectionChanged"); ListBox listBox = sender as ListBox; if (listBox != null) { double scrollTo = 0; bool confirmScroll = true; if (e.AddedItems.Count > 0) { Point listPoint = _listBoxScroller.PointToScreen(new Point(0, 0)); listPoint.X = SystemScaling.RealPixelsToWpfUnits(listPoint.X); listPoint.Y = SystemScaling.RealPixelsToWpfUnits(listPoint.Y); ListBoxItem tempItem = listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[listBox.Items.IndexOf(e.AddedItems[0])]) as ListBoxItem; Point itemPoint = tempItem.PointToScreen(new Point(0, 0)); itemPoint.X = SystemScaling.RealPixelsToWpfUnits(itemPoint.X); itemPoint.Y = SystemScaling.RealPixelsToWpfUnits(itemPoint.Y); //Debug.WriteLine("MEASURED VALUES: " + itemPoint.Y + ", " + listPoint.Y + ", " + tempItem.ActualHeight + ", " + _listBoxScroller.ActualHeight); if (itemPoint.Y < listPoint.Y) { //Debug.WriteLine("SCROLLING UP: " + itemPoint.Y + ", " + listPoint.Y); scrollTo = _listBoxScroller.VerticalOffset + (itemPoint.Y - listPoint.Y); //Debug.WriteLine("scrollTo: " + scrollTo); } else if ((itemPoint.Y + tempItem.ActualHeight) > (listPoint.Y + _listBoxScroller.ActualHeight + _listBoxScroller.Padding.Top + _listBoxScroller.Padding.Bottom)) { //Debug.WriteLine("SCROLLING DOWN: " + (itemPoint.Y + tempItem.ActualHeight) + ", " + (listPoint.Y + _listBoxScroller.ActualHeight + _listBoxScroller.Padding.Top + _listBoxScroller.Padding.Bottom)); scrollTo = (itemPoint.Y + tempItem.ActualHeight) - ((listPoint.Y + _listBoxScroller.ActualHeight) - _listBoxScroller.ContentVerticalOffset); } else { confirmScroll = false; } /*for (int i = 0; i < (listbox.Items.IndexOf(e.AddedItems[0])); i++) * { * ListBoxItem tempItem = listbox.ItemContainerGenerator.ContainerFromItem(listbox.Items[i]) as ListBoxItem; * * if (tempItem != null) * { * scrollTo += tempItem.ActualHeight; * } * }*/ } else if (e.RemovedItems.Count > 0) { for (int i = 0; i < (listBox.Items.IndexOf(e.RemovedItems[0])); i++) { ListBoxItem tempItem = listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[i]) as ListBoxItem; if (tempItem != null) { scrollTo += tempItem.ActualHeight; } } } else { for (int i = 0; i < (listBox.SelectedIndex); i++) { ListBoxItem tempItem = listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[i]) as ListBoxItem; if (tempItem != null) { scrollTo += tempItem.ActualHeight; } } } if (confirmScroll) { AnimateScroll(_listBoxScroller, scrollTo); } } }