質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

解決済

1回答

960閲覧

アプリのスタート画面の追加

likuson

総合スコア34

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2020/06/30 08:40

前提・実現したいこと

visualstudioのC#でアプリケーションを作成しています。
アプリの最初にスタートボタンを配置し,クリックされたらボタンが消え、アプリを起動させたいです。

発生している問題・エラーメッセージ

ボタンをVisible = false;で非表示にしたら、その後のアプリのキー入力が反応しませんでした。
なので、非表示では無く、そのボタン自体を閉じ(終了)たいです。
エラーはありません。

該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.IO; 11 12namespace blockbreking_v2 13{ 14 public partial class Form1 : Form 15 { 16 Racket racket; 17 Ball ball; 18 static int numOfBlocks = 20; 19 Block[] blocks = new Block[numOfBlocks]; 20 public static int score; 21 22 Random random = new Random(); 23 bool rightKey = false, leftKey = false; 24 25 private void Initialize() 26 { 27 racket = new Racket(new Rectangle(pictureBox1.Width / 2 - 25, pictureBox1.Height - 10, 50, 10), 0, 0, 0, 10); 28 ball = new Ball(new Rectangle(0, 0, 10, 10), 0, 255, 0, new Point(7, 8)); 29 30 int drawX = 10, drawY = 40; 31 for (int i = 0; i < blocks.Length; i++) 32 { 33 blocks[i] = new Block(new Rectangle(drawX, drawY, 25, 10), random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)); 34 drawX += 100; 35 if (drawX >= pictureBox1.Width - 25) 36 { 37 drawX = 10; 38 drawY += 30; 39 } 40 } 41 score = 0; 42 43 } 44 public Form1() 45 { 46 InitializeComponent(); 47 Initialize(); 48 } 49 50 private void timer1_Tick(object sender, EventArgs e) 51 { 52 pictureBox1.Invalidate(); 53 } 54 55 private void Form1_KeyDown(object sender, KeyEventArgs e) 56 { 57 if (e.KeyCode == Keys.Right) 58 rightKey = true; 59 if (e.KeyCode == Keys.Left) 60 leftKey = true; 61 } 62 63 private void Form1_KeyUp(object sender, KeyEventArgs e) 64 { 65 if (e.KeyCode == Keys.Right) 66 rightKey = false; 67 if (e.KeyCode == Keys.Left) 68 leftKey = false; 69 } 70 71 private void startSToolStripMenuItem_Click(object sender, EventArgs e) 72 { 73 timer1.Enabled = true; 74 Initialize(); 75 } 76 77 private void saveSToolStripMenuItem_Click(object sender, EventArgs e) 78 { 79 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 80 { 81 Encoding enc = Encoding.GetEncoding("Shift_JIS"); 82 using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false, enc)) 83 { 84 sw.Write(score); 85 } 86 } 87 } 88 89 private void button1_Click(object sender, EventArgs e) 90 { 91 92 timer1.Enabled = true; 93 Initialize(); 94 95 } 96 97 private void pictureBox1_Paint(object sender, PaintEventArgs e) 98 { 99 if (rightKey) 100 racket.MoveRight(pictureBox1.Width); 101 if (leftKey) 102 racket.MoveLeft(); 103 racket.DrawImage(e.Graphics); 104 ball.CalcSpeed(new Point(pictureBox1.Width, pictureBox1.Height), racket.GetPosition()); 105 foreach (Block b in blocks) 106 { 107 if (!b.Hit(ball.GetPosition())) 108 b.DrawImage(e.Graphics); 109 else 110 { 111 score += 10; 112 toolStripStatusLabel1.Text = score.ToString(); 113 if (score == 200) 114 { 115 timer1.Enabled = false; 116 float x = 140.0F; 117 float y = 70.0F; 118 float width = 200.0F; 119 float height = 50.0F; 120 RectangleF drawRect = new RectangleF(x, y, width, height); 121 Font drawFont = new Font("Arial", 30); 122 e.Graphics.FillRectangle(Brushes.Gold, x, y, width, height); 123 e.Graphics.DrawString(" Clear", drawFont, Brushes.DeepSkyBlue, drawRect); 124 } 125 } 126 } 127 ball.Move(); 128 ball.DrawImage(e.Graphics); 129 if (!ball.JudgeFail(new Point(pictureBox1.Width, pictureBox1.Height))) 130 { 131 timer1.Enabled = false; 132 float x = 140.0F; 133 float y = 70.0F; 134 float width = 200.0F; 135 float height = 50.0F; 136 RectangleF drawRect = new RectangleF(x, y, width, height); 137 Font drawFont = new Font("Arial", 30); 138 e.Graphics.FillRectangle(Brushes.Black, x, y, width, height); 139 e.Graphics.DrawString(" Fail!", drawFont, Brushes.Red, drawRect); 140 } 141 } 142 143 } 144 class ParentClass 145 { 146 protected Rectangle position; 147 protected SolidBrush brush; 148 protected bool visible = true; 149 150 public ParentClass(Rectangle position, int r, int g, int b) 151 { 152 this.position = position; 153 brush = new SolidBrush(Color.FromArgb(r, g, b)); 154 } 155 public virtual bool Hit(Rectangle obstaclePosition) 156 { 157 if (position.IntersectsWith(obstaclePosition) && visible) 158 { 159 return true; 160 } 161 else 162 163 return false; 164 165 } 166 public Rectangle GetPosition() 167 { 168 return position; 169 } 170 public void SetPosition(Rectangle position) 171 { 172 this.position = position; 173 } 174 public bool GetVisible() 175 { 176 return visible; 177 } 178 public virtual void DrawImage(Graphics g) 179 { 180 if (visible) 181 g.FillRectangle(brush, position); 182 } 183 } 184 class Block : ParentClass 185 { 186 public Block(Rectangle position, int r, int b, int g) : base(position, r, g, b) 187 { 188 } 189 public override bool Hit(Rectangle obstaclePosition) 190 { 191 if (position.IntersectsWith(obstaclePosition) && visible) 192 { 193 visible = false;//ブロックを見えなくする 194 return true; 195 } 196 else 197 { 198 return false; 199 } 200 } 201 } 202 203 class Racket : ParentClass 204 { 205 int speed;//ラケットのスピード 206 public Racket(Rectangle position, int r, int g, int b, int speed) : base(position, r, g, b) 207 { 208 this.speed = speed*3; 209 } 210 public void MoveRight(int width) 211 { 212 if (position.X + speed <= width - position.Width) 213 position.X += speed; 214 else 215 position.X = width - position.Width; 216 } 217 public void MoveLeft() 218 { 219 if (position.X - speed >= 0) 220 position.X -= speed; 221 else 222 position.X = 0; 223 } 224 } 225 226 class Ball : ParentClass 227 { 228 bool preHit; 229 Point speed; 230 231 public Ball(Rectangle position, int r, int g, int b, Point speed) : base(position, r, g, b) 232 { 233 this.position = position; 234 this.speed = speed; 235 preHit = false; 236 } 237 public void CalcSpeed(Point pictureBocSize, Rectangle obstaclePosition) 238 { 239 if (position.IntersectsWith(obstaclePosition) && !preHit) 240 { 241 double degree = Math.Atan2(position.Y + position.Height / 2 - speed.Y - (obstaclePosition.Y + obstaclePosition.Height / 2), position.X + position.Width / 2 - speed.X - (obstaclePosition.X + obstaclePosition.Width / 2)); 242 double racketDegree = Math.Atan2(obstaclePosition.Height, obstaclePosition.Width); 243 if (degree >= racketDegree && degree <= racketDegree) 244 speed.X = -speed.X; 245 else 246 speed.Y = -speed.Y; 247 preHit = true; 248 } 249 else 250 preHit = false; 251 if (position.Y + speed.Y < 0) 252 speed.Y = -speed.Y; 253 if (position.X + speed.X > pictureBocSize.X - position.Width) 254 speed.X = -speed.X; 255 if (position.X + speed.X < 0) 256 speed.X = -speed.X; 257 } 258 public void Move() 259 { 260 int score = Form1.score; 261 if (score >= 0&& score <= 60) 262 { 263 position.X += speed.X ; 264 position.Y += speed.Y ; 265 } 266 else if(score >= 70 && score <= 130) 267 { 268 position.X += speed.X * 2; 269 position.Y += speed.Y * 2; 270 } 271 else 272 { 273 position.X += speed.X * 3; 274 position.Y += speed.Y * 3; 275 } 276 277 } 278 public bool JudgeFail(Point pictureBoxSize) 279 { 280 if (position.Y > pictureBoxSize.Y) 281 return false; 282 else 283 return true; 284 } 285 public override void DrawImage(Graphics g) 286 { 287 g.FillEllipse(brush, position); 288 } 289 } 290 291} 292

試したこと

this.closeを試しましたが、アプリケーション自体が閉じてしまい、スタートボタンを閉じることができませんでした。

補足情報(FW/ツールのバージョンなど)

visualstudio2019 windows form apprication

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ボタンが非表示になっただけで、フォーカスはボタンにあたったままになっているためでしょう。

  • 案1:ボタンを無効にする

ボタンのVisibleをfalseにした後、Enabledもfalseにする。

  • 案2:フォーカスを外す

ボタンのVisibleをfalseにした後、以下のコードを追加してみてください。

C#

1 this.ActiveControl = null;

ContainerControl.ActiveControl Property

  • 案3:キーイベントを常にFormで処理させる

ボタンのVisibleをfalseにした後、以下のコードを追加してみてください。
KeyDown,KeyUpはデザイナーからイベントハンドラを設定していない場合にコメントを外してください。(今回は不要と思います)

C#

1 KeyPreview = true; 2 //KeyDown += new KeyEventHandler(Form1_KeyDown); 3 //KeyUp += new KeyEventHandler(Form1_KeyUp);

Form.KeyPreview Property

投稿2020/06/30 09:50

編集2020/06/30 10:37
SHOMI

総合スコア4079

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YAmaGNZ

2020/06/30 10:30

案3ですが、現状デザイナーからイベントハンドラを設定しているっぽいので、コードでイベントハンドラを追加したら2重登録になるのではないでしょうか。 その点を注意点として記載が必要なのではないかと思います。
SHOMI

2020/06/30 10:33

あ、そうですね。追記しておきます。
likuson

2020/06/30 12:52

private void button1_Click(object sender, EventArgs e) { button1.Visible = false; this.ActiveControl = null; Initialize(); timer1.Enabled = true; } と変更しました。しっかり、キー入力できました。ありがとうございます。 しかし、スタートボタンを押す前に後ろでゲームがスタートしてしまいます。 スタートボタンを押すと、ゲームがリセットされるので、ゲームに影響は無いのですが、どうにかできないでしょうか。
YAmaGNZ

2020/06/30 13:15

フラグを設けて管理すればいいのではないですか?
SHOMI

2020/07/02 00:46

もしくはボタンがVisibleの間はゲームの処理をしないようにするとか
likuson

2020/07/02 07:22

具体的なプログラムを教えていただけないでしょうか。
SHOMI

2020/07/04 04:26

動かしてみてはいませんが、pictureBox1_Paintの先頭でフラグもしくはボタンがVisibleかを確認してreturnすればよいのでは?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問