C#


キャプションバーのないプログラム #2

キャプションバーのないプログラムについて別のソリューションを考えます。 前回紹介した方法(マウスイベントをオーバーライドする)は素直な方法で、Macでも問題なく動くのですが、 Windows環境に限定するならもっと楽な方法が存在します。 それは、(overrideすることで)ウィンドウプロシージャを使う方法です。 これはVC++ではこっちのほうが普通の素直なソリューションだと思うのですがどうでしょうか。
では、前回と同じ機能のアプリケーションを作って実例としましょう。
まずはコードです。

001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106

//2009/09/x Shirao //キャプションバーのないプログラムサンプル using System; using System.Drawing; using System.Windows.Forms; class NonCaptionM : Form { //これも自動生成コード //メッセージを利用する方法 private const int WM_NCHITTEST = 0x84; //ポインタの動作を処理 private const int HTCLIENT = 0x1; //クライアント領域内にある private const int HTCAPTION = 0x2; private TextBox textBox1; private Button button2; private Button button1; //キャプションバー上にある public static void Main() { Application.Run(new NonCaptionM()); } public NonCaptionM() { InitializeComponent(); } #region Visual Studio 自動生成コード private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(268, 19); this.textBox1.TabIndex = 0; this.textBox1.Text = "時間"; // // button2 // this.button2.Location = new System.Drawing.Point(160, 37); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(120, 23); this.button2.TabIndex = 2; this.button2.Text = "消去"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Location = new System.Drawing.Point(12, 37); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "時間表示"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // NonCaptionM // this.ClientSize = new System.Drawing.Size(292, 72); this.ControlBox = false; this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.KeyPreview = true; this.MaximumSize = new System.Drawing.Size(300, 80); this.MinimumSize = new System.Drawing.Size(300, 80); this.Name = "NonCaptionM"; this.ShowInTaskbar = false; this.TopMost = true; this.ResumeLayout(false); this.PerformLayout(); } #endregion private void button1_Click(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString(); } private void button2_Click(object sender, EventArgs e) { textBox1.Clear(); } //メッセージを利用する方法 protected override void WndProc(ref Message msg) { switch (msg.Msg) { case WM_NCHITTEST: base.WndProc(ref msg); if ((int)msg.Result == HTCLIENT) //クライアント領域にあり msg.Result = (IntPtr)HTCAPTION; //キャプションバーにあるとみなす return; } base.WndProc(ref msg); } }

ポイント

ウィンドウプロシージャのオーバーライド

ここでやっているのは、クライアント領域をクリックすると、 キャプションバーをクリックしているようにみなすべしということです。 WM_NCHITTESTはWindowsメッセージで、 これはそのフォームのすべてのマウスイベントに先立って送られるメッセージです。 これが送られてきたら、マウスイベントを調べてResultがHTCLIENT(クライアント領域上にある) なら、ResultをHTCAPTIONに書き換えることで実現します。
(ウィンドウプロシージャとはなんだ?と思われるかもしれません。調べてみてください。)

94 95 96 97 98 99 100 101 102 103 104 105

protected override void WndProc(ref Message msg) { switch (msg.Msg) { case WM_NCHITTEST: base.WndProc(ref msg); if ((int)msg.Result == HTCLIENT) //クライアント領域にあり msg.Result = (IntPtr)HTCAPTION; //キャプションバーにあるとみなす return; } base.WndProc(ref msg); }

付録

ソース・実行ファイル

C#ソースコードと実行ファイルを置いておきます。


プログラムに戻る