using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GDI绘图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
g = splitContainer1.Panel1.CreateGraphics();
bHaveFirstPoint = false;
this.Text = "简单绘图软件";
}
private void Form1_Load(object sender, EventArgs e)
{
cboSize.Items.Add("5");
cboSize.Items.Add("7");
cboSize.Items.Add("9");
cboSize.Items.Add("11");
cboSize.Items.Add("14");
cboColor.Items.Add("Black");
cboColor.Items.Add("Blue");
cboColor.Items.Add("Coral");
cboColor.Items.Add("Gray");
cboColor.Items.Add("Cyan");
cboColor.Items.Add("DeepPink");
cboSize.Text = "14";
cboColor.Text = "DeepPink";
}
public Point startP;
public Graphics g;
bool bHaveFirstPoint;
private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.splitContainer1.Panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.splitContainer1_Panel1_MouseMove);
startP = e.Location;
bHaveFirstPoint = true;
}
}
private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
Pen myPen = new Pen(Color.FromName(cboColor.Text), Convert.ToInt32(cboSize.Text));
g.DrawLine(myPen, startP, e.Location);
this.splitContainer1.Panel1.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.splitContainer1_Panel1_MouseMove);
bHaveFirstPoint = false;
}
}
private void splitContainer1_Panel1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//定义字体
Font drawFont = new Font("Arial", float.Parse(cboSize.Text));
//定义画刷
SolidBrush drawBrush = new SolidBrush(Color.FromName(cboColor.Text));
//定义字符串格式
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;
//输出文本的左上角座标
float x = e.X;
float y = e.Y;
g.DrawString(txtInfo.Text, drawFont, drawBrush, x, y, drawFormat);
}
}
private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
{
if (cboColor.Text.Length == 0 || cboSize.Text.Length == 0)
{
return;
}
if(bHaveFirstPoint)
{
g.Clear(SystemColors.AppWorkspace);
Pen myPen = new Pen(Color.FromName(cboColor.Text), Convert.ToInt32(cboSize.Text));
g.DrawLine(myPen, startP, e.Location);
}
}
}
}