//================================================================================================= // 非クライアント領域でのクリック感知を考慮した場合のプログラム // // if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left) // // などの部分で正であるならばクリックしている状態と判断される // // 加えて、非クライアント領域でのクリックを感知する場合、WndProcメソッドをオーバーライドする // そして、直接非クライアント領域でのクリックされた場合の処理を書く必要がある // // 2013/03/28/否健康食品オワタ //================================================================================================= using System; using System.Windows.Forms; using System.Drawing; class Form01 : Form { //< クリックの状態によって文字が変化するラベル private Label label1; ///
/// 常に処理されるメソッド ///
public void updata() { //< クリック感知系の処理 //< 左クリックを感知したら処理 if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left) { label1.Text = "左クリックされました"; } //< 右クリックを感知したら処理 else if ((Control.MouseButtons & MouseButtons.Right) == MouseButtons.Right) { label1.Text = "右クリックされました"; } //< 中心ボタンクリックを感知したら処理 else if ((Control.MouseButtons & MouseButtons.Middle) == MouseButtons.Middle) { label1.Text = "中心クリックされました"; } } ///
/// WndProcメソッドのオーバーライド ///
///
protected override void WndProc(ref Message m) { const int WM_NCLBUTTONDOWN = 0x00A1; //< 非クライアント領域でマウスの左クリック const int WM_NCRBUTTONDOWN = 0x00A4; //< 非クライアント領域でマウスの右クリック switch (m.Msg) { //< 非クライアント領域(フォームの枠)を左クリックすると処理される case WM_NCLBUTTONDOWN: label1.Text = "非クライアント領域で左クリック"; break; //< 非クライアント領域(フォームの枠)を右クリックすると処理される case WM_NCRBUTTONDOWN: label1.Text = "非クライアント領域で右クリック"; break; } base.WndProc(ref m); } //< 以下無関係領域============================================================================ ///
/// コンストラクタ ///
public Form01() { InitializeComponent(); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(96, 137); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(92, 12); this.label1.TabIndex = 0; this.label1.Text = "クリックしていません"; // // Form00 // this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.label1); this.Name = "Form00"; this.Text = "クリック感知"; this.ResumeLayout(false); this.PerformLayout(); } };