简体   繁体   English

C#如何防止按钮被箭头聚焦?

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

I'm trying to write a simple game, but I found a problem in my code. 我正在尝试编写一个简单的游戏,但是我在代码中发现了一个问题。 Every time I want to change the position of an object it only focuses the buttons below the panel instead of actually moving the object, so I have to disable the buttons every time I want to move the object, which is a way that I don't really want to use, so I want to ask you, how do I fix this issue and the movement? 每次我要更改对象的位置时,它只会将按钮聚焦在面板下方,而不是实际移动对象,因此,每次要移动对象时,都必须禁用按钮,这是我不希望这样做的方式。真的要使用,所以我想问你,如何解决这个问题和动作? I want the object to move, not the buttons to focus. 我希望对象移动,而不是按钮移动。

Code: 码:

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

Try the following steps: 请尝试以下步骤:

  1. Set form's property KeyPreview to True. 将表单的属性KeyPreview设置为True。
  2. Override the ProcessCmdKey method as indicated in Win Coder . 覆盖Win Coder中指示的ProcessCmdKey方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建 WPF 应用程序时,如何防止菜单栏被选项卡聚焦 - When creating a WPF application, how do I prevent a menu bar from being focused with tab 如何防止C#控制台应用程序中的Ctrl键生成特殊字符? - How do I prevent special characters from being generated by the Ctrl key in a C# console application? c# 如何防止将一个特定字符添加到文本文件中 - c# how do i prevent one specific character from being added to a text file 如何防止ContextMenu限制在其容器中(Win Forms c#) - How do I prevent my ContextMenu from being constrained to its container (Win Forms c#) 如何防止 WPF 按钮在单击后保持突出显示? - How do I prevent WPF buttons from remaining highlighted after being clicked? 在C#中,如何防止多次按下PreviewKeyDown事件,直到特定代码完成? - In C# How do I prevent a PreviewKeyDown Event from being presses multiple times until the certain code has complete? 如何使用C#防止文本块重叠? - How do I use C# to prevent textblocks from overlapping? 在C#中的数字文本框中,如何防止仅将小数点放在第一位? - In C#, in a numerical textbox, how do I prevent a decimal point being placed in as the first digit only? 使用c#导入excel doc时,如何防止前导零被剥离? - How do you prevent leading zeros from being stripped when importing an excel doc using c# C# WinForms:当父窗体最小化时,如何防止子窗体最小化? - C# WinForms: How do you prevent child form from being minimized when parent form is minimized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM