C#


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

キャプションバーのないプログラムについて別のソリューションを考えます。 完全におまけみたいなものです。今回は、キーボードを使ってウィンドウを動かすことにします。

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 107 108 109 110 111 112 113 114 115 116 117 118

//2009/09/x Shirao //キャプションバーのないプログラムサンプル using System; using System.Drawing; using System.Windows.Forms; class NonCaptionK : Form { //これも自動生成コード private Button button1; private Button button2; private TextBox textBox1; //メッセージを利用する方法 private const int WM_NCHITTEST = 0x84; //ポインタの動作を処理 private const int HTCLIENT = 0x1; //クライアント領域内にある private const int HTCAPTION = 0x2; //キャプションバー上にある public static void Main() { Application.Run(new NonCaptionK()); } public NonCaptionK() { InitializeComponent(); } #region Visual Studio 自動生成コード private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = 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 = "時間"; // // 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); // // 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); // // NonCaptionK // 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 = "NonCaptionK"; 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(); } #region キーボード protected override void OnKeyDown(KeyEventArgs e) { Graphics grfx = CreateGraphics(); switch (e.KeyCode) { case Keys.Right: Location = new Point(Location.X + 20, Location.Y); break; case Keys.Left: Location = new Point(Location.X - 20, Location.Y); break; case Keys.Up: Location = new Point(Location.X, Location.Y - 20); break; case Keys.Down: Location = new Point(Location.X, Location.Y + 20); break; default: break; } base.OnKeyDown(e); } #endregion }

ポイント

OnKeyDown

OnKeyDownをオーバーライドして動かします。
このとき、Form.KeyPreviewにtrueを設定しておいてください。
方向キーを押すとその方向に動かすようにすればOKです。 動かすのは、キーが押されるとLocationに一定の値を加減算すればできます。 どのキーが押されたかというのはKeyEventArgsKeyCodeから得られます。
今回は冗談で使いましたが、本当にこれを使うなら、境界処理と移動値、各コントロールのキー動作 について考える必要があります。
行97のGraphicsは別の処理で使ってた名残です。気にしないでください。

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

protected override void OnKeyDown(KeyEventArgs e) { Graphics grfx = CreateGraphics(); switch (e.KeyCode) { case Keys.Right: Location = new Point(Location.X + 20, Location.Y); break; case Keys.Left: Location = new Point(Location.X - 20, Location.Y); break; case Keys.Up: Location = new Point(Location.X, Location.Y - 20); break; case Keys.Down: Location = new Point(Location.X, Location.Y + 20); break; default: break; } base.OnKeyDown(e); }

付録

ソース・実行ファイル

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


プログラムに戻る