【问题标题】:C# How do I prevent buttons from being focused by arrows?C#如何防止按钮被箭头聚焦?
【发布时间】:2017-06-01 21:45:39
【问题描述】:

我正在尝试编写一个简单的游戏,但在我的代码中发现了一个问题。 每次我想更改对象的位置时,它只关注面板下方的按钮,而不是实际移动对象,所以每次我想移动对象时都必须禁用按钮,这是我不这样做的一种方式真的很想用,所以想问你,这个问题和机芯怎么解决? 我希望对象移动,而不是聚焦按钮。

代码:

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;
            }
        }
    }
}

【问题讨论】:

标签: c# button focus keypress


【解决方案1】:

尝试以下步骤:

  1. 将表单的属性 KeyPreview 设置为 True。
  2. 覆盖 Win Coder 中指示的 ProcessCmdKey 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 2012-06-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多