// マウスの追跡: // 多くのアプリケーションで、描画は OnPaint または Paint イベントを待って行われるが、 // それらを介さず、マウスの移動などをきっかけとして描画することもできるという実例。 using System.Windows.Forms; using System.Drawing; public class MyForm : Form { [System.STAThread] // Main の直前にこれがないと日本語入力とかできない! static void Main() { // Main: プログラムが始まる特別なメソッド名 Application.Run(new MyForm()); } // =============================================================================================== public MyForm() { this.Text = "マウスの刻々の追跡と言うより、マウスの移動がイベントとなって、再描画が行われる"; // タイトルバーに入れる文字を指定 Rectangle scr = Screen.GetBounds(this); // スクリーン(ディスプレイ)のサイズを取得 this.Size = new Size(scr.Width, scr.Height); // サイズ的に全画面ということ this.CenterToScreen(); // 場所は中央に this.FormBorderStyle = FormBorderStyle.FixedSingle; Font myfont=new Font("MS ゴシック", 18); // TextBox tx = new TextBox(); tx.SetBounds(60, 30, 80, 20); tx.Font=myfont; this.Controls.Add(tx); TextBox ty = new TextBox(); ty.SetBounds(10, 80, 80, 20); ty.Font=myfont; this.Controls.Add(ty); Brush backbrush=new SolidBrush(Color.LightGreen), 黒いブラシ = new SolidBrush(Color.FromArgb(255, 0, 0, 0)); Pen blackpen = new Pen(Color.Black, 1); Graphics g=this.CreateGraphics(); // === あまり使われないこの関数がカギです!!! this.MouseMove += (o, e) => { Point mp = MousePosition; Point plglob = PointToScreen(new Point(0, 0)); // クライエント領域の原点をディスプレイ上の位置として読む int i = mp.X -plglob.X, j = mp.Y -plglob.Y; tx.Text = i.ToString(); ty.Text = j.ToString(); g.FillRectangle(backbrush, 0, 0, this.Width, this.Height); g.DrawString("→", myfont, 黒いブラシ, 60, 2); g.DrawString("↓", myfont, 黒いブラシ, 10, 52); g.DrawLine(blackpen, i, 0, i, 1100); g.DrawLine(blackpen, 0, j, 2000, j); }; } // =============================================================================================== } // ###################################################################################################