private static void Scale(RegionSelector selector) { Rect region = new Rect(selector.SourceX, selector.SourceY, selector.SourceWidth, selector.SourceHeight); selector.thumbnail.DrawnRegion = region; selector.thumbnail.Scale = Math.Min(ViewerWidth / region.Width, ViewerHeight / region.Height); }
private void panelRegionSelectionToolStripMenuItem_Click(object sender, EventArgs e) { ThumbnailPanel panel = (ThumbnailPanel)panelContextMenuStrip.SourceControl; RegionSelector selector = new RegionSelector(); selector.Load += (eventSender, eventArgs) => { selector.SetWindow(panel.GetWindow(), panel.ClientAreaOnly); selector.DrawnRegion = panel.DrawnRegion; }; if (selector.ShowDialog(this) == DialogResult.OK) { panel.DrawnRegion = selector.DrawnRegion; } }
private static void SourceHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RegionSelector selector = d as RegionSelector; if (selector != null) { double height = (double)e.NewValue; double sourceHeight = selector.thumbnail.SourceSize.Height; if (height < 0 || selector.SourceY + height > sourceHeight) { selector.SourceHeight = sourceHeight - selector.SourceX; } Scale(selector); } }
private static void SourceWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RegionSelector selector = d as RegionSelector; if (selector != null) { double width = (double)e.NewValue; double sourceWidth = selector.thumbnail.SourceSize.Width; if (width < 0 || selector.SourceX + width > sourceWidth) { selector.SourceWidth = sourceWidth - selector.SourceX; } Scale(selector); } }
private static void SourceYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RegionSelector selector = d as RegionSelector; if (selector != null) { double y = (double)e.NewValue; double sourceHeight = selector.thumbnail.SourceSize.Height; if (y < 0 || y >= sourceHeight) { selector.SourceY = (double)e.OldValue; } if (y + selector.SourceHeight > sourceHeight) { selector.SourceHeight = sourceHeight - y; } Scale(selector); } }
private static void SourceXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RegionSelector selector = d as RegionSelector; if (selector != null) { double x = (double)e.NewValue; double sourceWidth = selector.thumbnail.SourceSize.Width; if (x < 0 || x >= sourceWidth) { selector.SourceX = (double)e.OldValue; } if (x + selector.SourceWidth > sourceWidth) { selector.SourceWidth = sourceWidth - x; } Scale(selector); } }
private void SetContextMenu(Thumbnail thumbnail) { ContextMenu contextMenu = new ContextMenu(); MenuItem toggleItem = new MenuItem() { Header = Globalization.UIMessages.ToggleClientOnlyMenuItem, IsCheckable = true, }; Binding checkedBinding = new Binding("IsChecked") { Source = toggleItem, }; thumbnail.SetBinding(Thumbnail.ClientAreaOnlyProperty, checkedBinding); contextMenu.Items.Add(toggleItem); MenuItem selectorItem = new MenuItem() { Header = Globalization.UIMessages.SelectRegionMenuItem, }; Binding enabledBinding = new Binding("IsEnabled") { Source = selectorItem, Converter = Converters.BooleanToggleConverter.Instance, Mode = BindingMode.OneWayToSource, }; toggleItem.SetBinding(MenuItem.IsCheckedProperty, enabledBinding); selectorItem.Click += (sender, e) => { Rect region = thumbnail.DrawnRegion; RegionSelector selector = new RegionSelector(); selector.thumbnail.Scale = Math.Min(RegionSelector.ViewerWidth / region.Width, RegionSelector.ViewerHeight / region.Height); selector.thumbnail.ClientAreaOnly = thumbnail.ClientAreaOnly; selector.Loaded += (a, b) => { selector.Background = Brushes.Transparent; selector.ExtendFrame(RegionSelector.AeroRegion, Colors.Transparent); selector.thumbnail.SetWindow(thumbnail.Target); selector.SourceX = region.X; selector.SourceY = region.Y; selector.SourceWidth = region.Width; selector.SourceHeight = region.Height; }; selector.ContentRendered += (a, b) => { selector.thumbnail.DrawnRegion = region; }; if (selector.ShowDialog().GetValueOrDefault(false) == true) { thumbnail.DrawnRegion = selector.thumbnail.DrawnRegion; } }; contextMenu.Items.Add(selectorItem); contextMenu.Items.Add(new Separator()); MenuItem removeItem = new MenuItem() { Header = Globalization.UIMessages.RemoveMenuItem, }; removeItem.Click += (sender, e) => { thumbnail.Dispose(); canvas.Children.Remove(thumbnail); }; contextMenu.Items.Add(removeItem); thumbnail.ContextMenu = contextMenu; }