【问题标题】:Winforms KeyDown function does not work even though I added it to the events即使我将它添加到事件中,Winforms KeyDown 功能也不起作用
【发布时间】:2020-04-17 19:20:37
【问题描述】:

函数如下:

 private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            if (SpielField[ypos + 1][xpos] != '#')
            {
                SpielField[ypos].Replace('@', ' ');
                ypos++;
                string temp = new string(SpielField[ypos].ToCharArray(), 0, xpos);
                temp += '@';
                temp += SpielField[ypos].Substring(xpos + 1);
                SpielField[ypos] = temp;
            }
        }
    }

这是我如何将它添加到构造函数中的事件:

this.KeyDown += Form1_KeyDown;

我正在为 ConsoleApp 实现 winforms 功能,但我相信这没有什么区别。

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing;




namespace MazeProject
{
    class Form1 : Form
    {
        string[] SpielField;
        int column;
        int line;
        int xpos;
        int ypos;
        public Form1(int c,int l,string[] input)
        {
            this.Width = 800;
            this.Height = 800;
            this.column = c;
            this.line = l;
            this.KeyPreview = true;
            SpielField = new string[column];
            SpielField = input;
            for (int i = 0; i < column; i++) {
                for(int j = 0; j < line; j++)
                {
                    if(SpielField[i][j] == '@')
                    {
                        xpos = j;
                        ypos = i;
                        break;
                    }
                }
            }
            this.Paint += Form1_Paint;
            this.KeyDown += Form1_KeyDown;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Console.WriteLine(column);
            for (int i = 0; i < column; i++)
            {
                Font f = new Font(FontFamily.GenericMonospace, 20, FontStyle.Regular);
                Brush b = new SolidBrush(Color.Black);
                e.Graphics.DrawString(SpielField[i], f, b , 10, 650-(i+2)*40);
            }
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                if (SpielField[ypos + 1][xpos] != '#')
                {
                    SpielField[ypos].Replace('@', ' ');
                    ypos++;
                    string temp = new string(SpielField[ypos].ToCharArray(), 0, xpos);
                    temp += '@';
                    temp += SpielField[ypos].Substring(xpos + 1);
                    SpielField[ypos] = temp;
                }
            }
        }
    }

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string filePath = System.IO.Directory.GetCurrentDirectory() + "\\maze.txt";
            string[] input = File.ReadAllLines(filePath);
            int l = Convert.ToInt32(input[0]);
            int c = Convert.ToInt32(input[1]);
            string[] data = new string[c];
            for(int i = 0; i < c; i++)
            {
                data[i] = input[i + 2];
            }
            Form1 game = new Form1(c, l, data);
            Console.WriteLine(c);
            Console.WriteLine(l);

            Application.EnableVisualStyles();
            Application.Run(game);
            Console.ReadLine();


        }
    }
}

【问题讨论】:

  • 这可能不会像您想要的那样运行。没有理由仅仅为了打开 Windows 窗体应用程序而创建控制台应用程序。如果使用控制台应用程序的全部目的是使用输入文件。只需创建一个Button 并将ButtonClick 事件附加到OpenFileDialog,以便您可以选择您的文本文件。

标签: c# forms winforms


【解决方案1】:

如果你想从表单本身截取关键事件你需要设置

Form1.KeyPreview = true;

否则按键会被当前焦点控件截取

Microsoft Docs on KeyPreview

【讨论】:

  • 还是不行。功能有问题吗?
  • 你是什么意思:我正在为 ConsoleApp 实现 winforms 功能?这是一个控制台应用程序吗?您已使用 WinForms 标记此问题。你能更好地解释一下你的背景吗?
  • 这是一个控制台应用程序,但我正在使用 winforms 库来使用绘图功能
  • 你如何启动winforms引擎?我的意思是您在 Main 方法中有什么来启动 Winform。此外,如果您需要 WinForms,那么为什么要创建一个控制台应用程序来启动用于绘图的表单?
  • Application.EnableVisualStyles();应用程序.运行(游戏);我使用这些方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多