private PlotDeviceProperties GetPixelSizeAndResolution(Size wpfSize) { var source = PresentationSource.FromVisual(Content as Visual); if (source != null) { var unadjustedPixelSize = WpfUnitsConversion.ToPixels(source, wpfSize); // If the window gets below a certain minimum size, plot to the minimum size _lastPixelWidth = Math.Max((int)unadjustedPixelSize.Width, MinPixelWidth); _lastPixelHeight = Math.Max((int)unadjustedPixelSize.Height, MinPixelHeight); _lastResolution = WpfUnitsConversion.GetResolution(source); } // The PresentationSource will be null in the specific scenario where: // - Host is creating a graphics device, requesting properties of the plot window // - This plot window is docked and has never been visible // // In that case, it is okay to re-use the last properties that we've // seen for this window. The window will have received the appropriate // SizeChanged events prior to this, and those were able to get a // non-null PresentationSource. // // I do not understand why, this might be some optimization that // VS/WPF is doing for hidden document windows. // // The fallback to the Minimum size when last properties are null // is only there as a precaution, I have not seen it happen in my // testing. If they ever happen, you'll see a low resolution plot // when you activate the plot window. return(new PlotDeviceProperties(_lastPixelWidth ?? MinPixelWidth, _lastPixelHeight ?? MinPixelHeight, _lastResolution ?? DefaultResolution)); }
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var image = (FrameworkElement)sender; var pos = e.GetPosition(image); var pixelSize = WpfUnitsConversion.ToPixels(PresentationSource.FromVisual(image as Visual), pos); Model?.ClickPlot((int)pixelSize.X, (int)pixelSize.Y); }
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { var unadjustedPixelSize = WpfUnitsConversion.ToPixels(Content as Visual, e.NewSize); // If the window gets below a certain minimum size, plot to the minimum size int pixelWidth = Math.Max((int)unadjustedPixelSize.Width, MinPixelWidth); int pixelHeight = Math.Max((int)unadjustedPixelSize.Height, MinPixelHeight); int resolution = WpfUnitsConversion.GetResolution(Content as Visual); Model?.ResizePlotAfterDelay(pixelWidth, pixelHeight, resolution); }
private Size GetPixelSize(int width, int height) { var source = PresentationSource.FromVisual(_plotVisualComponent.Control as Visual); return(WpfUnitsConversion.ToPixels(source, new Size(width, height))); }