绘制移动线在C#中的透明面板 [英] Drawing a moving line in a transparent panel in C#
问题描述
有似乎是一个百万个问题在那里这一点,但我无法找到一个会工作。所以,我想它的时候,问题1,000,001。
There seems to be a million questions out there on this, yet I can't find one that will work. So, I guess it's time for question 1,000,001.
我有一个自定义控件图片框
和面板
。在面板
是图片框
的孩子与一个透明背景。这让我TP吸取任何图像中的图片框
加载之上。
I have a custom control with a PictureBox
and a Panel
. The Panel
is the child of PictureBox
with a transparent background. This allows me tp draw on top of whatever image is loaded in the PictureBox
.
制图部作品,但擦除部分没有。如果我用的Invalidate()
我只是得到了一堆闪烁,而行甚至从来没有表演。
The drawing part works, but the erasing part does not. And if I use Invalidate()
I just get a bunch of flickering, and the line never even shows.
如果最终的目标并不明显,它应该像什么像样的绘图应用程序,让您在一个地方点击,拖动,并配合鼠标,直到你放开移动。
If the end goal isn't obvious, it should work like any decent drawing application, where you click in one spot, drag around, and the line moves with the mouse until you let go.
代码:
private void drawLine(Point pt) {
// Erase the last line
if (m_lastPoints != null) {
m_graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
m_graphics.DrawLine(m_transPen, m_lastPoints[0], m_lastPoints[1]);
}
// Set the last points
m_lastPoints = new Point[] { m_mouseStartPoint, pt };
m_graphics.DrawLine(new Pen(m_color), m_mouseStartPoint, pt);
}
m_transPen
定义为新笔(Color.FromArgb(0,0,0,0));
和结果:
现在,如果我将其更改为:
Now, if I change it to:
m_graphics.DrawLine(Pens.White, m_lastPoints[0], m_lastPoints[1]);
我得到这个,这显示了它应该做的事,而不是只用白线,他们应该是透明的。
I get this, which shows what it should be doing, only instead of with white lines, they should be transparent.
推荐答案
无需擦除旧线!只是无效面板
并绘制新鲜的,最好在油漆
事件。
No need to erase the old line! Just invalidate the Panel
and draw the fresh one, best in the Paint
event.
但是作为这项工作,在面板
不能覆盖的图片框
。它必须在啦!将这个负载或构造事件:
But for this to work, the Panel
must not overlay the PictureBox
. It must be inside it! Put this in the load or constructor event :
yourPanel.Parent = yourPictureBox;
yourPanel.Size = yourPictureBox.Size;
yourPanel.Location = Point.Empty;
(我知道你有一个正确的了,但也许旁边的人只看了答案; - )
(I know you got that one right already, but maybe the next person only looks at the answer ;-)
要避免闪烁使用双缓冲面板
:
class DrawPanel : Panel
{
public DrawPanel()
{
DoubleBuffered = true;
}
}
其实后,如果您只想画上加载图像的顶部东西
,你甚至不需要一个单独的面板
。只是借鉴了图片框
本身!它有三个独立的层次:的BackgroundImage
,图片
和控制面
..
Actually, if you only want to draw something on top of the loaded Image
, you don't even need a separate Panel
. Just draw on the PictureBox
itself! It has three independent layers: BackgroundImage
, Image
and the Control surface
..
下面是最少的代码绘制光标控制线:
Here is the minimal code to draw a Cursor controlled line:
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.MouseUp += pictureBox1_MouseUp;
pictureBox1.Paint += pictureBox1_Paint;
// class level
Point mDown = Point.Empty;
Point mCurrent = Point.Empty;
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mDown != Point.Empty) e.Graphics.DrawLine(Pens.White, mDown, mCurrent);
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mDown = Point.Empty;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
mCurrent = e.Location;
pictureBox1.Invalidate();
}
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mDown = e.Location;
}
行,当你松开鼠标按钮消失。
The line disappears when you release the mouse button.
要使其永久有效,你需要它的两点存储在吸引他们,并通过在涂料该名单的工作需要的数据的列表
事件。
To make it permanent you need to store its two points in a list of data needed to draw them and work through that list in the Paint
event.
这名单或许应该还包括颜色,笔宽,然后一些,所以设计一个类的drawAction'将帮助..
That list should probably also include the color, pen width and then some, so designing a class 'drawAction' will help..
这篇关于绘制移动线在C#中的透明面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!