C#键盘事件处理程序矩形

时间:2016-02-05 20:11:13

标签: c# events drawing

我是C#的新手并尝试制作键盘事件。按下W,A,S或D键时应显示。首先,我的计划是展示一些pictureBox,如果按下正确的键,只需更改图片。

但后来我在互联网上搜索了类似于Java http://docs.oracle.com/javase/8/javafx/sample-apps/KeyboardExample.zip的内容。 它看起来像这样: enter image description here

据我所知,代码正在绘制一个带有一些字母的矩形。我看了msdn并找到了绘制矩形的示例: https://msdn.microsoft.com/de-de/library/sx8yykw8(v=vs.110).aspx

不幸的是我陷入了困境。通常我使用工具箱将内容添加到表单中。然后我双击它并在括号内写下我的代码。但是工具箱中没有“矩形”,所以我不确定如何添加它。

到目前为止,这是我的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //Stay always on top
        this.TopMost = true;
        //Does not work. Removes border but you can't move the window after this
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Can I delete this?            
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= 65 && e.KeyChar <= 122)
        {
            switch (e.KeyChar)
            {
                //If pressed w or W
                case (char)119:
                case (char)87:
                    Console.WriteLine(e.KeyChar);
                    break;
                //If pressed a or A
                case (char)97:
                case (char)65:
                    Console.WriteLine(e.KeyChar);
                    break;
                //If pressed s or S
                case (char)83:
                case (char)115:
                    Console.WriteLine(e.KeyChar);
                    break;
                //If pressed d or D
                case (char)100:
                case (char)68:
                    Console.WriteLine(e.KeyChar);
                    break;
                //Other keys
                default:
                    lblMessage.Text = "Key not supported";
                    //does not work
                    //timer1_Tick();
                    break;
            }
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        lblMessage.Hide();
    }
}

以下是我的表单现在的样子:

enter image description here

此刻我正在坚持的其他事情:

  • 如何在几秒钟后从Form1_KeyPress调用计时器隐藏lblMessage?

  • 删除边框而不会失去移动窗口的能力(例如,与this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;一样)

编辑:我将代码更改为最新的工作状态。

2 个答案:

答案 0 :(得分:4)

欢迎来到Windows桌面编程世界!

你有两个选择;你可以:

  1. 使用设计视图将组件添加到WASD表单(因为你有W,A,S,D框,看起来你已经添加了它们)和你的Form1_KeyPress()处理程序,更新框的属性。这可以简单如下,只需确保将其更改为正确的组件名称:

    //If pressed w or W
    case (char)119:
    case (char)87:
            Console.WriteLine(e.KeyChar);
            button1.BackColor = Color.Red;//Highlight W
            button2.BackColor = Color.Empty;//Ignore A
            button3.BackColor = Color.Empty;//Ignore S
            button3.BackColor = Color.Empty;//Ignore D
            break;
    
  2. 覆盖表单的OnDraw()处理程序并直接在屏幕上绘制框。这样更难,但会给你更多的力量。

  3. 关闭标签很容易。在Form1_Load()处理程序中,确保设置timer1的超时属性:

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Interval = 5000;//In ms = thousandths-of-a-second
    }
    

    在Form1_KeyPress()处理程序中打开计时器:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        ...
        lblMessage.Enabled = true;
        timer1.Start();
    }
    

    完成你的工作并在timer1_Tick()处理程序中关闭计时器:

    private void timer1_Tick(object sender, EventArgs e)
    {
        lblMessage.Enabled = false;
        timer1.Stop();
    }
    

答案 1 :(得分:2)

以下是我快速整理的内容:

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 WASD_Keyboard
{
    public partial class Form1 : Form
    {
        private PictureBox pictureBox1 = new PictureBox();
        private bool wPressed = false;
        private bool aPressed = false;
        private bool sPressed = false;
        private bool dPressed = false;
        private Timer timer = new Timer();

        public Form1()
        {
            InitializeComponent();
            //Stay always on top
            this.TopMost = true;
            //Does not work. Removes border but you can't move the window after this
            this.FormBorderStyle = FormBorderStyle.None;

            timer.Interval = 3000;
            //this is an event binding
            timer.Tick += timer1_Tick;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Dock the PictureBox to the form and set its background to white.
            pictureBox1.Dock = DockStyle.Fill;
            pictureBox1.BackColor = Color.White;
            pictureBox1.Paint += DrawRectangleRectangle;
            // Add the PictureBox control to the Form.
            this.Controls.Add(pictureBox1);
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // reversed logic to stop nesting
            if (e.KeyChar < 65 || e.KeyChar > 122) return;

            wPressed = false;
            aPressed = false;
            sPressed = false;
            dPressed = false;
            //this should really be multiple if statement so it can do more than one key
            //If pressed w or W
            if (e.KeyChar == (char) 119 || e.KeyChar == (char) 87) {
                wPressed = true;
                Console.WriteLine(e.KeyChar);
            }

            //If pressed a or A
            if (e.KeyChar == (char) 97 || e.KeyChar == (char) 65) {
                aPressed = true;
                Console.WriteLine(e.KeyChar);
            }

            //If pressed s or S
            if (e.KeyChar == (char) 83 || e.KeyChar == (char) 115) {
                sPressed = true;
                Console.WriteLine(e.KeyChar);
            }

            //If pressed d or D
            if (e.KeyChar == (char) 100 || e.KeyChar == (char) 68) {
                dPressed = true;
                Console.WriteLine(e.KeyChar);
            }

            if (!wPressed && !aPressed && !sPressed && !dPressed) {
                //Something goes wrong
                lblMessage.Text = "Key not supported";
                return;
            }

            pictureBox1.Refresh();
            // in older .net if you didn't do both you ran into multiple issues
            timer.Enabled = true;
            timer.Start();
        }

        public void DrawRectangleRectangle(object sender, PaintEventArgs e)
        {
            DrawRectangle(e, new Point(40, 10), new Size(20, 20), 'W', wPressed ? Color.Red : Color.White);
            DrawRectangle(e, new Point(10, 40), new Size(20, 20), 'A', aPressed ? Color.Red : Color.White);
            DrawRectangle(e, new Point(40, 40), new Size(20, 20), 'S', sPressed ? Color.Red : Color.White);
            DrawRectangle(e, new Point(70, 40), new Size(20, 20), 'D', dPressed ? Color.Red : Color.White);
        }

        public void DrawRectangle(PaintEventArgs e, Point p, Size s, char letter, Color c)
        {
            // Create pen.
            var blackPen = new Pen(Color.Black, 3);
            var brush = new SolidBrush(c);
            // Create rectangle.
            var rect = new Rectangle(p, s);

            // Draw rectangle to screen.
            e.Graphics.DrawRectangle(blackPen, rect);
            e.Graphics.FillRectangle(brush, rect);
            e.Graphics.DrawString(letter.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Blue, rect);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            wPressed = false;
            aPressed = false;
            sPressed = false;
            dPressed = false;

            timer.Enabled = false;
            timer.Stop();
            pictureBox1.Refresh();
        }
    }
}

注意: 这是高度异步,但没有使用任何锁定......