ฉันมีภาพวาดหลายชั้นบนแผงของฉัน

ฉันสร้างพาเนลและใส่ข้อความด้วย 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 เป็นวัตถุกราฟิกของฉัน


person Yohan Quiles    schedule 21.10.2019    source แหล่งที่มา
comment
แล้ว FillRectangle ที่จุดเริ่มต้นจะเติมสีพื้นหลังให้ทั้งเฟรมล่ะ?   -  person Thomas N    schedule 21.10.2019
comment
เราต้องดูโค้ด!!! คุณใช้ e.Graphics ตามที่คุณต้องการหรือไม่?   -  person TaW    schedule 21.10.2019
comment
ฉันแก้ไขโพสต์แล้ว คุณสามารถดูรหัสได้   -  person Yohan Quiles    schedule 21.10.2019
comment
ในเหตุการณ์ Paint ฉันทำ: e.Graphics.DrawImageUnscaled(backBuffer, 0, 0); โดยที่ backbuffer คือวัตถุบิตแมปของฉัน   -  person Yohan Quiles    schedule 21.10.2019
comment
bufferGraphics = e.Graphics ก็เช่นกัน นอกจากนี้: ทุกสิ่งที่คุณเพิ่มลงในบิตแมปจะอยู่ที่นั่นจนกว่าคุณจะเขียนทับ ไม่มีการทำให้บิตแมปเป็นโมฆะนอกจากการล้างข้อมูล - เหตุใดจึงต้องใช้บิตแมปเนื่องจากคุณกำลังเขียนโค้ดกิจกรรม Paint??? (และในทางกลับกัน)   -  person TaW    schedule 21.10.2019
comment
ฉันจะพยายามลบบิตแมปก่อนที่จะเขียนใหม่ และฉันใช้โค้ดจากผู้ใช้รายอื่นในบริษัทของฉันและทำงานได้ดี ฉันต้องอัปเดตโค้ดและเริ่มต้นใหม่ทั้งหมดเนื่องจากส่วนประกอบบางอย่างแตกต่างออกไป   -  person Yohan Quiles    schedule 21.10.2019
comment
โดยปกติแล้วจะเป็น Paint ด้วย e.Graphics หรือวาดลงใน Bimap อย่างหลังไม่ได้อยู่ในเหตุการณ์ Paint แต่เมื่อข้อมูลมีการเปลี่ยนแปลง สำหรับแบบเดิม Invalidate เมื่อข้อมูลมีการเปลี่ยนแปลง เหตุผลเดียวที่คนจะรวมทั้งสองเข้าด้วยกันก็คือเมื่อมีการดำเนินการจั่วจำนวนมาก (>1k) จากนั้นเราสามารถแคชอันที่เก่ากว่าในบิตแมปแล้วดึงส่วนที่เหลือทับมัน   -  person TaW    schedule 21.10.2019
comment
@TaW การสะท้อนของคุณน่าสนใจ ตอนนี้คุณพูดแล้ว ฉันคิดว่าแผงของฉันต้องเก็บการจับรางวัลครั้งล่าสุด (ฉันมีข้อมูลที่สร้างทุกๆ มิลลิวินาที) แต่อยากลบงวดที่ไม่จำเป็นออก   -  person Yohan Quiles    schedule 21.10.2019
comment
ฉันจะไม่วาดบ่อยเกินกว่าที่ตามนุษย์จะมองเห็น ไม่ต้องพูดถึงการอ่านเลย ฉันหมายถึงสถานการณ์เช่นโปรแกรมวาดภาพ บางทีเราควรรู้เพิ่มเติมอีกเล็กน้อยเกี่ยวกับการตั้งค่าทั่วไป   -  person TaW    schedule 21.10.2019
comment
ในตอนแรกฉันใช้ DataGridView แต่เมื่อข้อมูลมาถึง DTG ใช้เวลานานในการแสดงข้อมูล ดังนั้นฉันจึงใช้งาน Paint and Data มาถึงเร็วมากตอนนี้   -  person Yohan Quiles    schedule 21.10.2019


คำตอบ (1)


โอเค ฉันพบวิธีแก้ปัญหาแล้ว ฉันใช้วิธีการหนึ่งแล้วจึงทาสีตาม ฉันตั้งค่าวิธีการของฉันใน Paint และทำงานได้ดี ฉันแทนที่ bufferGraphics ด้วย e.Graphics

ขอบคุณคนที่พยายามช่วยเหลือฉัน (ภาษาอังกฤษของฉันแย่มาก ยินดีด้วยที่เข้าใจฉัน)

person Yohan Quiles    schedule 21.10.2019