【发布时间】:2016-03-20 04:18:27
【问题描述】:
我在网上找到了这段代码,我想用它在我的图纸上画一个蓝色的小盒子。但是当我点击图片框时,会出现蓝色框,但它会重绘我的整个图片框,这是一个问题,因为我有一张非常复杂的图片,重绘需要几秒钟。有什么办法可以绕过无效吗?也许是图片框内的图片框,背面图片框是我的复杂绘图,正面图片框是刷新的蓝色框?我似乎无法让前面的图片框背景颜色透明,有人可以帮我吗?
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determine the initial rectangle coordinates...
if (MeasureMentimported && !selectieBezig)
{
RectStartPoint = e.Location;
//Invalidate();
selectieBezig = true;
}
}
// Draw Rectangle
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//Als er niet links wordt geklikt, dan gewoon weer terug.
if (e.Button != MouseButtons.Left)
return;
//Alleen als de measurementfile is geladen, gaan we kijken of er iets te selecteren valt.
if (MeasureMentimported && selectieBezig)
{
Point tempEndPoint = e.Location;
int X1 = Math.Min(RectStartPoint.X, tempEndPoint.X);
int Y1 = Math.Min(RectStartPoint.Y, tempEndPoint.Y);
int X2 = Math.Max(RectStartPoint.X, tempEndPoint.X);
int Y2 = Math.Max(RectStartPoint.Y, tempEndPoint.Y);
Rect.Location = new Point(X1, Y1);
Rect.Size = new Size(X2 - X1, Y2 - Y1);
picTekenvlak.Invalidate();
//Rect.Location = new Point(
// Math.Min(RectStartPoint.X, tempEndPoint.X),
// Math.Min(0, 1000));
//Rect.Size = new Size(
// Math.Abs(RectStartPoint.X - tempEndPoint.X),
// Math.Abs(1000));
// MessageBox.Show("k");
}
}
// Draw Area
//
private void pictureBox1_Paint1(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (MeasureMentimported)
{
if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
{
e.Graphics.FillRectangle(selectionBrush, Rect);
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
selectieBezig = false;
if (e.Button == MouseButtons.Right)
{
if (Rect.Contains(e.Location))
{
Console.WriteLine("Right click");
}
}
}
【问题讨论】:
-
这里有一些提示可能会有所帮助 1. 您可以尝试将复杂图像缓存到缓冲区中,这样重新绘制不会花费太多时间; 2.据我所知,您需要将Windows(图像主机)背景画笔设置为透明,这样您就可以避免在重新绘制时闪烁(也许您必须覆盖重新绘制的行为)窗户); 3.也许你可以创建另一个矩形窗口而不是在顶部绘制,并将其放在图像容器的顶部
-
要使 winforms 中的控件透明,它必须嵌套在下面的控件中。对于图片框,您需要在代码中执行此操作:
pbox1.parent = pbox0;- 您可以尝试仅使旧矩形和新矩形的外边界无效:invalidate(Rectangle.Union(oldRect, newRect));另外:什么是“复杂”图像??大的?多大?还有:picTekenvlak是什么?
标签: c# picturebox transparent