这功能很简单嘛,主要是肯定只会在panel出现一个图形,不会出现两个。只不过有点麻烦,需要先画好三角形,正方形,做成控件,以控件形式添加到panel里显示,然后只要处理键盘事件就可以达到效果了。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;namespace WindowsApplication162
    {
        public partial class Form1 : Form
        {
            Panel Shape = new Panel();
            Panel P = new Panel();        public Form1()
            {
                InitializeComponent();            ComboBox CB = new ComboBox();
                CB.Parent = this;
                CB.Items.AddRange(new Object[] { "三角形", "正方形" });
                CB.SelectedIndexChanged += new EventHandler(CB_SelectedIndexChanged);
                CB.DropDownStyle = ComboBoxStyle.DropDownList;            P.Parent = this;
                P.Location = new Point(0, 20);
                this.KeyDown += new KeyEventHandler(Form1_KeyDown);
                this.KeyPreview = true;
                CB.KeyDown += new KeyEventHandler(CB_KeyDown);
            }        void CB_KeyDown(object sender, KeyEventArgs e)
            {
                e.SuppressKeyPress = true; // 防止联动,只允许鼠标选择ComboBox
            }        void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Up: Shape.Top--; break;
                    case Keys.Down: Shape.Top++; break;
                    case Keys.Left: Shape.Left--; break;
                    case Keys.Right: Shape.Left++; break;
                }
            }        void CB_SelectedIndexChanged(object sender, EventArgs e)
            {
                ComboBox CB = (ComboBox)sender;
                GraphicsPath GP = new GraphicsPath();            switch (CB.SelectedIndex)
                {
                    case 0:
                        GP.AddLine(0, 60, 30, 0);
                        GP.AddLine(30, 0, 60, 60);
                        GP.AddLine(60, 60, 0, 60);
                        break;
                    case 1:
                        GP.AddRectangle(new Rectangle(0, 0, 60, 60));
                        break;
                }            Shape.Parent = P;
                Shape.BackColor = Color.Red;
                Shape.Region = new Region(GP);
            }
        }
    }
      

  2.   

    画三角形等几何图形比较麻烦,用图片代替。再判断键盘事件,改变图片在panel中的位置就可以。