LoginSignup
1
0

More than 5 years have passed since last update.

Visual Studio | WPF > Window > 拡大して表示する (各種コントロールも拡大)

Last updated at Posted at 2017-12-25
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community

c++ Builder > form > フォームの大きさをコードで拡大する > this->ScaleBy(120,100);
の処理をWPFではどのように実装するか。

処理

以下とした

  • XAMLにてSizeToControl="WidthAndHeight"を設定
  • XAMLにてViewboxを使用
    • 中にGrid(Height, Width固定)を使用
      • 中にコントロールを配置する
  • code behindにおいてViewboxの(Height, Width)を変更する

参考

code

MainWindow.xaml
<Window x:Class="_171225_t1400_scaleBy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171225_t1400_scaleBy"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        SizeToContent="WidthAndHeight">
    <!-- SizeToContent を指定した-->
    <Viewbox Name="VWB_outer"
            StretchDirection="Both" Stretch="Uniform"
                Height="320" Width="515">
        <Grid Height="320" Width="515">
            <Button Name="Button1"
            Content="Button" Height="30" Click="Button_Click"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="28,36,0,0" VerticalAlignment="Top"/>
            <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="111,47,0,0" VerticalAlignment="Top"/>
            <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="312,37,0,0" VerticalAlignment="Top"/>
            <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="312,69,0,0" VerticalAlignment="Top"/>
        </Grid>
    </Viewbox>
</Window>
MainWindow.xaml.cs
using System.Windows;

namespace _171225_t1400_scaleBy
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        static bool s_bfEnlarged = false; // 拡大済かどうか

        public MainWindow()
        {
            InitializeComponent();
        }

        static readonly double kScaleFactor = 1.5;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (s_bfEnlarged)
            {
                VWB_inner.Height = VWB_inner.Height / kScaleFactor;
                VWB_inner.Width = VWB_inner.Width / kScaleFactor;
                s_bfEnlarged = false;
            } else {
                VWB_inner.Height = VWB_inner.Height * kScaleFactor;
                VWB_inner.Width = VWB_inner.Width * kScaleFactor;
                s_bfEnlarged = true;
            }
        }
    }
}

実行例

拡大なし
qiita.png

拡大あり
qiita.png

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0