XAMLを使わないWPF入門32「Fontサイズ・色・装飾・ファミリー」

コンピュータ

FontSize、Foreground、TextDecorations、FontFamilyの各プロパティを試してみます。

image

ファイル名:NoXAML32.csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

ファイル名:App.cs


using System;
using System.Windows;

namespace NoXAML32;

public class App : Application
{
    [STAThread]
    public static void Main(string[] args)
    {
        var app = new App();
        app.Run();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var win = new MainWindow();
        win.Show();
    }
}

ファイル名:AssemblyInfo.cs


using System.Windows;

[assembly:ThemeInfo(
    ResourceDictionaryLocation.None,            //where theme specific resource dictionaries are located
                                                //(used if a resource is not found in the page,
                                                // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly   //where the generic resource dictionary is located
                                                //(used if a resource is not found in the page,
                                                // app, or any theme specific resource dictionaries)
)]

ファイル名:MainWindow.cs


using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NoXAML32;

public class MainWindow : Window
{
    public MainWindow()
    {
        Title = "Fontサイズ・色・装飾・ファミリー (NoXAML)";
        Width = 600; Height = 380;

        var sp = new StackPanel { Margin = new Thickness(20), Orientation = Orientation.Vertical };

        // 1) FontSize(単位はDIP、1/96インチ)
        var tb1 = new TextBlock
        {
            Text = "FontSize=24(大きめ)",
            FontSize = 24
        };

        // 2) Foreground(色):Brushes / SolidColorBrush / ARGB など
        var tb2 = new TextBlock
        {
            Text = "Foreground=DeepSkyBlue",
            FontSize = 18,
            Foreground = Brushes.DeepSkyBlue
        };
        // Hex指定(#AARRGGBB)
        var tb2b = new TextBlock
        {
            Text = "Foreground=#CCFF3366(不透明度80%)",
            FontSize = 18,
            Foreground = new SolidColorBrush(Color.FromArgb(0xCC, 0xFF, 0x33, 0x66))
        };

        // 3) FontFamily(フォント名/複数指定でフォールバック)
        var tb3 = new TextBlock
        {
            Text = "FontFamily=\"Yu Gothic UI, Meiryo, Segoe UI Emoji\"",
            FontSize = 18,
            FontFamily = new FontFamily("Yu Gothic UI, Meiryo, Segoe UI Emoji")
        };

        // 4) TextDecorations(下線・取り消し線など)※TextBlock系のみ
        var tb4 = new TextBlock
        {
            Text = "下線(Underline)",
            FontSize = 18
        };
        tb4.TextDecorations = TextDecorations.Underline;

        var tb5 = new TextBlock
        {
            Text = "取り消し線(Strikethrough)",
            FontSize = 18
        };
        tb5.TextDecorations = TextDecorations.Strikethrough;

        // 追加でよく使うやつ:太字・斜体・幅
        var tb6 = new TextBlock
        {
            Text = "Bold / Italic / Stretch",
            FontSize = 18,
            FontWeight = FontWeights.Bold,
            FontStyle = FontStyles.Italic,
            FontStretch = FontStretches.Condensed
        };

        // Button/Labelなども同様(ただしTextDecorationsはTextBlock専用)
        var btn = new Button
        {
            Content = "ボタンの文字もFontSize/Foreground/FontFamilyで変更可",
            FontSize = 16,
            Foreground = Brushes.DarkGreen,
            FontFamily = new FontFamily("Meiryo UI"),
            Padding = new Thickness(10)
        };

        sp.Children.Add(tb1);
        sp.Children.Add(tb2);
        sp.Children.Add(tb2b);
        sp.Children.Add(tb3);
        sp.Children.Add(tb4);
        sp.Children.Add(tb5);
        sp.Children.Add(tb6);
        sp.Children.Add(btn);

        Content = sp;
    }
}

コメント