簡體   English   中英

C#如何防止按鈕被箭頭聚焦?

[英]C# How do I prevent buttons from being focused by arrows?

我正在嘗試編寫一個簡單的游戲,但是我在代碼中發現了一個問題。 每次我要更改對象的位置時,它只會將按鈕聚焦在面板下方,而不是實際移動對象,因此,每次要移動對象時,都必須禁用按鈕,這是我不希望這樣做的方式。真的要使用,所以我想問你,如何解決這個問題和動作? 我希望對象移動,而不是按鈕移動。

碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Míček
{
    public partial class Micek : Form
    {
        Random generujPozici = new Random();
        int body; int vyskaKroku = 30; int sirkaMice = 40; int poziceMice = 0;
        int x1 = (450 / 2) - (40 / 2); int x2 = (450 / 2) - (120 / 2) ;
        int y1 = 20; int y2 = 302;

        public Micek()
        {
            InitializeComponent();
            TlacStop.Enabled = false;
            KeyDown += new KeyEventHandler(stikniTlacitko);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics KP = e.Graphics;
            Pen Pero = new Pen(Color.Black, 5);

            KP.FillEllipse(Brushes.Red, x1, y1, sirkaMice, sirkaMice);
            KP.DrawRectangle(Pero, x2, y2, 120, 1);
        }

        private void Stopky_Tick(object sender, EventArgs e)
        {
            y1 += vyskaKroku;
            poziceMice = generujPozici.Next(0, 450 - sirkaMice);

            if (y1 + vyskaKroku > y2 && x1 + sirkaMice > x2 && x1 - sirkaMice < x2 + (120 - sirkaMice))
            {
                body++;
                x1 = poziceMice;
                y1 = 20;
            }
            else
            {
                if(y1 > y2)
                {
                    x1 = poziceMice;
                    y1 = 20;
                    body = 0;
                }
            }

            pocitadloBody.Text = body.ToString();
            Refresh();
        }

        private void TlacStart_Click(object sender, EventArgs e)
        {
            Stopky.Enabled = true;
            TlacStart.Enabled = false;
            if(TlacStop.Enabled == false)
            {
                TlacStop.Enabled = true;
            }
        }

        private void TlacStop_Click(object sender, EventArgs e)
        {
            Stopky.Enabled = false;
            TlacStart.Enabled = true;
            TlacStop.Enabled = false;
            x1 = (450 / 2) - (40 / 2);
            y1 = 20;
            body = 0;
            Refresh();
        }

        void stikniTlacitko(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Right:
                    if(x2 < 330)
                    {
                        x2 += 10;
                        Refresh();
                    }
                break;

                case Keys.Left:
                    if (x2 > 0)
                    {
                        x2 -= 10;
                        Refresh();
                    }
                break;
            }
        }
    }
}

請嘗試以下步驟:

  1. 將表單的屬性KeyPreview設置為True。
  2. 覆蓋Win Coder中指示的ProcessCmdKey方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM