3设计一基于window窗体的登录界面,窗体居中显示。要求用户输入用户名和密码,若登录失败则显示用户名或密码错误,若登录成功则显示第二个窗体。第二个窗体要求能在屏幕上以三种颜色、三种字体输出文字: “GIS开发设计基础—你的姓名”。这里你的姓名要替换成你的名字。
- 第一步:登录界面设计:
- 第二步:绘制字符串界面设计:
一个menustrip,一个textbox,一个picturebox,三个button,一个colorDialog,一个fontDialog
3.第三步:绘制界面代码设计:
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;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace C_shape考核第三题
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Graphics g;//创建画板
Bitmap bmp;//绘制形成的图片
string str;//接收文本框内容
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();//文件保存
List<Color> clist = new List<Color>();//颜色列表
List<Font> flist = new List<Font>();//字体列表
public int spacing = 0;//初始Y坐标
private void btn_Draw_Click(object sender, EventArgs e)
{
bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);//初始化图片
g = Graphics.FromImage(bmp);//初始化画板
g.Clear(Color.White);//情况颜色
str = textBox1.Text;
if(str=="")//如果没有输入指定内容,默认内容输出
{
for (int i = 0; i < clist.Count; i++)//以颜色个数嵌套循环
{
Brush brush = new SolidBrush(clist[i]);//更新画笔颜色
g.DrawString("GIS开发设计基础—唐文健", flist[i], brush, new Point(0, spacing));//(内容,字体,颜色,初始位置)
spacing += 30;//调整Y坐标大小,以达到换行的效果
}
}
else
{
for (int i=0;i<clist.Count;i++)
{
Brush brush = new SolidBrush(clist[i]);
g.DrawString(str, flist[i], brush, new Point(0, spacing));
spacing += 30;
}
}
spacing = 0;
this.pictureBox1.Image = bmp;
g.Dispose();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
//保存图片,可以设置格式
this.SaveFileDialog1.Filter = "(png)|*.png|(jpg)|*.jpg";
if (this.SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
string targetFn = this.SaveFileDialog1.FileName;
bmp.Save(targetFn, ImageFormat.Png);
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
//初始化画板
bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
g = Graphics.FromImage(bmp);
g.Clear(Color.White);
this.pictureBox1.Image = bmp;
g.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
clist.Add(this.colorDialog1.Color);
}
}
private void button2_Click(object sender, EventArgs e)
{
if(this.fontDialog1.ShowDialog()==DialogResult.OK)
{
flist.Add(this.fontDialog1.Font);
}
}
}
}
4.总结:
亮点为自主颜色和字体选择,自主内容输出。