Create a Zoomable Canvas Control : Canvas « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Canvas 
24.37.14.Create a Zoomable Canvas Control
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
   Title="WPF" Height="300" Width="300" >

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="Auto" />
            <Setter Property="Height" Value="24" />
        </Style>
    </Window.Resources>
    <DockPanel>
        <Slider DockPanel.Dock="Bottom" x:Name="zoomSlider" Minimum="0.1" Maximum="5" Value="1"/>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <WpfApplication1:ZoomableCanvasControl x:Name="zoomControl">
                <Canvas.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding Path=Value, ElementName=zoomSlider}"
                                    ScaleY="{Binding Path=Value, ElementName=zoomSlider}"/>
                </Canvas.LayoutTransform>
                <Rectangle Canvas.Top="0" Canvas.Left="0" StrokeThickness="2" Stroke="Red" Width="50" Height="50"/>
                <Rectangle Canvas.Top="50" Canvas.Left="50" StrokeThickness="2" Stroke="Blue" Width="150" Height="150"/>
                <Rectangle Canvas.Top="200" Canvas.Left="200" StrokeThickness="2" Stroke="Green" Width="200" Height="200"/>
            </WpfApplication1:ZoomableCanvasControl>
        </ScrollViewer>

    </DockPanel>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class ZoomableCanvasControl : Canvas
    {
        static ZoomableCanvasControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomableCanvasControl),new FrameworkPropertyMetadata(typeof(ZoomableCanvasControl)));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            double bottomMost = 0d;
            double rightMost = 0d;

            foreach(object obj in Children)
            {
                FrameworkElement child = obj as FrameworkElement;

                if(child != null)
                {
                    child.Measure(constraint);

                    bottomMost = Math.Max(bottomMost,GetTop(child+child.DesiredSize.Height);
                    rightMost = Math.Max(rightMost,GetLeft(child+child.DesiredSize.Width);
                }
            }
            return new Size(rightMost, bottomMost);
        }
    }
}
WPF Create A Zoomable Canvas Control
24.37.Canvas
24.37.1.Simple Canvas LayoutSimple Canvas Layout
24.37.2.Put Nested Canvas to TabItemPut Nested Canvas to TabItem
24.37.3.Positioning on a CanvasPositioning on a Canvas
24.37.4.Canvas without ViewboxCanvas without Viewbox
24.37.5.Animation target Canvas Left, TopAnimation target Canvas Left, Top
24.37.6.Pixel Not SnappedPixel Not Snapped
24.37.7.Rotate Shape in a CanvasRotate Shape in a Canvas
24.37.8.Canvas dependantCanvas dependant
24.37.9.Canvas.SetTopCanvas.SetTop
24.37.10.Canvas.SetBottomCanvas.SetBottom
24.37.11.Canvas.SetRightCanvas.SetRight
24.37.12.Canvas PreviewMouseDown action and MouseDown actionCanvas PreviewMouseDown action and MouseDown action
24.37.13.Create a Scrollable Canvas ControlCreate a Scrollable Canvas Control
24.37.14.Create a Zoomable Canvas ControlCreate a Zoomable Canvas Control
24.37.15.Set control position for CanvasSet control position for Canvas
24.37.16.Canvas Positioning Properties SampleCanvas Positioning Properties Sample
24.37.17.Get position on a Canvas with Canvas.GetLeftGet position on a Canvas with Canvas.GetLeft
24.37.18.Canvas size changed event
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.