当前位置:首页C# > 正文

【内部资料】C#winform画笔代码

作者:野牛程序员:2024-01-07 10:02:17C#阅读 2563

画笔代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PPTorPDF
{
    public partial class PenForm : Form
    {
        private bool isDrawing = false;
        private Point lastPoint;
        
        private Bitmap drawingBitmap;
        private Pen currentPen = new Pen(Color.Black, 5) { LineJoin = LineJoin.Round };



        public class NewPanel : Panel
        {
            public NewPanel()
            {
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.UserPaint, true);
            }
        }


        private GraphicsPath drawingPath = new GraphicsPath();

        

        private NewPanel panel6;


        public PenForm()
        {
            InitializeComponent();


            panel6 = new NewPanel();
           // panel6.Width = panel1.Width;
           // panel6.Height = panel1.Height;
            panel6.Width = Screen.PrimaryScreen.Bounds.Width;
            panel6.Height = Screen.PrimaryScreen.Bounds.Height;

            //panel6.BackColor = Color.Blue;
            panel6.BackColor = panel1.BackColor;

            panel6.Paint += panel6_Paint;  // 将panel6_Paint事件与panel6控件的Paint事件关联
            panel6.MouseMove += panel6_MouseMove;
            panel6.MouseUp += panel6_MouseUp;
            panel6.MouseDown += panel6_MouseDown;


            panel1.Controls.Add(panel6);

         //   drawingBitmap = new Bitmap(panel1.Width, panel1.Height);
            // 设置 drawingBitmap 为屏幕的宽度和高度
            drawingBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            InitializeColorComboBox();

            colorComboBox.SelectedIndex = 0;
            trackBar1.Value = 4;

        }

        private void InitializeColorComboBox()
        {
            // 添加颜色到colorComboBox

            colorComboBox.Items.Add("White");
            colorComboBox.Items.Add("Red");
            colorComboBox.Items.Add("Blue");
            colorComboBox.Items.Add("Green");
            colorComboBox.Items.Add("Yellow");
            colorComboBox.Items.Add("Black"); // 用于橡皮擦
            colorComboBox.SelectedIndex = 0; // 默认选择第一个颜色
        }

    
        private void panel6_Paint(object sender, PaintEventArgs e)
        {
            using (Graphics g = Graphics.FromImage(drawingBitmap))
            {
                // 绘制整个路径
                g.DrawPath(currentPen, drawingPath);
            }

            e.Graphics.DrawImage(drawingBitmap, Point.Empty);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
          
        }
        private void panel6_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            lastPoint = e.Location;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
          
        }

        private void panel6_MouseMove(object sender, MouseEventArgs e)
        {          

            if (isDrawing)
            {
                using (Graphics g = Graphics.FromImage(drawingBitmap))
                {
                    if (currentPen.Color == Color.Black)
                    {
                        g.FillEllipse(Brushes.Black, e.X - 10, e.Y - 10, 20, 20);
                    }
                    else
                    {
                        g.DrawLine(currentPen, lastPoint, e.Location);
                    }
                }
                lastPoint = e.Location;
                panel6.Invalidate();
                //    panel1.Invalidate(new Rectangle(lastPoint.X - 10, lastPoint.Y - 10, 20, 20));
                //    panel1.Invalidate(new Rectangle(lastPoint.X - 22, lastPoint.Y - 22, 30, 30));
                //   panel1.Invalidate(new Rectangle(lastPoint.X - 30, lastPoint.Y - 30, 60, 60));
                //   panel6.Invalidate(new Rectangle(lastPoint.X - 35, lastPoint.Y - 35, 105, 105));

                //    panel1.Invalidate(new Rectangle(lastPoint.X - 46, lastPoint.Y - 46, 100, 100));
                // drawingPath.AddEllipse(e.X - 30, e.Y - 30, 60, 60);

                //  this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);


            }

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
          
        }

        private void panel6_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }

        private void EraserButton_Click(object sender, EventArgs e)
        {
            // 切换为橡皮擦,颜色设为黑色
            currentPen.Color = Color.Black;
            // 设置 colorComboBox 选中项为黑色
            colorComboBox.SelectedItem = "Black";

            // 更改鼠标指针为块状
            Cursor = Cursors.Cross; // 你也可以选择其他合适的 Cursors 枚举值
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
           

            // 清除画板
            drawingPath.Reset();
            using (Graphics g = Graphics.FromImage(drawingBitmap))
            {
                g.Clear(Color.Black);
            }
         
            panel6.Invalidate();
            // 切换到其他模式,恢复鼠标指针为默认形状
            Cursor = Cursors.Default;
            // 设置 colorComboBox 选中项为黑色
            colorComboBox.SelectedItem = "White";
        }

        private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 更新画笔颜色
            if (colorComboBox.SelectedItem != null)
            {
                currentPen.Color = Color.FromName(colorComboBox.SelectedItem.ToString());
                if (colorComboBox.SelectedItem != "Black")
                {
                    // 切换到其他模式,恢复鼠标指针为默认形状
                    Cursor = Cursors.Default;
                }
            }

        }

        private void colorComboBox_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Click(object sender, EventArgs e)
        {
            // 切换到其他模式,恢复鼠标指针为默认形状
            Cursor = Cursors.Default;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            // 滑块控件滑动时设置画笔的粗细
            SetPenThickness(trackBar1.Value);
        }

        private void SetPenThickness(int thickness)
        {
            currentPen.Width = thickness;
        }
    }
}

