【问题标题】:I have several layers of drawings on my panel我的面板上有几层图纸
【发布时间】:2019-10-21 11:39:17
【问题描述】:

我创建了一个面板,并使用 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 对象。

【问题讨论】:

  • 一开始FillRectangle 用背景色填充整个框架怎么样?
  • 我们需要看代码!!!您是否根据需要使用 e.Graphics?
  • 我编辑了我的帖子,你可以看到代码。
  • 在我做的绘画事件中:e.Graphics.DrawImageUnscaled(backBuffer, 0, 0);其中 backbuffer 是我的 Bitmap 对象
  • bufferGraphics = e.Graphics 也是如此吗?另外:您添加到位图的任何内容都将保留在那里,直到您覆盖它;除了清除它之外,没有使位图无效。 - 为什么还要使用位图,因为您正在编写 Paint 事件??? (反之亦然)

标签: c# winforms panel paint


【解决方案1】:

好的,我找到了解决方案: 我用了一种方法,然后我画了。我在 Paint 中设置了我的方法的主体,它工作正常。我用 e.Graphics 替换了 bufferGraphics。

感谢那些试图帮助我的人(我的英语很差,所以恭喜你能理解我)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多