WPFの实现word的缩放效果

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPFの实现word的缩放效果相关的知识,希望对你有一定的参考价值。

原文:WPFの实现word的缩放效果

ms-word做出的效果令人十分欣喜,那么如何用wpf达到这个效果,下面我们来进行讨论。

界面上我用一个WrapPanel作为父级控件,动态添加InkCanvas作为子控件

 

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Name="disRow"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <Slider x:Name="slider" Grid.Row="1" HorizontalAlignment="Right" Width="200"
                Margin="10,0,100,0" VerticalAlignment="Top" TickFrequency="0.2" 
                TickPlacement="BottomRight" Cursor="Hand" IsSnapToTickEnabled="True" 
                Value="0.6" Maximum="1" LargeChange="0.1" Template="{StaticResource tmp}" ValueChanged="slider_ValueChanged_1"/>
        
        <TextBlock x:Name="textBlock" Grid.Row="1" Width="60"
                   HorizontalAlignment="Right" Margin="0,0,10,0" 
                   FontFamily="宋体" FontSize="15" FontWeight="Light" 
                   TextWrapping="Wrap"  VerticalAlignment="Top">
            <TextBlock.Text>
                <Binding Path="Value" ElementName = "slider" StringFormat="{}{0:P1}"/>
            </TextBlock.Text>
        </TextBlock>
        
        <ScrollViewer x:Name="scr" Margin="0" VerticalScrollBarVisibility="Auto">
            <WrapPanel Name="parent" Grid.Row="0" Width="{Binding Path=Width,ElementName=disRow}" 
                Height="{Binding Path=Height,ElementName=disRow}"
            HorizontalAlignment="Center"  Margin="0,0,0,0" VerticalAlignment="Top"/>
        </ScrollViewer>
    </Grid>

后台添加:

  double SH;
        double SW;
        const int A4_H = 297;
        const int A4_W = 210;
        static double rate;

        public MainWindow()
        {
            InitializeComponent();
            //SH = SystemParameters.PrimaryScreenHeight; //实际宽高
            //SW = SystemParameters.PrimaryScreenWidth;

            SW = SystemParameters.WorkArea.Width;      //工作区
            SH = SystemParameters.WorkArea.Height;

            rate = SH / A4_H;
        }

        private void test_Loaded(object sender, RoutedEventArgs e)
        {
            parent.Width = A4_W * rate;
            for (int i=0; i<60; i++)
            {
                InkCanvas temp = new InkCanvas();
                temp.Width = A4_W*rate;
                temp.Height = A4_H * rate;

                temp.Background= new SolidColorBrush(Color.FromArgb(0xFF, 0xF1, 0xE2, 0x9E));
                temp.Margin = new Thickness(5,10,0,10);
                parent.Children.Add(temp);
            }
        }

ValueChange的代码:

  private void slider_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.NewValue == e.OldValue||(e.OldValue==0&&e.NewValue!=0.2))
            {
                return;
            }

            switch (e.NewValue.ToString())
            {
                case "0":
                    parent.Width = A4_W * rate * 2 + 40;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate / 4;
                        c.Height = A4_H * rate / 4;
                    }
                    break;
                case "0.2":
                    parent.Width = A4_W * rate * 2 + 40;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate/2;
                        c.Height = A4_H * rate / 2;
                    }
                    break;
                case "0.3":
                    break;
                case "0.4":
                    parent.Width = A4_W * rate * 2+20;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate;
                        c.Height = A4_H * rate;
                    }
                    break;
                case "0.5":
                    break;
                case "0.6":
                    parent.Width = A4_W * rate;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate;
                        c.Height = A4_H * rate;
                    }
                    break;
                case "0.7":
                    break;
                case "0.8":
                    parent.Width = A4_W * rate * 1.5;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width= A4_W * rate * 1.5;
                        c.Height = A4_H * rate*1.5;
                    }
                    break;
                case "0.9":
                    parent.Width = A4_W * rate * 2;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate * 2;
                        c.Height = A4_H * rate*2;
                    }
                    break;
                case "1":
                    parent.Width = A4_W * rate * 2.5;
                    foreach (InkCanvas c in parent.Children)
                    {
                        c.Width = A4_W * rate * 2.5;
                        c.Height = A4_H * rate*2.5;
                    }
                    break;

            }
        }

实现的效果:

技术分享图片

以上是关于WPFの实现word的缩放效果的主要内容,如果未能解决你的问题,请参考以下文章

WPF窗体の投影效果

WPF设计の自定义窗体

WPF窗体の投影效果

WPF单例启动及利用单例制作类似WORD打开文档选项卡效果

在WPF里面实现以鼠标位置为中心缩放移动图片

WPF设计の不规则窗体