📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 鼠标事件
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.TrackBar trackBar2;
private System.Windows.Forms.TrackBar trackBar3;
Bitmap bmp=new Bitmap(432,304);
int oldx,oldy;
int point;
Color pen_c;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.trackBar2 = new System.Windows.Forms.TrackBar();
this.trackBar3 = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar3)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(32, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(152, 136);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Location = new System.Drawing.Point(24, 168);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(496, 216);
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
//
// trackBar1
//
this.trackBar1.BackColor = System.Drawing.Color.Red;
this.trackBar1.Location = new System.Drawing.Point(200, 16);
this.trackBar1.Maximum = 255;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(296, 45);
this.trackBar1.TabIndex = 2;
this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.Both;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// trackBar2
//
this.trackBar2.BackColor = System.Drawing.Color.Green;
this.trackBar2.Location = new System.Drawing.Point(200, 56);
this.trackBar2.Maximum = 255;
this.trackBar2.Name = "trackBar2";
this.trackBar2.Size = new System.Drawing.Size(296, 45);
this.trackBar2.TabIndex = 3;
this.trackBar2.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// trackBar3
//
this.trackBar3.BackColor = System.Drawing.Color.Blue;
this.trackBar3.Location = new System.Drawing.Point(200, 104);
this.trackBar3.Maximum = 255;
this.trackBar3.Name = "trackBar3";
this.trackBar3.Size = new System.Drawing.Size(296, 45);
this.trackBar3.TabIndex = 4;
this.trackBar3.TickStyle = System.Windows.Forms.TickStyle.Both;
this.trackBar3.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 419);
this.Controls.Add(this.trackBar3);
this.Controls.Add(this.trackBar2);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar3)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
Graphics g=Graphics.FromImage(bmp);
Pen p=new Pen(pen_c,point);
g.DrawLine(p,oldx,oldy,e.X,e.Y);
pictureBox2.Image=bmp;
oldx=e.X;
oldy=e.Y;
}
}
private void trackBar1_Scroll(object sender, System.EventArgs e)
{
pictureBox1.BackColor=Color.FromArgb(trackBar1.Value,trackBar2.Value,trackBar3.Value);
pen_c=Color.FromArgb(trackBar1.Value,trackBar2.Value,trackBar3.Value);
}
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.BackColor=Color.FromArgb(0,0,0);
Graphics g=Graphics.FromImage(bmp);
pen_c=Color.Black;
point=3;
g.Clear(Color.White);
pictureBox2.Image=bmp;
pictureBox2.Refresh();
}
private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
oldx=e.X;
oldy=e.Y;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -