トップページに戻る    次のC#のサンプルへ    前のC#のサンプルへ

7-1 ペースト君

C#のサンプル

ペースト君(VB.NET)のC#バージョンです。
オープンソースソフトです。
Visual C# 2008 Express Editionで作成しました。

ペースト君2018

Form1.Designer.cs

namespace Pastekun2021
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナ変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナで生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnClear = new System.Windows.Forms.Button();
            this.btnSQL = new System.Windows.Forms.Button();
            this.btnSakura = new System.Windows.Forms.Button();
            this.btnText = new System.Windows.Forms.Button();
            this.btnJavaScript = new System.Windows.Forms.Button();
            this.btnWork = new System.Windows.Forms.Button();
            this.btnEnd = new System.Windows.Forms.Button();
            this.btnPHP = new System.Windows.Forms.Button();
            this.btnCPP = new System.Windows.Forms.Button();
            this.btnCS = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnClear
            // 
            this.btnClear.BackColor = System.Drawing.Color.Lime;
            this.btnClear.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnClear.Location = new System.Drawing.Point(0, 540);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(200, 180);
            this.btnClear.TabIndex = 5;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = false;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // btnSQL
            // 
            this.btnSQL.BackColor = System.Drawing.Color.Pink;
            this.btnSQL.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnSQL.Location = new System.Drawing.Point(0, 0);
            this.btnSQL.Name = "btnSQL";
            this.btnSQL.Size = new System.Drawing.Size(200, 540);
            this.btnSQL.TabIndex = 0;
            this.btnSQL.Text = "SQL";
            this.btnSQL.UseVisualStyleBackColor = false;
            this.btnSQL.Click += new System.EventHandler(this.btnSQL_Click);
            // 
            // btnSakura
            // 
            this.btnSakura.BackColor = System.Drawing.Color.PaleGreen;
            this.btnSakura.Font = new System.Drawing.Font("MS UI Gothic", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnSakura.Location = new System.Drawing.Point(200, 0);
            this.btnSakura.Name = "btnSakura";
            this.btnSakura.Size = new System.Drawing.Size(200, 360);
            this.btnSakura.TabIndex = 1;
            this.btnSakura.Text = "Sakura";
            this.btnSakura.UseVisualStyleBackColor = false;
            this.btnSakura.Click += new System.EventHandler(this.btnSakura_Click);
            // 
            // btnText
            // 
            this.btnText.BackColor = System.Drawing.Color.Fuchsia;
            this.btnText.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnText.Location = new System.Drawing.Point(200, 360);
            this.btnText.Name = "btnText";
            this.btnText.Size = new System.Drawing.Size(200, 360);
            this.btnText.TabIndex = 6;
            this.btnText.Text = "Text";
            this.btnText.UseVisualStyleBackColor = false;
            this.btnText.Click += new System.EventHandler(this.btnText_Click);
            // 
            // btnJavaScript
            // 
            this.btnJavaScript.BackColor = System.Drawing.Color.LimeGreen;
            this.btnJavaScript.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnJavaScript.Location = new System.Drawing.Point(400, 540);
            this.btnJavaScript.Name = "btnJavaScript";
            this.btnJavaScript.Size = new System.Drawing.Size(400, 180);
            this.btnJavaScript.TabIndex = 8;
            this.btnJavaScript.Text = "JavaScript";
            this.btnJavaScript.UseVisualStyleBackColor = false;
            this.btnJavaScript.Click += new System.EventHandler(this.btnJavaScript_Click);
            // 
            // btnWork
            // 
            this.btnWork.BackColor = System.Drawing.Color.Aqua;
            this.btnWork.Font = new System.Drawing.Font("MS UI Gothic", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnWork.Location = new System.Drawing.Point(400, 360);
            this.btnWork.Name = "btnWork";
            this.btnWork.Size = new System.Drawing.Size(400, 180);
            this.btnWork.TabIndex = 7;
            this.btnWork.Text = "Work";
            this.btnWork.UseVisualStyleBackColor = false;
            this.btnWork.Click += new System.EventHandler(this.btnWork_Click);
            // 
            // btnEnd
            // 
            this.btnEnd.BackColor = System.Drawing.Color.Yellow;
            this.btnEnd.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnEnd.Location = new System.Drawing.Point(800, 360);
            this.btnEnd.Name = "btnEnd";
            this.btnEnd.Size = new System.Drawing.Size(224, 360);
            this.btnEnd.TabIndex = 9;
            this.btnEnd.Text = "終了";
            this.btnEnd.UseVisualStyleBackColor = false;
            this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click);
            // 
            // btnPHP
            // 
            this.btnPHP.BackColor = System.Drawing.Color.Plum;
            this.btnPHP.Font = new System.Drawing.Font("MS UI Gothic", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnPHP.Location = new System.Drawing.Point(400, 180);
            this.btnPHP.Name = "btnPHP";
            this.btnPHP.Size = new System.Drawing.Size(400, 180);
            this.btnPHP.TabIndex = 3;
            this.btnPHP.Text = "PHP";
            this.btnPHP.UseVisualStyleBackColor = false;
            this.btnPHP.Click += new System.EventHandler(this.btnPHP_Click);
            // 
            // btnCPP
            // 
            this.btnCPP.BackColor = System.Drawing.Color.Honeydew;
            this.btnCPP.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnCPP.Location = new System.Drawing.Point(400, 0);
            this.btnCPP.Name = "btnCPP";
            this.btnCPP.Size = new System.Drawing.Size(400, 180);
            this.btnCPP.TabIndex = 2;
            this.btnCPP.Text = "C++";
            this.btnCPP.UseVisualStyleBackColor = false;
            this.btnCPP.Click += new System.EventHandler(this.btnCPP_Click);
            // 
            // btnCS
            // 
            this.btnCS.BackColor = System.Drawing.Color.Lime;
            this.btnCS.Font = new System.Drawing.Font("MS UI Gothic", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.btnCS.Location = new System.Drawing.Point(800, 0);
            this.btnCS.Name = "btnCS";
            this.btnCS.Size = new System.Drawing.Size(224, 360);
            this.btnCS.TabIndex = 4;
            this.btnCS.Text = "C#";
            this.btnCS.UseVisualStyleBackColor = false;
            this.btnCS.Click += new System.EventHandler(this.btnCS_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1020, 720);
            this.Controls.Add(this.btnCS);
            this.Controls.Add(this.btnCPP);
            this.Controls.Add(this.btnPHP);
            this.Controls.Add(this.btnEnd);
            this.Controls.Add(this.btnWork);
            this.Controls.Add(this.btnJavaScript);
            this.Controls.Add(this.btnText);
            this.Controls.Add(this.btnSakura);
            this.Controls.Add(this.btnSQL);
            this.Controls.Add(this.btnClear);
            this.Name = "Form1";
            this.Text = "ペースト君2018";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Shown += new System.EventHandler(this.Form1_Shown);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnSQL;
        private System.Windows.Forms.Button btnSakura;
        private System.Windows.Forms.Button btnText;
        private System.Windows.Forms.Button btnJavaScript;
        private System.Windows.Forms.Button btnWork;
        private System.Windows.Forms.Button btnEnd;
        private System.Windows.Forms.Button btnPHP;
        private System.Windows.Forms.Button btnCPP;
        private System.Windows.Forms.Button btnCS;
    }
}

Form1.cs

using System;
using System.Linq;
using System.Windows.Forms;

namespace Pastekun2021
{
    public partial class Form1 : Form
    {
        private const string PathEmEditor = @"C:\Program Files\EmEditor\emeditor.exe"; //EmEditor
        private const string PathSakura = @"C:\Program Files\sakura\sakura.exe"; //Sakura

        private string[] CommandLineArgs = Environment.GetCommandLineArgs();
        private int Hyoujiritu; //表示率

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized; //最大化
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (CommandLineArgs.Length == 1) Hyoujiritu = 100;
            else Hyoujiritu = int.Parse(CommandLineArgs[1]);

            //表示率が100なら何もしない
            if (Hyoujiritu == 100) return;

            Func<System.Drawing.Size, System.Drawing.Size> wkFunc1 = pTargetSize =>
            {
                int NewWidth = (int)((double)pTargetSize.Width * Hyoujiritu / 100);
                int NewHeight = (int)((double)pTargetSize.Height * Hyoujiritu / 100);
                return new System.Drawing.Size(NewWidth, NewHeight);
            };

            Func<System.Drawing.Point, System.Drawing.Point> wkFunc2 = pTargetPoint =>
            {
                int NewX = (int)((double)pTargetPoint.X * Hyoujiritu / 100);
                int NewY = (int)((double)pTargetPoint.Y * Hyoujiritu / 100);
                return new System.Drawing.Point(NewX, NewY);
            };

            this.Size = wkFunc1(this.Size);

            foreach (var EachBtn in this.Controls.OfType<Button>()) {
                EachBtn.Size = wkFunc1(EachBtn.Size);
                EachBtn.Location = wkFunc2(EachBtn.Location);
            }
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnWork_Click(object sender, EventArgs e)
        {
            var InsProcessStartInfo = new System.Diagnostics.ProcessStartInfo();
            InsProcessStartInfo.FileName = getWorkFolder();
            InsProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            System.Diagnostics.Process.Start(InsProcessStartInfo);

            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject("", true);
        }

        private void btnSQL_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "sql");
        }

        private void MakeTxt(string pExePath, string pKakutyousi)
        {
            string WillOut = "";
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text)) {
                WillOut = iData.GetData(DataFormats.Text).ToString();
            }
            else if (Clipboard.ContainsFileDropList()) {
                var FullPathList = new List<string>();

                // データを取得する(取得できなかった時はnull)
                System.Collections.Specialized.StringCollection files =
                    Clipboard.GetFileDropList();

                // 取得したファイル名を列挙する
                foreach (string EachFullPath in files) {
                    FullPathList.Add(EachFullPath);
                }
                // フルパスの昇順にソートしておく
                FullPathList.Sort();

                if (FullPathList.Count > 0) {
                    WillOut = string.Join(Environment.NewLine, FullPathList.ToArray());
                    WillOut += Environment.NewLine;
                }
            }

            string FileName = DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss秒") + "." + pKakutyousi;
            string OutFilePath = System.IO.Path.Combine(getWorkFolder(), FileName);
            System.IO.File.WriteAllText(OutFilePath, WillOut,System.Text.Encoding.GetEncoding("shift-jis"));

            var InsProcessStartInfo = new System.Diagnostics.ProcessStartInfo();
            InsProcessStartInfo.FileName = pExePath;
            InsProcessStartInfo.Arguments = OutFilePath;
            InsProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            System.Diagnostics.Process.Start(InsProcessStartInfo);

            this.Close();
        }

        //DドライブがなかったらCドライブを使う
        private string getWorkFolder()
        {
            return System.IO.Directory.Exists(@"D:\WorkFile\") ? @"D:\WorkFile\" : @"C:\WorkFile\";
        }

        private void btnSakura_Click(object sender, EventArgs e)
        {
            MakeTxt(PathSakura, "txt");
        }

        private void btnText_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "txt");
        }

        private void btnPHP_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "php");
        }

        private void btnCPP_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "cpp");
        }

        private void btnCS_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "cs");
        }

        private void btnJavaScript_Click(object sender, EventArgs e)
        {
            MakeTxt(PathEmEditor, "js");
        }
    }
}


WPF版もあります。 ペースト君2020WPF

PasteKun2020WPF.xaml

<Window x:Class="PasteKun2020WPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ペースト君2020WPF" Height="Auto" Width="Auto" WindowState="Maximized">
    <Grid Height="Auto" Name="grid1" Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="3" Name="btnSQL"   Background="White" FontSize="72" Click="btnSQL_Click">SQL</Button>
        <Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="3" Grid.RowSpan="1" Name="btnClear" Background="White" FontSize="72" Click="btnClear_Click">Clear</Button>

        <Button Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="2" Name="btnHTML"  Background="White" FontSize="72" Click="btnHTML_Click">HTML</Button>
        <Button Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="2" Grid.RowSpan="2" Name="btnText"  Background="White" FontSize="72" Click="btnText_Click">Text</Button>

        <Button Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="1" Name="btnEmEditor"  Background="White" FontSize="72" Click="BtnEmEditor_Click">EmEditor</Button>
        <Button Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="1" Name="btnPHP"    Background="White" FontSize="72" Click="btnPHP_Click">PHP</Button>

        <Button Grid.Column="2" Grid.ColumnSpan="2"  Grid.Row="2" Grid.RowSpan="1" Name="btnWork"  Background="White" FontSize="72" Click="btnWork_Click">Work</Button>
        <Button Grid.Column="2" Grid.ColumnSpan="2"  Grid.Row="3" Grid.RowSpan="1" Name="btnJS"    Background="White" FontSize="72" Click="btnJS_Click">JavaScript</Button>

        <Button Grid.Column="4" Grid.ColumnSpan="1"  Grid.Row="0" Grid.RowSpan="2" Name="btnCS"   Background="White" FontSize="72" Click="btnCS_Click">C#</Button>
        <Button Grid.Column="4" Grid.ColumnSpan="1"  Grid.Row="2" Grid.RowSpan="2" Name="btnEnd"   Background="White" FontSize="72" Click="btnEnd_Click">終了</Button>
    </Grid>
</Window>

PasteKun2020WPF.xaml.cs

using System;
using System.Windows;

namespace PasteKun2020WPF
{
    /// <summary>
    /// Window1.xaml の相互作用ロジック
    /// </summary>
    public partial class Window1 : Window
    {
        private const string PathSakura = @"C:\Program Files\sakura\sakura.exe"; //Sakura
        private const string PathEmEditor = @"C:\Program Files\EmEditor\EmEditor.exe"; //EmEditor

        public Window1()
        {
            InitializeComponent();
        }

        private void MakeTxt(string pExePath, string pExtensino)
        {
            string WillOut = "";
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text)) {
                WillOut = iData.GetData(DataFormats.Text).ToString();
            }
            else if (Clipboard.ContainsFileDropList()) {
                var FullPathList = new List<string>();

                // データを取得する(取得できなかった時はnull)
                System.Collections.Specialized.StringCollection files =
                    Clipboard.GetFileDropList();

                // 取得したファイル名を列挙する
                foreach (string EachFullPath in files) {
                    FullPathList.Add(EachFullPath);
                }
                // フルパスの昇順にソートしておく
                FullPathList.Sort();

                if (FullPathList.Count > 0) {
                    WillOut = string.Join(Environment.NewLine, FullPathList.ToArray());
                    WillOut += Environment.NewLine;
                }
            }
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
                WillOut = iData.GetData(DataFormats.Text).ToString();

            string FileName = DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss秒") + "." + pExtensino;
            string OutFilePath = System.IO.Path.Combine(getWorkFolder(), FileName);

            System.Text.Encoding enc = new System.Text.UTF8Encoding(false);

            System.IO.File.WriteAllText(OutFilePath, WillOut, enc);

            var InsProcessStartInfo = new System.Diagnostics.ProcessStartInfo();
            InsProcessStartInfo.FileName = pExePath;
            InsProcessStartInfo.Arguments = OutFilePath;
            InsProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            System.Diagnostics.Process.Start(InsProcessStartInfo);

            this.Close();
        }

        //DドライブがなかったらCドライブを使う
        private string getWorkFolder()
        {
            return System.IO.Directory.Exists(@"D:\WorkFile\") ? @"D:\WorkFile\" : @"C:\WorkFile\";
        }

        private void btnSQL_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "sql");
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetDataObject("", true);
        }

        private void btnPHP_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "php");
        }

        private void btnHTML_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "html");
        }

        private void btnText_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "txt");
        }

        private void btnWork_Click(object sender, RoutedEventArgs e)
        {
            var InsProcessStartInfo = new System.Diagnostics.ProcessStartInfo();
            InsProcessStartInfo.FileName = getWorkFolder();
            InsProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            System.Diagnostics.Process.Start(InsProcessStartInfo);

            this.Close();
        }

        private void btnJS_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "js");
        }

        private void btnCS_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathSakura, "cs");
        }

        private void btnEnd_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnEmEditor_Click(object sender, RoutedEventArgs e)
        {
            MakeTxt(PathEmEditor, "txt");
        }
    }
}