У меня есть несколько слоев рисунков на моей панели - PullRequest
0 голосов
/ 21 октября 2019

Я создал панель и положил на нее текст с помощью DrawString и DrawRectangle. Для рисования панели я использую событие Paint и метод Invalidate, но когда вызывается Invalidate, у меня получается следующий результат (с прозрачным задним цветом): Моя панель с кулиской

Как вы можетевидите, текст рисуется, но то, что стоит за ним, не исчезает, как будто 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 - мой графический объект.

1 Ответ

0 голосов
/ 21 октября 2019

Хорошо, я нашел решение: я использовал метод, и я рисую после. Я установил тело моего метода в Paint, и он работает нормально. Я заменяю bufferGraphics на e.Graphics.

Спасибо людям, которые пытаются мне помочь (мой английский очень плохой, поэтому поздравляю, что меня поняли).

...