Enlarge canvas in WPF using code

Here's the script:


I have a canvas with different diagrams drawn on it. Now you need to zoom in using the code located either with C # or with VB. Moreover, I need to put the scaling code in some dll so that I can reuse the same set of code outside my application.

Now my question is: how to do this ....

I tried using the following code, which is.

public MainWindow()
{
    InitializeComponent();

    canvas.MouseEnter += new MouseEventHandler(canvas_MouseEnter);
    canvas.MouseWheel += new MouseWheelEventHandler(canvas_MouseWheel);
}

void canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
    double height = canvas.ActualHeight;
    double width = canvas.ActualWidth;
    double zoom = e.Delta;
    height += 2;
    width += 2;
    ScaleTransform sc = new ScaleTransform(width, height);
    canvas.LayoutTransform = sc;
    canvas.UpdateLayout();
}
+3
source share
2 answers

, . - , - . "Zoom Behaviors", . ...

+3

:

    var canvas = new Canvas();
    var st = new ScaleTransform();
    var textBox = new TextBox {Text = "Test"};
    canvas.RenderTransform = st;
    canvas.Children.Add(textBox);
    canvas.MouseWheel += (sender, e) =>
    {
        if (e.Delta > 0)
        {
            st.ScaleX *= 2;
            st.ScaleY *= 2;
        }
        else
        {
            st.ScaleX /= 2;
            st.ScaleY /= 2;
        }
    };
0

Source: https://habr.com/ru/post/1769938/


All Articles