窗体界面设计02

 01.渐变色窗体

 Color颜色对象的FromArgb()方法的应用,语法结构:

 public static System.Drawing.Color FromArgb(int red,int green,int blue)

 Pen对象的应用

 Graphics对象的DrawRectangle方法,语法结构:

 public void DrawRectangle(System.Drawing.Pen pen,float x,float y,float width,float height)

代码
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            
base.OnPaintBackground(e);
            
int y, dy;                              //定义变量
            y = this.ClientRectangle.Location.Y;
            dy 
= this.ClientRectangle.Height / 256;
            
for(int i=255;i>=0;i--)                 //利用For循环语句渐变窗体背景
            {
                Color c
=new Color();                //定义颜色对象案例
                
//调用Color对象的FromArgb方法
                c=Color.FromArgb(255,i,0);
                SolidBrush sb
=new SolidBrush(c);    //定义画笔颜色
                Pen p = new Pen(sb, 1);             //定义画笔
                
//绘制矩形
                e.Graphics.DrawRectangle(p, this.ClientRectangle.X, y, this.Width, y + dy);
                y 
= y + dy;
            }
        }

        
private void Form1_DoubleClick(object sender, EventArgs e)
        {
            
this.Close();
            Application.Exit();
        }

 02.笑脸窗体

 Panel控件的应用

 GrahpicsPath类的应用

 Point点对象的应用

 AddString方法的应用,语法结构:

代码
public void AddString(
string s,
System.Drawing.FontFamily family,
int style,
float emSize,
System.Drawing.Point origin,
System.Drawing.StringFormat format
)
参数意义:
s:要添加的System.String
family:一个System.Drawing.FontFamily,表示绘制文本所用字体的名称。
style:一个System.Drawing.FontStyle枚举,它表示有关文本的样式信息(粗体、斜体等),并且必须为整数。
emSize:限定字符的Em(字体大小)方框的高度。
origin:一个System.Drawing.Point,它表示文本从其起始的点。
format:指定文本格式设置信息(如行间距和对齐方式)的System.Drawing.StringFormat。
代码
        private void Form2_Load(object sender, EventArgs e)
        {
            
this.Left = (SystemInformation.PrimaryMonitorMaximizedWindowSize.Width - this.Width) / 2;
            
this.Top = (SystemInformation.PrimaryMonitorMaximizedWindowSize.Height - this.Height) / 2;

            GraphicsPath gp 
= new GraphicsPath();                               //创建GraphicsPath对象实例
                                                                                
//定义矩形区域
            Rectangle rect = new Rectangle(new Point(00), new Size(this.Width , this.Height));
            gp.AddEllipse(rect);                                                
//绘制椭圆
            this.Region=new Region(gp);

            GraphicsPath gpl
=new GraphicsPath();
            Rectangle rectl
=new Rectangle(new Point(0,0),new Size(this.panel1.Width,this.panel1.Height));
            gpl.AddEllipse(rectl);
            
this.panel1.Region=new Region(gpl);                                 //绘制左眼

            GraphicsPath gpr 
= new GraphicsPath();
            Rectangle rectr 
= new Rectangle(new Point(00), new Size(this.panel2.Width, this.panel2.Height));
            gpr.AddEllipse(rectr);
            
this.panel2.Region = new Region(gpr);                               //绘制右眼

            GraphicsPath myPath 
= new GraphicsPath();
            Rectangle rectm 
= new Rectangle(00this.panel3.Width, this.panel3.Height);
            myPath.StartFigure();
            myPath.AddArc(rectm, 
0180);
            myPath.CloseFigure();
            
this.panel3.Region  = new Region(myPath);                           //绘制嘴

            GraphicsPath gpp 
= new GraphicsPath();
            
string stringText = "退出";
            FontFamily family 
= new FontFamily("宋体");
            
int fontStyle = (int)FontStyle.Bold;
            
int emSize = 20;
            Point origin 
= new Point(2020);
            StringFormat format 
= StringFormat.GenericDefault;                   //绘制退出文字
            gpp.AddString(stringText, family, fontStyle, emSize, origin, format);
            
this.button1.Region = new Region(gpp);
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
if (MessageBox.Show("你确定要退出吗?""提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Application.Exit();
            }

        }

 03.八边形图形窗体

 利用Graphics对象绘制图案,CreateGraphics方法创建Graphics对象案例

 Pen类的应用

 Brush类的应用:

 SolidBrush:画笔的最简单形式,它用纯色进行绘制

 LinearGradientBrush:使用两种颜色的渐变色进行绘制

 HatchBrush:与SolidBrush相似,但可以从大量预设的图案中选择要使用的图案,而不是纯色。

 TextureBrush:使用纹理进行绘制。

 PathGradientBrush:基于开发人员定义的惟一路径,使用复杂的混合色渐变进行绘制。

 Color类的应用

代码
using System.Drawing.Drawing2D

        
private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics g 
= this.CreateGraphics();                       //定义Graphics对象实例
            Draw2D();                                                 //调用函数Draw2D
        }

        
public void Draw2D()
        {
            
int i;                                                    //定义整型变量并赋值
            int Sect = 8;
            
float r;
            
float[] x = new float[31];                                //定义浮点型变量数组
            float[] y = new float[31];
            
this.ClientSize = new Size(300300);
            r 
= this.ClientSize.Width / 2;
            Graphics g 
= this.CreateGraphics();                       //创建Graphics对象案例
            for (i = 0; i < Sect; i++)                                //利用For循环为数组赋值
            {
                x[i] 
= (float)(r * Math.Cos(i * 2 * Math.PI / Sect) + this.ClientSize.Width / 2);
                y[i] 
= (float)(r * Math.Sin(i * 2 * Math.PI / Sect) + this.ClientSize.Height / 2);
            }
            
for (int m = 0; m < Sect - 1; m++)                        //利用双For循环绘制图案
            {
                
for (int n = 0; n < Sect; n++)
                {
                    g.DrawLine(Pens.Red, x[m], y[m], x[n], y[n]);     
//绘制红色的直线
                }
            }
        }


        
private void Form3_Load(object sender, EventArgs e)
        {
            
this.timer1.Enabled = true;                               //计时器可用
            this.TopMost = true;                                      //总在最前
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        
private void Form3_DoubleClick(object sender, EventArgs e)
        {
            
this.Close();                                             //关闭程序
        }

 04.动态绘制直线和曲线

 MouseUp事件、MouseMove事件和MouseDown事件的应用

 Graphics对象的DrawLine()方法的应用,语法结构:

 public void DrawLine(System.Drawing.Pen pen,float x1,float y1,float x2,float y2)

 Graphics对象的DrawRectangle()方法的应用,语法结构:

 public void DrawRectangle(System.Drawing.Pen pen,float x,float y,float width,float height)

代码
        int startX;         //获取鼠标起始点的X坐标
        int startY;         //获取鼠标起始点的Y坐标
        Graphics g;         //定义Graphics对象实例

        
private void Form1_Load(object sender, EventArgs e)
        {
            
this.StartPosition = FormStartPosition.CenterScreen;
            
this.BackColor = Color.Snow;        //设置窗体背景颜色
        }

        
private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            g 
= this.CreateGraphics();          //创建Graphics对象实例
            Pen p = new Pen(Color.Red, 4);      //设置画笔颜色和宽度
            if (radioButton1.Checked == true)
            {
                g.DrawLine(p, startX, startY, e.X, e.Y);
            }
        }

        
private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            startX 
= e.X;                       //为变量赋值
            startY = e.Y;
        }

        
private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            g 
= this.CreateGraphics();
            Pen p 
= new Pen(Color.Blue, 2);     //设置画笔颜色和宽度
            if (radioButton2.Checked == true)
            {
                g.DrawRectangle(p, e.X, e.Y, 
11); //绘制曲线
            }

        }

        
private void button1_Click(object sender, EventArgs e)
        {
            g 
= this.CreateGraphics();
            g.Clear(Color.Snow);                    
//清空窗体背景
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
this.Close();
            Application.Exit();
        }

 05.动态绘制验证码

 产生随机数的Random类的应用

 Bitmap对象的SetPixel()方法的应用,语法结构:

 Public void SetPixel(int x,int y,System.Drawing.Color color)

代码
        private string CheckCode()
        {
            
int number;                                              //定义变量
            char code;
            
string checkCode = String.Empty;
            Random random 
= new Random();                            //产生非负随机数
            for (int i = 0; i < 4; i++)                              //利用For循环产生4由英文字母或数字组成的字符串
            {
                number 
= random.Next();
                
if (number % 2 == 0)
                    code 
= (char)('0' + (char)(number % 10));        //随机数字
                else
                    code 
= (char)('A' + (char)(number % 26));        //随机字母
                checkCode += " " + code.ToString();
            }
            
return checkCode;                                        //返回产生的随机数字和字母
        }

        
private void CodeImage(string checkCode)
        {
            
if(checkCode==null||checkCode.Trim()==String.Empty)
            {
                
return;
            }
            System.Drawing.Bitmap image
=new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length*50.0)),50);
            Graphics g
=Graphics.FromImage(image);
            
