为要处理的panel添加绘画事件,代码写在panel的Paint事件中:
private void panel1_Paint(object sender, PaintEventArgs e) {
//(new 一个矩形,范围为panel的上半部分)
Rectangle rt1 = new Rectangle(0, 0, panel1.Width, (panel1.Height/2));
//(new一个渐变笔刷,渐变范围为上面新建的矩形,即panel的上半部分,渐变色从白色到蓝色,渐变角度LinearGradientMode.Vertical为垂直渐变) LinearGradientBrush br = new LinearGradientBrush(rt1, Color.White, Color.Blue, LinearGradientMode.Vertical); Graphics g = e.Graphics;//为控件创建Graphics g.FillRectangle(br, rt1);//填充矩形
//(下面同上面的操作,渐变panel的下半部分) LinearGradientBrush br2 = new LinearGradientBrush(rt1, Color.Blue, Color.White, LinearGradientMode.Vertical); Rectangle rt2 = new Rectangle(0,(panel1.Height/2), panel1.Width, panel1.Height/2); g.FillRectangle(br2, rt2);
//实现圆角形状的panel List list = new List();//建立点集合list int width = this.panel1.Width; int height = this.panel1.Height; //panel左上的点 list.Add(new Point(0, 4)); list.Add(new Point(1, 4)); list.Add(new Point(1, 3)); list.Add(new Point(2, 2)); list.Add(new Point(3, 1)); list.Add(new Point(4, 1)); list.Add(new Point(4, 0)); //panel右上的点 list.Add(new Point(width - 4, 0)); list.Add(new Point(width - 4, 1)); list.Add(new Point(width - 3, 1)); list.Add(new Point(width - 2, 2)); list.Add(new Point(width - 1, 3)); list.Add(new Point(width - 1, 4)); list.Add(new Point(width - 0, 4)); //panel右下角的点 list.Add(new Point(width - 0, height - 4));