winform窗体代码

namespace PPTorPDF
{
    partial class PenForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            panel1 = new Panel();
            EraserButton = new Button();
            ClearButton = new Button();
            colorComboBox = new ComboBox();
            panel2 = new Panel();
            label2 = new Label();
            label1 = new Label();
            trackBar1 = new TrackBar();
            panelM = new Panel();
            panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)trackBar1).BeginInit();
            panelM.SuspendLayout();
            SuspendLayout();
            // 
            // panel1
            // 
            panel1.BackColor = SystemColors.Desktop;
            panel1.Dock = DockStyle.Fill;
            panel1.Location = new Point(0, 0);
            panel1.Name = "panel1";
            panel1.Size = new Size(2330, 1056);
            panel1.TabIndex = 0;
            panel1.MouseDown += panel1_MouseDown;
            panel1.MouseMove += panel1_MouseMove;
            panel1.MouseUp += panel1_MouseUp;
            // 
            // EraserButton
            // 
            EraserButton.Location = new Point(31, 3);
            EraserButton.Name = "EraserButton";
            EraserButton.Size = new Size(112, 34);
            EraserButton.TabIndex = 1;
            EraserButton.Text = "橡皮擦";
            EraserButton.UseVisualStyleBackColor = true;
            EraserButton.Click += EraserButton_Click;
            // 
            // ClearButton
            // 
            ClearButton.Location = new Point(149, 3);
            ClearButton.Name = "ClearButton";
            ClearButton.Size = new Size(112, 34);
            ClearButton.TabIndex = 2;
            ClearButton.Text = "清除画板";
            ClearButton.UseVisualStyleBackColor = true;
            ClearButton.Click += ClearButton_Click;
            // 
            // colorComboBox
            // 
            colorComboBox.FormattingEnabled = true;
            colorComboBox.Location = new Point(369, 6);
            colorComboBox.Name = "colorComboBox";
            colorComboBox.Size = new Size(182, 32);
            colorComboBox.TabIndex = 3;
            colorComboBox.SelectedIndexChanged += colorComboBox_SelectedIndexChanged;
            // 
            // panel2
            // 
            panel2.Controls.Add(label2);
            panel2.Controls.Add(label1);
            panel2.Controls.Add(trackBar1);
            panel2.Controls.Add(EraserButton);
            panel2.Controls.Add(colorComboBox);
            panel2.Controls.Add(ClearButton);
            panel2.Dock = DockStyle.Top;
            panel2.Location = new Point(0, 0);
            panel2.Name = "panel2";
            panel2.Size = new Size(2330, 66);
            panel2.TabIndex = 4;
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Location = new Point(580, 13);
            label2.Name = "label2";
            label2.Size = new Size(100, 24);
            label2.TabIndex = 6;
            label2.Text = "画笔粗细:";
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new Point(281, 10);
            label1.Name = "label1";
            label1.Size = new Size(82, 24);
            label1.TabIndex = 5;
            label1.Text = "画笔颜色";
            // 
            // trackBar1
            // 
            trackBar1.LargeChange = 10;
            trackBar1.Location = new Point(677, 13);
            trackBar1.Maximum = 20;
            trackBar1.Name = "trackBar1";
            trackBar1.Size = new Size(354, 69);
            trackBar1.TabIndex = 4;
            // 
            // panelM
            // 
            panelM.BackColor = SystemColors.ActiveCaption;
            panelM.Controls.Add(panel2);
            panelM.Controls.Add(panel1);
            panelM.Dock = DockStyle.Fill;
            panelM.Location = new Point(0, 0);
            panelM.Name = "panelM";
            panelM.Size = new Size(2330, 1056);
            panelM.TabIndex = 5;
            // 
            // PenForm
            // 
            AutoScaleDimensions = new SizeF(11F, 24F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(2330, 1056);
            Controls.Add(panelM);
            DoubleBuffered = true;
            Name = "PenForm";
            StartPosition = FormStartPosition.CenterParent;
            Text = "画板-野牛程序员";
            panel2.ResumeLayout(false);
            panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)trackBar1).EndInit();
            panelM.ResumeLayout(false);
            ResumeLayout(false);
        }

        #endregion

        private Panel panel1;
        private Button EraserButton;
        private Button ClearButton;
        private ComboBox colorComboBox;
        private Panel panel2;
        private Panel panelM;
        private TrackBar trackBar1;
        private Label label1;
        private Label label2;
    }
}


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击