try
            {
                Random random
=new Random();                         //产生非负随机数
                g.Clear(Color.White);                               //清空图像背景色
                for (int i = 0; i < 3; i++)                         //绘制图像的背景噪声线
                {
                    
int x1 = random.Next(image.Width);
                    
int x2 = random.Next(image.Width);
                    
int y1 = random.Next(image.Height);
                    
int y2 = random.Next(image.Height);
                    g.DrawLine(
new Pen(Color.Black), x1, y1, x2, y2);
                }
                Font font
=new System.Drawing.Font("Arial",40,(System.Drawing.FontStyle.Bold));
                g.DrawString(checkCode,font,
new SolidBrush(Color.Red),2,2);
                                                                   
//绘制图像的前景噪声点

                
for(int i=0;i<1000;i++)
                {
                    
int x=random.Next(image.Width);
                    
int y=random.Next(image.Height);
                    image.SetPixel(x,y,Color.FromArgb(random.Next()));
                }
                                                                   
//绘制图像的边框线

                
//g.DrawRectangle(new Pen(Color.Silver),0,0,image.Width-1,image.Height-1);
                this.pictureBox1.Width=image.Width;
                
this.pictureBox1.Height=image.Height;
                
this.pictureBox1.BackgroundImage=image;
            }
            
catch
            {
            }
        }


        
private void Form2_Load(object sender, EventArgs e)
        {
            CodeImage(CheckCode());                               
//调用函数CodeImage
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            CodeImage(CheckCode());
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

 06.椭圆及椭圆弧的绘制

 Graphics对象的DrawEllipse()方法的应用,语法结构:

 public void DrawEllipse(System.Drawing.Pen pen,float x,float y,float width,float height)

 Graphics对象的DrawArc()方法的应用,语法结构:

 public void DrawArc(System.Drawing.Pen pen,float x,float y,float widht,float height,float startAngle,float sweepAngle)

 startAngle:从x轴到弧线的点沿顺时针方向度量的角(以度为单位)

 sweepAngle:从startAngle参数到弧线的结束点沿顺时针方向度量的角(以度为单位)

 Graphics对象的FillPie()方法的应用,语法结构:

 public void FillPie(System.Drawing.Brush brush,float x,float y,float width,float height,float startAngle,float sweepAngle)

代码
        private void button1_Click(object sender, EventArgs e)
        {

            Bitmap bitM 
= new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            Graphics g 
= Graphics.FromImage(bitM);
            g.Clear(Color.White);
            g.DrawEllipse(
new Pen(Color.Blue), 101010050);             //绘制椭圆
            this.pictureBox1.BackgroundImage = bitM;
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bitM 
= new Bitmap(this.pictureBox2.Width, this.pictureBox2.Height);
            Graphics g 
= Graphics.FromImage(bitM);
            g.Clear(Color.White);
            g.DrawEllipse(
new Pen(Color.Blue), 30107050);              //绘制圆
            this.pictureBox2.BackgroundImage = bitM;
        }

        
private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bitM 
= new Bitmap(this.pictureBox3.Width, this.pictureBox3.Height);
            Graphics g 
= Graphics.FromImage(bitM);
            g.Clear(Color.White);
            g.DrawArc(
new Pen(Color.Blue), 1010905030180);        //绘制椭圆弧
            this.pictureBox3.BackgroundImage = bitM;
        }

        
private void button4_Click(object sender, EventArgs e)
        {
            Bitmap bitM 
= new Bitmap(this.pictureBox4.Width, this.pictureBox4.Height);
            Graphics g 
= Graphics.FromImage(bitM);
            g.Clear(Color.White);
            g.FillPie(
new SolidBrush(Color.Red), 10101005090270); //绘制填充椭圆弧
            g.FillPie(new SolidBrush(Color.Yellow), 10101005090 + 27090);
            
this.pictureBox4.BackgroundImage = bitM;
        }

        
private void button5_Click(object sender, EventArgs e)
        {
            
this.Close();
            Application.Exit();
        }

 07.移动鼠标复制坐标区域图像

 PointToScreen()方法的应用,语法结构:

 public System.Drawing.Point PointToScreen(System.Drawing.Point p)

 Size对象的应用

 Graphics对象的CopyFromScreen()方法的应用,语法结构:

 public void CopyFromScreen(int courceX,int sourceY, int destinationX,int destinationY,System.Drawing.Size blockRegionSize)

 参数意义:

 sourceX:位于源矩形左上角的点的x坐标

 sourceY:位于源矩形左上角的点的y坐标

 destinationX:位于目标矩形左上角的点的x坐标

 destinationY:位于目标矩形左上角的点的y坐标

 blockRegionSize:要传输的区域大小

