panel中绘制了圆,该如何用键盘实现其圆的透明度变化

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;namespace penal圆
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                panel1.Paint += new PaintEventHandler(panel1_Paint);
            }
            int radius = 20;
            byte alpha = 128;
            void panel1_Paint(object sender, PaintEventArgs e)
            {
                var g = e.Graphics;
                g.TranslateTransform(panel1.Width / 2, panel1.Height / 2);
                g.SmoothingMode = SmoothingMode.AntiAlias;            g.FillRectangle(Brushes.Yellow, -100, 0, 200, 100);
                var sb = new SolidBrush(Color.FromArgb(alpha, 255, 0, 0));
                g.FillEllipse(sb, -radius, -radius, 2 * radius, 2 * radius);
            }
            protected override void OnKeyUp(KeyEventArgs e)
            {
                base.OnKeyUp(e);
                if (Keys.Up == e.KeyCode)
                {
                    radius += 10;
                }
                else if (Keys.Down == e.KeyCode)
                {
                    radius -= 10;
                }
                if (Keys.Left == e.KeyCode)
                {
                    alpha += 10;
                }
                else if (Keys.Right == e.KeyCode)
                {
                    alpha -= 10;
                }
                panel1.Invalidate();
            }
        }
    }