Skip to content

Instantly share code, notes, and snippets.

@LivingGhost
Created July 31, 2022 14:44
Show Gist options
  • Save LivingGhost/3339f265df34d6e23de43b44cace3cde to your computer and use it in GitHub Desktop.
Save LivingGhost/3339f265df34d6e23de43b44cace3cde to your computer and use it in GitHub Desktop.
batファイルのみでWinFormsを構築してみるテスト
@(echo '> NUL
echo off)
setlocal enableextensions
echo %~n0%~x0
if "%~1" neq "RESTARTED" ( goto RESTART )
set "THIS_PATH=%~f0"
set "PARAM_1=%~2"
PowerShell.exe -Command "iex -Command ((gc \"%THIS_PATH:`=``%\") -join \"`n\")"
exit /b %errorlevel%
:RESTART
powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process \"%~f0\" -ArgumentList \"RESTARTED %~1\" -WindowStyle Hidden"
exit
') | sv -Name TempVar
function WinForms
{
$code = @"
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
public class MyClass {
[STAThread]
public static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private class Form1: Form {
private TextBox textBox1;
private Button button1;
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.textBox1 = new TextBox();
this.button1 = new Button();
this.textBox1.Location = new Point(47, 49);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(191, 19);
this.textBox1.TabIndex = 0;
this.button1.Location = new Point(274, 47);
this.button1.Name = "button1";
this.button1.Size = new Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "実行";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new EventHandler(this.button1_Click);
this.ClientSize = new Size(400, 150);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Text = "フォームのタイトル";
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button1_Click(object sender, EventArgs e) {
Console.WriteLine("button1_Click");
// テンポラリフォルダのパスを取得
string path = Environment.GetEnvironmentVariable("temp");
// 書き込むファイルのフルパスを編集する
string writePath = string.Format(@"{0}\_input_result", path);
using(StreamWriter sw = new StreamWriter(writePath, false)) {
// 書き込み
sw.Write(textBox1.Text);
// ファイルを閉じます
sw.Close();
}
Application.Exit();
}
}
}
"@
Add-Type -Language CSharp -TypeDefinition $code -ReferencedAssemblies (
"System.Windows.Forms",
"System.Drawing"
)
[MyClass]::Main()
}
WinForms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment