2月 20

クリップボードからデータを取得する

ClipboardクラスのGet~関数群を利用します。
下の例はFormにTextBoxとButtonを貼り付けて
Buttonが押されたらTextBoxにクリップボードのテキストを設定しています。

動作確認をする場合は
メモ帳などのエディタを開いて文字列を入力、クリップボードにコピーしたあとに
Buttonを押してみてください。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // クリップボードからテキストを取得し、TextBoxに設定
            // テキストが格納されていない場合はstring.Empty "" が返ります
            textBox1.Text = Clipboard.GetText();

            // クリップボードにテキストが格納されているとは限らないので
            // データの種別を判断する例はこちら
            IDataObject data = Clipboard.GetDataObject();
            if (data != null)
            {
                string text = (string)data.GetData(DataFormats.Text);
                textBox1.Text = text;
            }
        }
    }
}

2月 20

クリップボードにデータを設定する

ClipboardクラスのSet~関数群を利用します。
下の例はFormにTextBoxとButtonを貼り付けて
Buttonが押されたらTextBoxのテキストをクリップボードに設定しています。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // TextBoxのテキストをクリップボードに設定します。いずれの方法でもOK
            Clipboard.SetText(textBox1.Text);
            Clipboard.SetData(DataFormats.Text, textBox1.Text);
            Clipboard.SetDataObject(textBox1.Text);
        }
    }
}

2月 20

アプリケーションのバージョンを取得する

アプリケーションのバージョン情報を取得する方法を2つ紹介します。
アプリケーションの更新通知などの仕組みに利用できると思います。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;    // これを追加します Assembly
using System.Diagnostics;   // これを追加します FileVersionInfo

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 現在実行中のアセンブリ参照を取得してバージョンを取得する方法
            Assembly asmbl = Assembly.GetExecutingAssembly();
            AssemblyName name = asmbl.GetName();
            MessageBox.Show(name.Version.ToString());

            // ファイル名を指定して、そのファイルのバージョンを取得する方法
            string filename = @"C:\src\FileMany\FileMany\bin\Release\FileMany.exe";
            if (File.Exists(filename))
            {
                FileVersionInfo filever = FileVersionInfo.GetVersionInfo(filename);
                MessageBox.Show(filever.FileVersion);
            }
        }
    }
}

2月 20

二重起動防止

Mutexを使って二重起動を防止します。
具体的には、FormLoad時などにMutexを作成してみて
既に作成されていれば二重起動されたと見なします。

private System.Threading.Mutex mutex;

private void Form1_Load(object sender, System.EventArgs e)
{
    // Mutexを作成(Mutex名はユニークな名前を付けてください)
    mutex = new System.Threading.Mutex( false, "unko" );
    if ( !mutex.WaitOne( 0, false ) )
    {
        MessageBox.Show( "二重起動です!", "INFO" );
        this.Close();
    }
}

private void Form1_Closed(object sender, System.EventArgs e)
{
    // 作成したMutexをクローズ
    mutex.Close();
}
2月 20

他のアプリケーションの終了を待つ

前準備として
ツールボックス → コンポーネント → Process を
フォームにDrag&Dropします。

次にProcessコンポーネントの
EnableRaisingEventsプロパティーをtrueにし、
アプリ終了時のイベントを取得可能にします。

最後にProcessコンポーネントの
Exitedイベントハンドラを作成します。
(イベント一覧よりExitedをダブルクリック)

アプリ終了時にExitedハンドラが呼び出されるので
ここへ必要な処理を記述してください。

private void button1_Click(object sender, System.EventArgs e)
{
    try
    {
        process1.StartInfo.FileName = "notepad.exe";
        process1.Start();
    }
    catch( Win32Exception )
    {
        MessageBox.Show( "実行失敗", "ERROR" );
    }
}

private void process1_Exited(object sender, System.EventArgs e)
{
    MessageBox.Show( "終了したみたい", "INFO" );
}

2月 20

アプリケーションのアイコンを設定する

メインFormのiconプロパティーに.icoファイルを指定すると
システムメニューボックスのアイコンを変更することができますが
さらにアプリケーションのアイコンを変更したい場合は

メニューより

【プロジェクト】→【プロパティー】

でプロジェクトのプロパティーを開き

【アプリケーション】タブの【リソース】グループ内の
【アイコンとマニフェスト】のところにアイコンを指定します。


app_icon.jpg(84075 byte)