代码
        private void Form4_MouseMove(object sender, MouseEventArgs e)
        {
            Point myp 
= this.PointToScreen(e.Location);
            Graphics myg 
= this.CreateGraphics();
            Size mys 
= new Size(100100);
            myg.CopyFromScreen(myp.X 
- 50, myp.Y - 5000, mys);
            myg.Dispose();
        }

 08.动态获取当前程序的图标

 Icon对象的应用

 Graphics对象的DrawImage()方法的应用,语法结构:

 public void DrawImage(System.Drawing.Image image,float x,float y)

 Graphics对象的DrawString()方法的应用,语法结构:

 public void DrawString(string s,System.Drawing.Font font,System.Drawing.Brush brush,float x,float y)

代码
        private void button1_Click(object sender, EventArgs e)
        {
            Icon icon1 
= new Icon(this.Icon, 6060);                   //定义Icom对象实例
            Graphics myg = this.CreateGraphics();                       //创建Graphics对象实例
            Bitmap bmp = icon1.ToBitmap();                              //创建Bitmap对象实例
            myg.DrawImage(bmp, new Point(10020));                     //绘制图标
            myg.DrawString("当前程序的图标高度:" + icon1.Height.ToString(), Font, Brushes.Red, 5070);
            myg.DrawString(
"当前程序的图标宽度:" + icon1.Width.ToString(), Font, Brushes.Red, 5090);
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
this.Close();
            Application.Exit();
        }
 

 09.动态获取系统图标

 SystemIcons对象的应用

 Graphics对象的DrawIcon()方法的应用,语法结构:

 public void DrawIcon(System.Drawing.Icon icon,int x,int y)

代码
        private void Form6_Paint(object sender, PaintEventArgs e)
        {
            Rectangle[] rects 
= new Rectangle[9];                     //定义Rectangle数组
            Pen p = new Pen(Color.Red, 2);                            //定义画笔
            int index = 0;
            
for (int i = 20; i < 200; i = i + 60)                     //双For循环语句
            {
                
for (int j = 20; j < 200; j = j + 60)
                {
                    e.Graphics.DrawRectangle(p, j, i, 
6060);        //绘制矩形
                    if (index < 3)
                    {
                        rects[index
++= new Rectangle(j, i, 3232);
                    }
                    
else
                    {
                        rects[index
++= new Rectangle(j, i, 6060);
                    }
                }
            }
                                                                     
//定义图标数组,并获取系统图标
            Icon[] icons ={
                             SystemIcons.Application,SystemIcons.Asterisk,SystemIcons.Error,
                             SystemIcons.Exclamation,SystemIcons.Hand,SystemIcons.Information,
                             SystemIcons.Shield,SystemIcons.Warning,SystemIcons.WinLogo
                         };
            
for (int i = 0; i < icons.Length; i++)                  //利用循环绘制图标
            {
                e.Graphics.DrawIcon(icons[i], rects[i]);
            }

        }

 10.动态打开、显示和缩放图像

 OpenFileDialog控件的使用,属性意义:

 Name:用来设置在程序代码中引用控件时使用的名称。

 FileName:用来设置打开对话框的默认文件名。

 InitialDirectory:用来设置对话框的默认初始目录,如果不指定,则显示为当前目录。

 DefaultExt:用来设置对话框默认的文件扩展名。

 Filter:用来设置对话框的文件类型,注意该属性的写法,如(文本类型*.txt)|*.txt。

 Title:用来设置对话框的标题。

 Multiselect:如果该属性为True,则打开对话框允许同时打开多个文件,如果为False,则一次只能打开一个文件。

代码
        private void button1_Click(object sender, EventArgs e)
        {
            
string myname;
            openFileDialog1.Filter 
= "*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
                                                                                 
//设置打开图像的类型
            openFileDialog1.ShowDialog();                                        //"打开"对话框
            myname = openFileDialog1.FileName;
            pictureBox1.Image 
= Image.FromFile(myname);                          //显示打开图像
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
if (pictureBox1.Width >= 50)                                        //当图像的宽度值小于50时,就不能再缩小了
            {
                pictureBox1.Width 
= Convert.ToInt32(pictureBox1.Width * 0.8);
                pictureBox1.Height 
= Convert.ToInt32(pictureBox1.Height * 0.8);
            }
            
else
            {
                MessageBox.Show(
this"图像已是最小,不能再缩小了!",           //提示对话框
                    "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        
private void button3_Click(object sender, EventArgs e)
        {
            
if (pictureBox1.Width < 310)                                        //当图像的宽度值大于310时,就不能再放大了
            {
                pictureBox1.Width 
= Convert.ToInt32(pictureBox1.Width * 1.2);
                pictureBox1.Height 
= Convert.ToInt32(pictureBox1.Height * 1.2);
            }
            
else
            {
                MessageBox.Show(
this"图像已是最大,不能再放大了!",           //提示对话框
                    "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


原文地址:https://www.cnblogs.com/yongfeng/p/1681414.html