1. 程式人生 > >C# GUI+開發一個簡易畫板

C# GUI+開發一個簡易畫板

利用C#的視窗程式開發一個簡易畫板

父窗體

在這裡插入圖片描述

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;

namespace Sketchpad
{
    public partial class Form1
: Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { 儲存SToolStripMenuItem.Enabled = false; this.timer1.Interval = 1000; this.timer1.Enabled = true; IsMdiContainer =
true; 垂直平鋪ToolStripMenuItem.Enabled = false; 水平平鋪ToolStripMenuItem.Enabled = false; 疊加ToolStripMenuItem.Enabled = false; 重排ToolStripMenuItem.Enabled = false; } private void 開啟OToolStripMenuItem_Click(object sender, EventArgs e) {
OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "開啟檔案"; ofd.Filter = "圖片檔案(*.jpg;*.png)|*.jpg;*.png|所有檔案(*.*)|*.*"; if(ofd.ShowDialog()==DialogResult.OK) { Form2 form = new Form2(); form.MdiParent = this; form.Show(); form.Text = ofd.FileName; string imgPath = ofd.FileName; form.pictureBox1.Load(imgPath); if (ofd.FileName != "") { 儲存SToolStripMenuItem.Enabled = true; 垂直平鋪ToolStripMenuItem.Enabled = true; 水平平鋪ToolStripMenuItem.Enabled = true; 疊加ToolStripMenuItem.Enabled = true; 重排ToolStripMenuItem.Enabled = true; } } } private void 儲存SToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "儲存檔案"; sfd.Filter = "圖片檔案(*.jpg)|*.jpg|所有檔案(*.*)|*.*"; sfd.ShowDialog(); } private void 新建NToolStripMenuItem_Click(object sender, EventArgs e) { Form2 form = new Form2(); form.MdiParent = this; form.Show(); 儲存SToolStripMenuItem.Enabled = true; 垂直平鋪ToolStripMenuItem.Enabled = true; 水平平鋪ToolStripMenuItem.Enabled = true; 疊加ToolStripMenuItem.Enabled = true; 重排ToolStripMenuItem.Enabled = true; } private void 垂直平鋪ToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); } private void 水平平鋪ToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileHorizontal); } private void 疊加ToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.Cascade); } private void 重排ToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.ArrangeIcons); } private void 關於AToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("製作者:"); } private void 顏色CToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog color = new ColorDialog(); color.ShowDialog(); } private void toolStripButton1_Click(object sender, EventArgs e) { 開啟OToolStripMenuItem_Click(sender, e); } private void toolStripButton2_Click(object sender, EventArgs e) { 儲存SToolStripMenuItem_Click(sender, e); } private void toolStripButton3_Click(object sender, EventArgs e) { 新建NToolStripMenuItem_Click(sender, e); } private void 工具欄TToolStripMenuItem_Click(object sender, EventArgs e) { 工具欄TToolStripMenuItem.Checked = !工具欄TToolStripMenuItem.Checked; toolStrip1.Visible = !toolStrip1.Visible; } private void 狀態條SToolStripMenuItem_Click(object sender, EventArgs e) { 狀態條SToolStripMenuItem.Checked = !狀態條SToolStripMenuItem.Checked; statusStrip1.Visible = !statusStrip1.Visible; } private void timer1_Tick_1(object sender, EventArgs e) { toolStripStatusLabel1.Text = "時間:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); this.statusStrip1.Refresh(); } private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild == null) { Application.Exit(); } else { this.ActiveMdiChild.Close(); Application.Exit(); } } } }
子窗體

在這裡插入圖片描述

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;

namespace Sketchpad
{
    public partial class Form2 : Form
    {
        private int pictype = 0;
        private Point orgP, endP, mP;
        public Pen myPen = new Pen(Color.Black, 3);
        Graphics g;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Left)
            {
                orgP.X = e.X;
                orgP.Y = e.Y;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                g = this.pictureBox1.CreateGraphics();
                endP.X = e.X;
                endP.Y = e.Y;
                if (pictype == 4)
                {
                    g.DrawLine(myPen, mP, endP);
                    mP = endP;
                }
                if (pictype != 4)
                {
                    pictureBox1.Refresh();
                }
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            switch (pictype)
            {
                case 1:
                    e.Graphics.DrawLine(myPen, orgP, endP);//直線
                    break;
                case 2:
                    Rectangle rectangle = new Rectangle(orgP.X, orgP.Y, System.Math.Abs(endP.X), System.Math.Abs(endP.Y));//矩形
                    e.Graphics.DrawRectangle(myPen, rectangle);
                    break;
                case 3:
                    Rectangle Rec = new Rectangle(orgP.X, orgP.Y, System.Math.Abs(endP.X), System.Math.Abs(endP.Y));//橢圓
                    e.Graphics.DrawEllipse(myPen, Rec);
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictype = 1;
            pictureBox1.Cursor = Cursors.Cross;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictype = 2;
            pictureBox1.Cursor = Cursors.Cross;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pictype = 3;
            pictureBox1.Cursor = Cursors.Cross;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pictype = 4;
            pictureBox1.Cursor = Cursors.Cross;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            pictype = 5;
            pictureBox1.Cursor = Cursors.Cross;
        }
    }
}