我的面板上有几层图纸
I have several layers of drawings on my panel
我创建了一个面板,并使用 DrawString 和 DrawRectangle 在上面放置了文本。要绘制面板,我使用了 Paint 事件和 Invalidate 方法,但是当调用 Invalidate 时,我得到以下结果(背景颜色透明):My panel with drawstring
正如你所看到的,文本被绘制出来了,但它后面的内容并没有消失,就好像 Invalidate 已经完成了一半的工作。
我已将 DoubleBuffered 设置为 true 并尝试 Graphics.Clear 但没有成功。
有人可以帮助我吗?
有代码:
int x = 0;
int y = arrowUpHeight;
int traceIndex = 0;
lock (_lockDraw)
{
while (y < Height)
{
rect = new Rectangle(x, y, Width, 20);
r = new Rectangle(x, y, rect.Width + 1, rect.Height + 1);
bufferGraphics.SetClip(r);
if (traceIndex < traceList.Count)
{
if (traceIndex < numberOfTraces)
{
Line data = traceList[traceIndex];
if (traceIndex % 2 == 0)
{
bufferGraphics.FillRectangle(brushEven, rect);
}
else
{
bufferGraphics.FillRectangle(brushOdd, rect);
}
if (y < mousePositionY && mousePositionY < y + 20)
{
traceSelected = true;
traceList[traceIndex].Selected = true;
listLinesVisible[traceIndex].Selected = true;
listLines[listLines.IndexOf(traceList[traceIndex])].Selected = true;
indexRightClick = traceIndex;
}
else
{
if (listLines.IndexOf(traceList[traceIndex]) < listLines.Count && listLines.IndexOf(traceList[traceIndex]) >= 0)
{
traceList[traceIndex].Selected = false;
listLinesVisible[traceIndex].Selected = false;
listLines[listLines.IndexOf(traceList[traceIndex])].Selected = false;
}
}
if (traceList[traceIndex].Message.ToUpper().Contains(textBoxRUISearch.MyTextBox.Text.ToUpper()) && textBoxRUISearch.MyTextBox.Text != String.Empty)
{
bufferGraphics.FillRectangle(searchBrush, rect);
}
if (traceList[traceIndex].Selected)
{
//doesn't work correctly for the moment when scrolling
bufferGraphics.FillRectangle(selectedBrush, rect);
bufferGraphics.DrawString(data.Date, traceFont, selectedStringBrush, xTime, y);
bufferGraphics.DrawString(data.LevelString, traceFont, selectedStringBrush, xLevel, y);
bufferGraphics.DrawString(data.Source, traceFont, selectedStringBrush, xSource, y);
if (data.Attachment != null)
{
bufferGraphics.DrawString("Attachment : ", traceFont, selectedStringBrush, xMessage, y);
bufferGraphics.DrawString(data.Message, traceFont, selectedStringBrush, xMessage + (xMessage / 5), y);
}
else
bufferGraphics.DrawString(data.Message, traceFont, selectedStringBrush, xMessage, y);
}
else
{
bufferGraphics.DrawString(data.Date, traceFont, stringBrush, xTime, y);
// Info = 1, Warning = 2, Error = 3, Debug = 5, Special = 6
switch (data.LevelString)
{
case "Info":
bufferGraphics.DrawString(data.LevelString, traceFont, brushInfo, xLevel, y);
break;
case "Warning":
bufferGraphics.DrawString(data.LevelString, traceFont, brushWarning, xLevel, y);
break;
case "Error":
bufferGraphics.DrawString(data.LevelString, traceFont, brushError, xLevel, y);
break;
case "Debug":
bufferGraphics.DrawString(data.LevelString, traceFont, brushDebug, xLevel, y);
break;
default:
break;
}
bufferGraphics.DrawString(data.Source, traceFont, stringBrush, xSource, y);
if (data.Attachment != null)
{
bufferGraphics.DrawString("Attachment : ", traceFont, brushAttachment, xMessage, y);
bufferGraphics.DrawString(data.Message, traceFont, stringBrush, xMessage + (xMessage / 5), y);
}
else
bufferGraphics.DrawString(data.Message, traceFont, stringBrush, xMessage, y);
}
bufferGraphics.DrawRectangle(Pens.LightGray, rect);
//
// if (data.selected)
// {
// Color color = Color.FromArgb(100, 0, 0, 255);
// Brush brush = new SolidBrush(color);
// _bufferGraphics.FillRectangle(brush, rect);
// }
}
else
{
if (listLines.ElementAtOrDefault(listLines.Count - 1) != null)
listLines.RemoveAt(listLines.Count - 1);
if (listLinesVisible.ElementAtOrDefault(listLinesVisible.Count - 1) != null)
listLinesVisible.RemoveAt(listLinesVisible.Count - 1);
y -= 20;
}
}
else
bufferGraphics.FillRectangle(brushUnderTraces, rect);
y += 20;
traceIndex++;
}
}
this.panelTraces.Invalidate();
其中 bufferGraphics 是我的 Graphics 对象。
好的,我找到了解决方案:
我用了一种方法,然后我画了。我在 Paint 中设置了我的方法主体,它工作正常。我将 bufferGraphics 替换为 e.Graphics.
感谢那些试图帮助我的人(我的英语很糟糕所以恭喜你理解我)。
我创建了一个面板,并使用 DrawString 和 DrawRectangle 在上面放置了文本。要绘制面板,我使用了 Paint 事件和 Invalidate 方法,但是当调用 Invalidate 时,我得到以下结果(背景颜色透明):My panel with drawstring
正如你所看到的,文本被绘制出来了,但它后面的内容并没有消失,就好像 Invalidate 已经完成了一半的工作。
我已将 DoubleBuffered 设置为 true 并尝试 Graphics.Clear 但没有成功。
有人可以帮助我吗?
有代码:
int x = 0;
int y = arrowUpHeight;
int traceIndex = 0;
lock (_lockDraw)
{
while (y < Height)
{
rect = new Rectangle(x, y, Width, 20);
r = new Rectangle(x, y, rect.Width + 1, rect.Height + 1);
bufferGraphics.SetClip(r);
if (traceIndex < traceList.Count)
{
if (traceIndex < numberOfTraces)
{
Line data = traceList[traceIndex];
if (traceIndex % 2 == 0)
{
bufferGraphics.FillRectangle(brushEven, rect);
}
else
{
bufferGraphics.FillRectangle(brushOdd, rect);
}
if (y < mousePositionY && mousePositionY < y + 20)
{
traceSelected = true;
traceList[traceIndex].Selected = true;
listLinesVisible[traceIndex].Selected = true;
listLines[listLines.IndexOf(traceList[traceIndex])].Selected = true;
indexRightClick = traceIndex;
}
else
{
if (listLines.IndexOf(traceList[traceIndex]) < listLines.Count && listLines.IndexOf(traceList[traceIndex]) >= 0)
{
traceList[traceIndex].Selected = false;
listLinesVisible[traceIndex].Selected = false;
listLines[listLines.IndexOf(traceList[traceIndex])].Selected = false;
}
}
if (traceList[traceIndex].Message.ToUpper().Contains(textBoxRUISearch.MyTextBox.Text.ToUpper()) && textBoxRUISearch.MyTextBox.Text != String.Empty)
{
bufferGraphics.FillRectangle(searchBrush, rect);
}
if (traceList[traceIndex].Selected)
{
//doesn't work correctly for the moment when scrolling
bufferGraphics.FillRectangle(selectedBrush, rect);
bufferGraphics.DrawString(data.Date, traceFont, selectedStringBrush, xTime, y);
bufferGraphics.DrawString(data.LevelString, traceFont, selectedStringBrush, xLevel, y);
bufferGraphics.DrawString(data.Source, traceFont, selectedStringBrush, xSource, y);
if (data.Attachment != null)
{
bufferGraphics.DrawString("Attachment : ", traceFont, selectedStringBrush, xMessage, y);
bufferGraphics.DrawString(data.Message, traceFont, selectedStringBrush, xMessage + (xMessage / 5), y);
}
else
bufferGraphics.DrawString(data.Message, traceFont, selectedStringBrush, xMessage, y);
}
else
{
bufferGraphics.DrawString(data.Date, traceFont, stringBrush, xTime, y);
// Info = 1, Warning = 2, Error = 3, Debug = 5, Special = 6
switch (data.LevelString)
{
case "Info":
bufferGraphics.DrawString(data.LevelString, traceFont, brushInfo, xLevel, y);
break;
case "Warning":
bufferGraphics.DrawString(data.LevelString, traceFont, brushWarning, xLevel, y);
break;
case "Error":
bufferGraphics.DrawString(data.LevelString, traceFont, brushError, xLevel, y);
break;
case "Debug":
bufferGraphics.DrawString(data.LevelString, traceFont, brushDebug, xLevel, y);
break;
default:
break;
}
bufferGraphics.DrawString(data.Source, traceFont, stringBrush, xSource, y);
if (data.Attachment != null)
{
bufferGraphics.DrawString("Attachment : ", traceFont, brushAttachment, xMessage, y);
bufferGraphics.DrawString(data.Message, traceFont, stringBrush, xMessage + (xMessage / 5), y);
}
else
bufferGraphics.DrawString(data.Message, traceFont, stringBrush, xMessage, y);
}
bufferGraphics.DrawRectangle(Pens.LightGray, rect);
//
// if (data.selected)
// {
// Color color = Color.FromArgb(100, 0, 0, 255);
// Brush brush = new SolidBrush(color);
// _bufferGraphics.FillRectangle(brush, rect);
// }
}
else
{
if (listLines.ElementAtOrDefault(listLines.Count - 1) != null)
listLines.RemoveAt(listLines.Count - 1);
if (listLinesVisible.ElementAtOrDefault(listLinesVisible.Count - 1) != null)
listLinesVisible.RemoveAt(listLinesVisible.Count - 1);
y -= 20;
}
}
else
bufferGraphics.FillRectangle(brushUnderTraces, rect);
y += 20;
traceIndex++;
}
}
this.panelTraces.Invalidate();
其中 bufferGraphics 是我的 Graphics 对象。
好的,我找到了解决方案: 我用了一种方法,然后我画了。我在 Paint 中设置了我的方法主体,它工作正常。我将 bufferGraphics 替换为 e.Graphics.
感谢那些试图帮助我的人(我的英语很糟糕所以恭喜你理解我)。