6月 13

ホスト名からIPアドレスを取得する

ホスト名からIPアドレスを取得するには
System.Net.DnsクラスのGetHostEntry関数を使います。

    // IPを調べたいホスト名
    string hostname = "www.yahoo.co.jp";

    // IPHostEntry取得
    System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(hostname);

    // 最初のアドレスを取得
    System.Net.IPAddress ip = hostEntry.AddressList[0];

    // xxx.xxx.xxx.xxx形式の文字列を取得
    string IPaddress = ip.ToString();
6月 13

IPアドレスを取得する

PCのIPアドレスを取得する方方です。
まずホスト名を取得し、それを元にエントリーを列挙しています。

IPアドレスは1つとは限らないので注意が必要です。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;   // これが必要

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

        private void button1_Click(object sender, EventArgs e)
        {
            // ホスト名を取得
            string hostname = Dns.GetHostName();

            // ホスト名からエントリー取得
            IPHostEntry ipentry = Dns.GetHostEntry(hostname);

            // IPアドレスは2つ以上のケースもあるのですべて列挙(有線+無線など)
            string msg = string.Empty;
            foreach (IPAddress ip in ipentry.AddressList)
            {
                msg += ip.ToString() + "\r\n";
            }

            MessageBox.Show(msg);
        }
    }
}
6月 13

ホスト名を取得する

PCのホスト名を取得するには
System.Net.DnsクラスのGetHostName関数を使います。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;   // これが必要

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

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Dns.GetHostName());
        }
    }
}
6月 13

マウスカーソルの形状を変更する(ユーザー定義)

マウスカーソル形状を
ユーザー定義のマウスカーソルファイル(.cur)
に変更する場合の例です。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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)
        {
            // デフォルトのカーソルに戻す
            this.Cursor = Cursors.Default;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // ユーザー定義のカーソルに変更
            this.Cursor = @"c:\temp\custom.cur";
        }
    }
}
6月 13

マウスカーソルの形状を変更する

Cursorプロパティーの値を変更します。
Form上でカーソルを変更したい場合は
FormのCursorプロパティーの値を変更します。

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.AppStarting;  // 矢印+砂時計

            // ここで何か重い処理など

            this.Cursor = Cursors.Arrow;        // 通常の矢印に戻す
        }
    }
6月 13

メッセージボックスを表示する

単純なメッセージボックスを表示する例です。
お知らせを表示するだけであればFormを作る必要はありません。

メッセージボックスで押されたボタンの種類の判別は戻り値を取得します。

msg

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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)
        {
            // 単純なメッセージを表示
            MessageBox.Show("メッセージ");

            // タイトル名とメッセージを表示
            MessageBox.Show("メッセージ", "タイトル");

            // OKボタン、キャンセルボタンを表示
            MessageBox.Show("メッセージ", "タイトル", MessageBoxButtons.OKCancel);

            // アイコンを指定
            MessageBox.Show(
            	"メッセージ", 
            	"タイトル", 
            	MessageBoxButtons.OKCancel, 
            	MessageBoxIcon.Information);

            // デフォルトボタンを指定(この場合は2番目のCancelがデフォルト)
            // メッセージボックスで押されたボタンの種類の判別は戻り値を取得します。
            DialogResult ret = MessageBox.Show(
            	"メッセージ", 
            	"タイトル", 
            	MessageBoxButtons.OKCancel, 
            	MessageBoxIcon.Information, 
            	MessageBoxDefaultButton.Button2);

            if (ret == DialogResult.OK)
                MessageBox.Show("OKが押されました");
        }
    }
}
6月 13

MDI子ウィンドウをタブ表示にして切り替える

MDI子ウィンドウをタブ表示にする方法です。
VisualStudioのようにウィンドウをタブ表示にしたいことがあると思いますが
ちょっと工夫して擬似的にこれを実現する方法を書いてみます。

完成のサンプルはこんな感じです

mdi_tab

作り方はTabコントロールを貼り付けているだけなのですが、コツは

1.MDI子ウィンドウが1つ以上ある時だけTabコントロールを表示する
2.Tabコントロールのタブ部分のみ表示する
3.MDI子ウィンドウをTabコントロール上に配置するのではない

です。

あとは
・TabコントロールのSelectedIndexChangedイベントハンドラでActiveな子ウィンドウを切り替え
・親FormのMdiChildActivateイベントハンドラでTabコントロールの選択状態を変更

などの処理を自分で書きます。

これを実現する有料コンポーネントなども見かけましたが
手作りで近いものをこの方法で作れると思います。

6月 13

アクティブなMDI子ウィンドウを取得する

現在アクティブなMDI子ウィンドウを取得するには
ActiveMdiChildプロパティーを参照します。

例ではメニューから【閉じる】が選択されると
現在アクティブなMDI子ウィンドウを閉じます。

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
    {
        System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // MDI親ウィンドウにする
            this.IsMdiContainer = true;
        }

        private void 開くToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // MDI子ウィンドウを開く
            Form2 form = new Form2();
            form.MdiParent = this;  // 親を指定
            form.Text = "新しいForm:" + this.MdiChildren.Length.ToString();
            form.Show();
        }

        private void 閉じるToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.ActiveMdiChild != null)
            {
                this.ActiveMdiChild.Close();
            }
        }
    }
}
6月 13

MDI子ウィンドウの一覧を表示する

MDI子ウィンドウの一覧を表示する例です。

mdi_child1

このように
あるメニューを選ぶと、MDI子ウィンドウの一覧を表示させたい場合
MenuStripコントロールに仕組みが用意されています。

MDI親ウィンドウにMenuStripを配置し
【ウィンドウ】のようなメニューを追加しておきます。

後はMenuStripのMdiWindowListItemプロパティーに
子ウィンドウの一覧を表示させたいメニューを登録するだけです。

mdi_child2

6月 13

MDIウィンドウを作る

MDIウィンドウの例です。
ウィンドウの中に子ウィンドウが入れ子になっています。

MDIウィンドウを作るには
親にしたいFormのIsMdiContainerプロパティーにtrueを設定するだけです。

子ウィンドウ側は
MdiParentプロパティーに親ウィンドウのインスタンスを設定します。
後はShow()するだけです。

例ではForm1を親ウィンドウ、Form2を子ウィンドウとし、
Form1のメニューより【開く】を選択すると子ウィンドウが表示されます。

mdi

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
    {
        System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // MDI親ウィンドウにする
            this.IsMdiContainer = true;
        }

        private void 開くToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // MDI子ウィンドウを開く
            Form2 form = new Form2();
            form.MdiParent = this;  // 親を指定
            form.Text = "新しいForm:" + this.MdiChildren.Length.ToString();
            form.Show();
        }
    }
}