.NET C# winform编写简易画图程序

版权:原创 更新时间:1年以上
[该文章底部包含文件资源,可根据自己情况,决定是否下载资源使用,时间>金钱,如有需要,立即查看资源]

编写一个简易画图程序,可以使用.NET C#中的Windows Forms技术来实现。代码示例如下所示:

using System;
using System.Drawing;
using System.Windows.Forms;
public class SimpleDrawingProgram : Form
{
    private Point currentPoint;
    private Pen pen;
    public SimpleDrawingProgram()
    {
        // 设置窗口标题
        Text = "简易画图程序";
        // 创建画笔并初始化其属性
        pen = new Pen(Color.Black, 3);
        pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        // 设置窗口大小及背景颜色
        Size = new Size(600, 400);
        BackColor = Color.White;
        // 注册鼠标事件处理方法
        MouseDown += SimpleDrawingProgram_MouseDown;
        MouseMove += SimpleDrawingProgram_MouseMove;
        MouseUp += SimpleDrawingProgram_MouseUp;
    }
    private void SimpleDrawingProgram_MouseDown(object sender, MouseEventArgs e)
    {
        currentPoint = e.Location;
    }
    private void SimpleDrawingProgram_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            using (Graphics graphics = CreateGraphics())
            {
                graphics.DrawLine(pen, currentPoint, e.Location);
            }
            currentPoint = e.Location;
        }
    }
    private void SimpleDrawingProgram_MouseUp(object sender, MouseEventArgs e)
    {
        currentPoint = Point.Empty;
    }
    static void Main()
    {
        Application.Run(new SimpleDrawingProgram());
    }
}

下面展示一个简单画图程序的基本功能,包含 选择颜色,铅笔,直线,矩形,椭圆,文字,橡皮等功能

鼠标触摸事件:

       private void fMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //如果选择文字输入,则打开strInput窗体
                if (tool == drawTools.String)
                {
                    fTxtCon inputBox = new fTxtCon();
                    inputBox.StartPosition = FormStartPosition.CenterParent;
                    if (inputBox.ShowDialog() == DialogResult.OK)
                    {
                        Graphics g = this.CreateGraphics();
                        Font theFont = this.Font;
                        g.DrawString(inputBox.ztxt.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                        ig.DrawString(inputBox.ztxt.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                    }
                }
                //如果开始绘制,则开始记录鼠标位置
                else if ((isDrawing = !isDrawing) == true)
                {
                    startPoint = new Point(e.X, e.Y);
                    oldPoint = new Point(e.X, e.Y);
                }
            }
        }
       private void fMain_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g;
            g = this.CreateGraphics();

            if (isDrawing)
            {
                switch (tool)
                {
                    case drawTools.None:
                        break;
                    case drawTools.Pen:
                        //从上一个点到当前点绘制线段
                        g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
                        ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                    case drawTools.Line:
                        //首先恢复此次操作之前的图像,然后再添加Line
                        this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
                        break;
                    case drawTools.Ellipse:
                        //首先恢复此次操作之前的图像,然后再添加Ellipse
                        this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case drawTools.Rectangle:
                        //首先恢复此次操作之前的图像,然后再添加Rectangle
                        this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case drawTools.String:
                        break;
                    case drawTools.Rubber:
                        //用背景色绘制宽线段
                        g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
                        ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                }
            }
        }

        private void fMain_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
            switch (tool)
            {
                case drawTools.Line:
                    ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
                    break;
                case drawTools.Ellipse:
                    ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
                case drawTools.Rectangle:
                    ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
            }
        }

        private void fMain_Paint(object sender, PaintEventArgs e)
        {
            //将Image中保存的图像,绘制出来
            Graphics g = this.CreateGraphics();
            if (img != null)
            {
                g.Clear(Color.White);
                g.DrawImage(img, this.ClientRectangle);
            }
        }


附件:下载该文件资源,减少时间成本(增值服务)
不商用,只限学习使用,使用后请删除
上传者拥有该资源完整版权,下载既表明已授权您可以进行报备商用,无报备或者使用后报备,视为侵权,报备后使用,为合法有效使用,报备方式,下载后,在下载列表,点击报备,填写使用场景即可
File Source
.rar
199.48 KB
文件部分展示图(单击放大)
.NET C# winform编写简易画图程序.NET C# winform编写简易画图程序.NET C# winform编写简易画图程序
留言
该资源可下载
File Source
.rar
199.48 KB
.NET C# winform编写简易画图程序.NET C# winform编写简易画图程序.NET C# winform编写简易画图程序
最新结算
股权转让协议意向书模板
类型: .docx 金额: CNY 2.23¥ 状态: 待结算 详细>
股权转让协议意向书模板
类型: .docx 金额: CNY 0.28¥ 状态: 待结算 详细>
CSS3图片向上3D翻转渐隐消失特效
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
CSS3图片向上3D翻转渐隐消失特效
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
.net c# 将金额转人名币大写金额
类型: .rar 金额: CNY 2.39¥ 状态: 待结算 详细>
.net c# 将金额转人名币大写金额
类型: .rar 金额: CNY 0.3¥ 状态: 待结算 详细>
合伙退伙协议书范本模板
类型: .doc 金额: CNY 2.23¥ 状态: 待结算 详细>
合伙退伙协议书范本模板
类型: .doc 金额: CNY 0.28¥ 状态: 待结算 详细>
合伙退伙协议书范本模板
类型: .doc 金额: CNY 2.23¥ 状态: 待结算 详细>
合伙退伙协议书范本模板
类型: .doc 金额: CNY 0.28¥ 状态: 待结算 详细>
我们力求给您提供有用的文章,再此基础上,会附加营收资源,不做任何广告,让平台可以更好发展 若您发现您的权利被侵害,或使用了您的版权,请发邮件联系 sunlifel@foxmail.com ggbig觉得 : 不提供源码的文章不是好文章
合作伙伴
联系我们
  • QQ:21499807
  • 邮箱:sunlifel@foxmail.com
  • QQ扫一扫加QQ
    QQ扫一扫
Copyright 2023-2024 ggbig.com·皖ICP备2023004211号-1
打赏文章