新手求教几个用 C# 制作画板遇到的 bug
我想问一下,1:为什么我做的画板最小化后画的图就没有了2:当第二点的X或Y坐标大于第一点时,矩形无法绘制
3:我的左右键都响应绘图,我想改为只左键响应
一点分数聊表谢意啊
这是我的代码:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Pen MyPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
PointF pStart;
PointF pEnd;
PointF pWork;
Boolean working;
Boolean brushmode;
String dmode = "line";
void DrawShape(Graphics e,PointF startp,PointF endp,Boolean brushmode,Pen penmode,String domde)
{
Rectangle rect1 = new Rectangle((int)startp.X,(int)startp.Y,
(int)(endp.X- startp.X),(int)(endp.Y - startp.Y));
switch(dmode)
{
case"line":
e.DrawLine(penmode, startp.X,startp.Y,endp.X,endp.Y);
break;
case"rect":
if(brushmode== true) e.FillRectangle(penmode.Brush,rect1);
else e.DrawRectangle(penmode,rect1);
break;
case"circle":
if(brushmode== true) e.FillEllipse(penmode.Brush,rect1);
else e.DrawEllipse(penmode, rect1);
break;
default:break;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Drawing2D.HatchBrush brush2 = new
System.Drawing.Drawing2D.HatchBrush(
System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal,
Color.Brown);
}
private void BtnSetColor_Click(object sender, System.EventArgs e)
{
ColorDialog1.ShowDialog();
//将窗体的背景颜色设置为选定的颜色
BtnSetColor.BackColor = ColorDialog1.Color;
}
private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
working = true;
pStart.X = e.X;
pStart.Y = e.Y;
toolStripStatusLabel1.Text = "[" + e.X + "," + e.Y + "]";
}
private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(working== true)
{
pWork.X = e.X;
pWork.Y = e.Y;
toolStripStatusLabel1.Text = "[" + e.X + "," + e.Y + "]";
}
}
private void PictureBox1_MouseUp(object sender,System.Windows.Forms.MouseEventArgs e)
{
if(working == true)
{
pEnd.X= e.X;
pEnd.Y= e.Y;
MyPen.Color= BtnSetColor.BackColor;
MyPen.Width = (int)NumericUpDown1.Value;
brushmode = CheckBox1.Checked;
if(RadioButton1.Checked == true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
if(RadioButton2.Checked ==true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
if(RadioButton3.Checked ==true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if(RadioButton4.Checked ==true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
if(RadioButton5.Checked ==true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
if(RadioButton6.Checked ==true)
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
if(RadioButton7.Checked ==true)
dmode = "line";
if(RadioButton8.Checked ==true)
dmode = "circle";
if(RadioButton9.Checked ==true)
dmode = "rect";
if(RadioButton10.Checked ==true)
MyPen.Brush = new
System.Drawing.Drawing2D.HatchBrush(
System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal,
MyPen.Color);
if(RadioButton11.Checked ==true)
MyPen.Brush = new
System.Drawing.Drawing2D.HatchBrush(
System.Drawing.Drawing2D.HatchStyle.Cross,MyPen.Color);
if(RadioButton12.Checked == true)
MyPen.Brush = new SolidBrush(MyPen.Color);
DrawShape(PictureBox1.CreateGraphics(),
pStart,pEnd,brushmode,MyPen,dmode);
working = false;
}
}
private void BtnClear_Click(object sender,System. EventArgs e)
{
PictureBox1.CreateGraphics().Clear(BtnSetColor.BackColor);
}
private void PictureBox1_Click(object sender, EventArgs e)
{
}
private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void ToolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
}
}