⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 没事时做的一个简单的画图小项目
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

namespace hutu
{
    enum drawstyle
    {
        pen,
        line,
        circle,
        rect,
        fcircle,
        frect,
        rub
    };
    public partial class Form1 : Form
    {
        drawstyle ds;
        SolidBrush bs;//定义画刷
        Pen rub;
       
        Bitmap bit;
        Graphics g;
        Pen p;
        Color c;
        string filename;
        int x;
        int y;
        bool isdown;//判断是否按下鼠标
        Form2 f = new Form2();
        bool ischange;//记录内容是否改变
        public Form1()
        {
            InitializeComponent();
            this.Text = "未命名--画图";
            filename = "";
            ischange = false;//初始化为否
            c = Color.Tomato;
            bs = new SolidBrush(c);//初始化画刷
            rub = new Pen(Color.White,20);//初始化橡皮
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");//图标
            //初始化画布
            bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
           this.pictureBox1.Image = bit;
            //bit = new Bitmap(100, 200);
            g = Graphics.FromImage(bit);
            this.pictureBox1.Image = bit;
            p = new Pen(Color.DarkGreen, 2);//初始化画笔颜色
            this.ds =drawstyle.pen;//默认为画笔
        }
        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {//打开
            this.Text = "未命名--画图";
            filename = this.Text;
            filename = "";
                this.openFileDialog1.FileName = "";
                this.openFileDialog1.Filter = "bmp files(*.bmp)|*.bmp";
                if (this.ischange == true)
                {
                    DialogResult result = MessageBox.Show(filename + "的内容已经改变" + '\n' + "想保存文件吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                    if (result == DialogResult.OK)
                    {                    
                        this.saveFileDialog1.Filter = "bmp files(*.bmp)|*.bmp";
                        this.saveFileDialog1.FileName = filename;
                        this.saveFileDialog1.ShowDialog();
                        this.openFileDialog1.Filter = "bmp files(*.bmp)|*.bmp";
                        this.openFileDialog1.ShowDialog();
                    }
                    else
                    {
                        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            this.filename = this.openFileDialog1.FileName;
                            bit = new Bitmap(filename);
                            this.pictureBox1.Width = bit.Width;
                            this.pictureBox1.Height = bit.Height;
                            this.pictureBox1.Image = bit;
                            g = Graphics.FromImage(bit);
                        }
                    }
                   
                    }
                else
                    {
                        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            this.filename = this.openFileDialog1.FileName;
                            bit = new Bitmap(filename);
                            this.pictureBox1.Width = bit.Width;
                            this.pictureBox1.Height = bit.Height;
                            this.pictureBox1.Image = bit;
                            g = Graphics.FromImage(bit);
                        }
                }

            }
      
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {//鼠标按下
            this.x = e.X;
            this.y = e.Y;
            this.isdown = true;

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {//鼠标移动
            if (isdown)
            {
                switch (this.ds)
                {
                    case drawstyle.pen:

                        g.DrawLine(p, x, y, e.X, e.Y);
                        x = e.X;
                        y = e.Y;
                        g.Save();
                        this.pictureBox1.Image = bit;
                        break;

                }
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {//鼠标弹起
            ischange = true;
            this.isdown = false;
            switch (this.ds)
            {
                case drawstyle.line:
                    g.DrawLine(p, x, y, e.X, e.Y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
                case drawstyle.circle:
                    g.DrawEllipse(p, x, y, e.X, e.Y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
                case drawstyle.rect:
                    g.DrawRectangle(p, x, y, e.X, e.Y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
                case drawstyle.fcircle:
                    g.FillEllipse(bs, x, y, e.X - x, e.Y - y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
                case drawstyle.frect:
                    g.FillRectangle(bs, x, y, e.X - x, e.Y - y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
                case drawstyle.rub:

                    g.DrawLine(rub, x, y, e.X, e.Y);
                    g.Save();
                    this.pictureBox1.Image = bit;
                    break;
            }
        }

        private void editColorToolStripMenuItem_Click(object sender, EventArgs e)
        {//编辑颜色
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.c = this.colorDialog1.Color;
                this.p.Color = c;
                this.bs.Color = c;

            }
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {//新建
            this.Text = "未命名--画图";
            filename = this.Text;
            if (this.f.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.Width = f.wo;
                this.pictureBox1.Height = f.ho;
                bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                g = Graphics.FromImage(bit);
                this.pictureBox1.Image = bit;
            }
            if (this.ischange == true)
            {
                DialogResult result = MessageBox.Show(filename + "的内容已经改变" + '\n' + "想保存文件吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    this.openFileDialog1.Filter = "bmp files(*.bmp)|*.bmp";
                    this.saveFileDialog1.FileName = filename;
                    this.saveFileDialog1.ShowDialog();
                }
                else
                {
                    if (this.f.ShowDialog() == DialogResult.OK)
                    {
                        this.pictureBox1.Width = f.wo;
                        this.pictureBox1.Height = f.ho;
                        bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                        g = Graphics.FromImage(bit);
                        this.pictureBox1.Image = bit;
                    }
                }

            }
            ischange = false;
            filename = "";
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {//自由画
            this.ds = drawstyle.pen;

        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {//直线
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");
            this.ds = drawstyle.line;
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {//空心圆
            this.ds = drawstyle.circle;
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {//橡皮
            this.ds = drawstyle.rub;
            this.pictureBox1.Cursor = new Cursor("BULLSEYE.cur");
            


        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {//空心矩形
            this.ds = drawstyle.rect;
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {//实心椭圆
            

            this.ds = drawstyle.fcircle;
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {//实心矩形
            this.ds = drawstyle.frect;
            this.pictureBox1.Cursor = new Cursor("pen_i.cur");
        }

        private void smallToolStripMenuItem_Click(object sender, EventArgs e)
        {//细笔
            this.p.Width = 2;
        }

       

        private void regularToolStripMenuItem_Click(object sender, EventArgs e)
        {//中笔
            this.p.Width = 4;
        }

        private void bigToolStripMenuItem1_Click(object sender, EventArgs e)
        {//粗笔
            this.p.Width = 6;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {//保存
           
            if (filename == "")
            {
                saveFileDialog1.FileName = "未命名";
                saveFileDialog1.Filter = "位图文件(*.bmp)|*.bmp";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName;
                    this.Text = "画图--" + filename;
                    pictureBox1.Image.Save(filename);

                }
            else
            {
                pictureBox1.Image.Save(filename);
                ischange = false;
            }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {//另存为
            saveFileDialog1.FileName = "未命名";
            saveFileDialog1.Filter = "位图文件(*.bmp)|*.bmp";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = saveFileDialog1.FileName;
                this.Text =filename;
              this.pictureBox1.Image.Save(filename);

            }
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }

        private void preprintToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string mes = "打印之前必须已经安装打印机." + '\n' + "要安装打印机,请单击'开始',指向'设置',选定'打印机',然后再双击'安装打印机'";
            printPreviewDialog1.Document = this.printDocument1;
            try
            {
                printPreviewDialog1.ShowDialog();
            }
            catch
            {
                MessageBox.Show(mes, "打印错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -