Я пытался использовать следующий код:
pictureBox1.Invalidate();
//or
pictureBox1.Update();
//or
Refresh();
Но он просто ничего не сделал на нем.Я хочу очистить всю нарисованную графику на своем графическом блоке, так как после нажатия на следующую страницу я хочу нарисовать на ней новые прямоугольники.Поэтому, пожалуйста, не предоставляйте мне такое решение:
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height))
или
Graphics.Clear();
Кто-нибудь хочет поделиться решением со мной?Я ценю это.
стр. 2 стр. 3
Form2.cs
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red, 2);
e.Graphics.DrawRectangle(pen, rect);
}
bool draw;
int x_start, y_start;
Rectangle rect;
//UserRect rect;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
pictureBox1.Cursor = Cursors.Cross;
draw = true;
x_start = e.X;
y_start = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
}else if(e.Button == MouseButtons.Right)
{
pictureBox1.Cursor = Cursors.Cross;
}
if (!draw) return;
LocalMousePosition = pictureBox1.PointToClient(Cursor.Position);
int x = Math.Min(x_start, LocalMousePosition.X);
int y = Math.Min(y_start, LocalMousePosition.Y);
int width = Math.Max(x_start, LocalMousePosition.X) - Math.Min(x_start, LocalMousePosition.X);
int height = Math.Max(y_start, LocalMousePosition.Y) - Math.Min(y_start, LocalMousePosition.Y);
rect = new Rectangle(x, y, width, height);
xx = x;
yy = y;
ww = width;
hh = height;
Refresh();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//pictureBox1.Cursor = Cursors.Default;
draw = false;
rectang = new UserRect(rect);
rect = new Rectangle(e.X, e.Y, 0, 0);
rectang.SetPictureBox(this.pictureBox1);
}
else if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Cursor = Cursors.Default;
_tracking = false;
}
}
UserRect.cs
public void Draw(Graphics g)
{
g.DrawRectangle(new Pen(Color.Green), rect);
foreach (PosSizableRect pos in Enum.GetValues(typeof(PosSizableRect)))
{
g.DrawRectangle(new Pen(Color.Red), GetRect(pos));
}
}
public void SetPictureBox(PictureBox p)
{
this.mPictureBox = p;
mPictureBox.MouseDown += new MouseEventHandler(mPictureBox_MouseDown);
mPictureBox.MouseUp += new MouseEventHandler(mPictureBox_MouseUp);
mPictureBox.MouseMove += new MouseEventHandler(mPictureBox_MouseMove);
mPictureBox.Paint += new PaintEventHandler(mPictureBox_Paint);
}
private void mPictureBox_Paint(object sender, PaintEventArgs e)
{
try
{
Draw(e.Graphics);
}
catch (Exception exp)
{
System.Console.WriteLine(exp.Message);
}
}
private void mPictureBox_MouseDown(object sender, MouseEventArgs e)
{
mIsClick = true;
nodeSelected = PosSizableRect.None;
nodeSelected = GetNodeSelectable(e.Location);
if (rect.Contains(new Point(e.X, e.Y)))
{
mMove = true;
}
oldX = e.X;
oldY = e.Y;
}
private void mPictureBox_MouseUp(object sender, MouseEventArgs e)
{
//MessageBox.Show(rect.ToString());
mIsClick = false;
mMove = false;
}
private void mPictureBox_MouseMove(object sender, MouseEventArgs e)
{
ChangeCursor(e.Location);
if (mIsClick == false)
{
return;
}
Rectangle backupRect = rect;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
switch (nodeSelected)
{
case PosSizableRect.LeftUp:
rect.X += e.X - oldX;
rect.Width -= e.X - oldX;
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
case PosSizableRect.LeftMiddle:
rect.X += e.X - oldX;
rect.Width -= e.X - oldX;
break;
case PosSizableRect.LeftBottom:
rect.Width -= e.X - oldX;
rect.X += e.X - oldX;
rect.Height += e.Y - oldY;
break;
case PosSizableRect.BottomMiddle:
rect.Height += e.Y - oldY;
break;
case PosSizableRect.RightUp:
rect.Width += e.X - oldX;
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
case PosSizableRect.RightBottom:
rect.Width += e.X - oldX;
rect.Height += e.Y - oldY;
break;
case PosSizableRect.RightMiddle:
rect.Width += e.X - oldX;
break;
case PosSizableRect.UpMiddle:
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
default:
if (mMove)
{
rect.X = rect.X + e.X - oldX;
rect.Y = rect.Y + e.Y - oldY;
}
break;
}
}
oldX = e.X;
oldY = e.Y;
if (rect.Width < 5 || rect.Height < 5)
{
rect = backupRect;
}
TestIfRectInsideArea();
mPictureBox.Invalidate();
}