環境
Windows 11 Pro (64bit)
dev.bat
@echo off
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
prompt $e[33m$p$g$e[m
cmd
wpf.rsp
# csc @wpf.rsp foo.cs
/r:System.Xaml.dll
/r:WPF\PresentationCore.dll
/r:WPF\PresentationFramework.dll
/r:WPF\WindowsBase.dll
計数器
元ネタはこちらです。圧倒的感謝……!
XAMLを使わない
fig1.cs
// csc /t:winexe @wpf.rsp fig1.cs
using System;
using System.Windows;
using System.Windows.Controls;
class MyWindow : Window {
public MyWindow() {
var panel = new DockPanel();
var label = new Label();
var button = new Button();
label.Content = 0;
button.Content = "Click!";
button.Click += (sender, e) => {
label.Content = (int)label.Content + 1;
};
DockPanel.SetDock(button, Dock.Bottom);
panel.Children.Add(button);
panel.Children.Add(label);
Title = "test";
Width = 240;
Height = 100;
Content = panel;
}
}
class Program {
[STAThread]
static void Main() {
var wnd = new MyWindow();
wnd.ShowDialog();
}
}
XAMLを使う
fig2.cs
// csc /t:winexe @wpf.rsp fig2.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
public class MyWindow : Window {
Label label;
void OnLoad(object sender, RoutedEventArgs e) {
label = (Label)FindName("label");
label.Content = 0;
}
void button_Click(object sender, RoutedEventArgs e) {
label.Content = (int)label.Content + 1;
}
}
class Program {
[STAThread]
static void Main() {
var xaml = @"
<foo:MyWindow
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:foo='clr-namespace:;assembly=fig2'
Width='240' Height='100' Title='test' Loaded='OnLoad'>
<DockPanel>
<Button DockPanel.Dock='Bottom'
Click='button_Click'>Click!</Button>
<Label Name='label' />
</DockPanel>
</foo:MyWindow>
";
try {
var wnd = (MyWindow)XamlReader.Parse(xaml);
wnd.ShowDialog();
} catch (Exception e) {
MessageBox.Show(e.ToString());
}
}
}
時計
XAMLを使わない
fig3.cs
// csc /t:winexe @wpf.rsp fig3.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
class MyWindow : Window {
public MyWindow() {
var clock = new TextBlock();
clock.FontSize = 30;
var timer = new DispatcherTimer();
timer.Tick += (sender, e) => {
var dt = DateTime.Now;
clock.Text = dt.ToString();
};
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
Title = "Clock";
Width = 320;
Height = 100;
Content = clock;
}
}
class Program {
[STAThread]
static void Main() {
var wnd = new MyWindow();
wnd.ShowDialog();
}
}
XAMLを使う
fig4.cs
// csc /t:winexe @wpf.rsp fig4.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Threading;
public class MyWindow : Window {
TextBlock clock;
DispatcherTimer timer;
void OnLoad(object sender, RoutedEventArgs e) {
clock = (TextBlock)FindName("clock");
clock.FontSize = 30;
timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e) {
var dt = DateTime.Now;
clock.Text = dt.ToString();
}
}
class Program {
[STAThread]
static void Main() {
var xaml = @"
<foo:MyWindow
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:foo='clr-namespace:;assembly=fig4'
Width='320' Height='100' Title='Clock' Loaded='OnLoad'>
<TextBlock Name='clock' />
</foo:MyWindow>
";
try {
var wnd = (MyWindow)XamlReader.Parse(xaml);
wnd.ShowDialog();
} catch (Exception e) {
MessageBox.Show(e.ToString());
}
}
}
PowerShell
fig5.ps1
# powershell -ExecutionPolicy RemoteSigned ./fig5.ps1
Add-Type -AssemblyName PresentationFramework
$xaml = "
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Title='Clock' SizeToContent='WidthAndHeight'>
<TextBlock Name='clock' Width='300' Height='50'
FontSize='30' />
</Window>"
$wnd = [Windows.Markup.XamlReader]::Parse($xaml)
$clock = $wnd.FindName("clock")
$timer = New-Object Windows.Threading.DispatcherTimer
$timer.add_Tick({
$clock.Text = Get-Date
})
$timer.Interval = New-TimeSpan -Seconds 1
$timer.Start()
[void] $wnd.ShowDialog()
ストップウォッチ
fig6.cs
// csc /t:winexe @wpf.rsp fig6.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
class MyWindow : Window {
TextBlock clock;
TextBlock text;
DispatcherTimer timer;
DateTime start;
int mode = 0;
public MyWindow() {
var panel = new StackPanel();
panel.HorizontalAlignment = HorizontalAlignment.Center;
panel.VerticalAlignment = VerticalAlignment.Center;
clock = new TextBlock();
text = new TextBlock();
text.Background = Brushes.LightGray;
text.MouseDown += text_MouseDown;
panel.Children.Add(clock);
panel.Children.Add(text);
//
var ff = new FontFamily("Courier New");
FontFamily = ff;
FontSize = 30;
Title = "test";
Width = 220;
Height = 120;
Content = panel;
ResizeMode = ResizeMode.CanMinimize;
//
timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e) {
var now = DateTime.Now;
clock.Text = now.ToString("HH:mm:ss");
if (mode == 1) {
var ts = now - start;
text.Text = ts.ToString(@"hh\:mm\:ss");
}
}
void text_MouseDown(object sender, MouseButtonEventArgs e) {
if (e.ChangedButton == MouseButton.Left && mode == 0) {
mode = 1;
start = DateTime.Now;
}
if (e.ChangedButton == MouseButton.Right && mode == 1) {
mode = 0;
text.Text = "";
}
}
}
class Program {
[STAThread]
static void Main() {
var wnd = new MyWindow();
wnd.ShowDialog();
}
}