开发者

Canvas zooming in WPF using code behind

Here the scenario is:

I have a canvas with different diagrams drawn on it. Now the requirement is to zoom into the canvas using the code behind either using C# or VB. Moreover I need to place the zoom code in some dll so that i can reuse the same set of code through out my application.

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

I have tried the following code pls have a look..

public MainWindow()
{
    InitializeComponent();

    canvas.MouseEnter += new MouseEventHandler(canvas_MouseEnter);开发者_如何学C
    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();
}


Try to implement this sample:

    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;
        }
    };


I believe what you are looking for is a zoom behavior. Behaviors are objects that encapsulate some form of interactive behavior. I've seen several examples of "Zoom Behaviors" that you should be able to use for your project. You should be able to use or modify one of the following...

  • Laurent Bugnion's Zoom Behavior
  • WPF Extensions - has a zoom control
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