using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.Diagnostics; namespace WindowsApplication1 { /// /// Form1 の概要の説明です。 /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Button button1; private System.ComponentModel.IContainer components; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } /// /// 使用されているリソースに後処理を実行します。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows フォーム デザイナで生成されたコード /// /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.label1 = new System.Windows.Forms.Label(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label1.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label1.Location = new System.Drawing.Point(216, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(240, 64); this.label1.TabIndex = 0; this.label1.Text = "label1"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // timer1 // this.timer1.Enabled = true; this.timer1.Interval = 10; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // label2 // this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label2.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label2.Location = new System.Drawing.Point(216, 88); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(240, 64); this.label2.TabIndex = 1; this.label2.Text = "label2"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label3 // this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label3.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label3.Location = new System.Drawing.Point(216, 160); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(240, 64); this.label3.TabIndex = 2; this.label3.Text = "label3"; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label4 // this.label4.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label4.Location = new System.Drawing.Point(16, 16); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(200, 64); this.label4.TabIndex = 3; this.label4.Text = "スクリーン座標:"; this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label5 // this.label5.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label5.Location = new System.Drawing.Point(16, 88); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(200, 64); this.label5.TabIndex = 4; this.label5.Text = "クライアント座標(フォーム):"; this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label6 // this.label6.Font = new System.Drawing.Font("MS UI Gothic", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.label6.Location = new System.Drawing.Point(16, 160); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(200, 64); this.label6.TabIndex = 5; this.label6.Text = "クライアント座標 (ボタン):"; this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // button1 // this.button1.Font = new System.Drawing.Font("MS UI Gothic", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128))); this.button1.Location = new System.Drawing.Point(16, 240); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(440, 104); this.button1.TabIndex = 6; this.button1.Text = "ボタン"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(474, 360); this.Controls.Add(this.button1); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// アプリケーションのメイン エントリ ポイントです。 /// [STAThread] static void Main() { Application.Run(new Form1()); } private void timer1_Tick(object sender, System.EventArgs e) { // マウス・カーソルのスクリーン座標での位置を取得 label1.Text = Cursor.Position.ToString(); // フォームを基準に(スクリーン座標を)クライアント座標に変換 Point formClientCurPos = this.PointToClient(Cursor.Position); label2.Text = formClientCurPos.ToString(); // ボタンを基準に(スクリーン座標を)クライアント座標に変換 Point btnClientCurPos = button1.PointToClient(Cursor.Position); label3.Text = btnClientCurPos.ToString(); } private void button1_Click(object sender, System.EventArgs e) { // ボタンのクライアント領域の原点を(クライアント座標で)取得 Point btnClientLocation = button1.ClientRectangle.Location; // (ボタンを基準に)クライアント座標からスクリーン座標に変換 // ※注意:ボタンのクライアント座標をスクリーン座標に // 変換するには、必ず<ボタンの>PointToScreenメソッドを使うこと Point btnScreenLocation = button1.PointToScreen(btnClientLocation); MessageBox.Show("ボタンのクライアント領域の原点は、\n\n" + "クライアント座標では" + btnClientLocation.ToString() + "になり、\n\n" + "スクリーン座標では" + btnScreenLocation.ToString() + "になる。"); // カーソルの位置をボタンの原点に移動 Cursor.Position = btnScreenLocation; } } }