首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >面板的C#双缓冲

面板的C#双缓冲
EN

Stack Overflow用户
提问于 2013-10-13 07:41:29
回答 1查看 435关注 0票数 0

我到处寻找面板的双缓冲,但它使用的术语对我来说太混乱了。我正在绘制生物节律,并希望有一个移动功能来缓慢地向前移动生物节律。然而,它闪烁了很多,所以我需要找出如何阻止它。我试图线程它,但没有用,因为线程仍然会导致闪烁。

下面是计时器内部的内容:你可以跳到底部,看看它在哪里绘制图形。基本上,它会刷新、计算和绘制点。

代码语言:javascript
复制
  Panel p = panel1;
        p.Refresh();
        double physical = Math.Sin(2 * Math.PI * t / 23) * 100;
        double emotional = Math.Sin(2 * Math.PI * t / 28) * 100;
        double intellectual = Math.Sin(2 * Math.PI * t / 33) * 100;
        double physicalint = int.Parse(Math.Round(physical).ToString());
        double emotionalint = int.Parse(Math.Round(emotional).ToString());
        double intellectualint = int.Parse(Math.Round(intellectual).ToString());

        Console.WriteLine(physical + " Physical, " + emotional + " Emotional, " + intellectual + " Intellectual.");
        Console.WriteLine(physicalint + " Physical, " + emotionalint + " Emotional, " + intellectualint + " Intellectual.");

        int midpt = p.Width / 2;
        double Mastery = (physicalint + intellectualint) / 2;
        double Passion = (physicalint + emotionalint) / 2;
        double Wisdom = (emotionalint + intellectualint) / 2;
        int mastery = int.Parse(Math.Round(Mastery).ToString());
         int passion = int.Parse(Math.Round(Passion).ToString());
         int wisdom = int.Parse(Math.Round(Wisdom).ToString());
        Results.Text = "Primary Biorhythms\nPhysical: " + physicalint.ToString() + "\nEmotional: " + emotionalint.ToString() + "\nIntellectual: " + intellectualint.ToString();
        Results.Text += "\nSecondary Biorhythms\nWisdom: " + wisdom.ToString() + "\nPassion: " + passion.ToString() + "\nMastery: " + mastery.ToString();


        int eanum = int.Parse(Spread.Text);

        if (mode == 0)
        {


            for (int i = -eanum; i <= eanum; i++)
            {
                double actualheightphy = p.Height / 2;
                double physical2 = Math.Sin(2 * Math.PI * (t + i) / 23) * 100;
                double emotional2 = Math.Sin(2 * Math.PI * (t + i) / 28) * 100;
                double intellectual2 = Math.Sin(2 * Math.PI * (t + i) / 33) * 100;
                if (physical2 < 0) { physical2 = physical2 * 1.45; }
                if (physical2 > 0) { physical2 = physical2 * 1.5; }
                if (emotional2 < 0) { emotional2 = emotional2 * 1.45; }
                if (emotional2 > 0) { emotional2 = emotional2 * 1.5; }
                if (intellectual2 < 0) { intellectual2 = intellectual2 * 1.45; }
                if (intellectual2 > 0) { intellectual2 = intellectual2 * 1.5; }
                actualheightphy -= physical2;
                double actualheightemo = p.Height / 2;
                double actualheightint = p.Height / 2;
                actualheightemo -= emotional2;
                actualheightint -= intellectual2;
                int distancebetween = p.Width / eanum;

                p.CreateGraphics().FillEllipse(Brushes.Red, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightphy).ToString()) - 5, 10, 10);
                p.CreateGraphics().FillEllipse(Brushes.Blue, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightemo).ToString()) - 5, 10, 10);
                p.CreateGraphics().FillEllipse(Brushes.Green, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightint).ToString()) - 5, 10, 10);
                if (i % 7 == 0)
                {
                    p.CreateGraphics().DrawString(i.ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular), Brushes.White, new RectangleF(i * distancebetween + midpt - 5, p.Height - 12, 30, 30));
                }
                if (eanum == 7)
                {
                    p.CreateGraphics().DrawString(i.ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular), Brushes.White, new RectangleF(i * distancebetween + midpt - 5, p.Height - 12, 30, 30));

                }
            }
            p.CreateGraphics().DrawLine(Pens.White, 0, (p.Height) / 2, p.Width, (p.Height) / 2);
            p.CreateGraphics().DrawLine(Pens.White, p.Width / 2, 0, p.Width / 2, p.Height);

            this.CreateGraphics().DrawString("100", new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), Brushes.Black, new RectangleF(p.Width / 2 + p.Left - 15, -1, 100, 100));
            this.CreateGraphics().DrawString("-100", new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), Brushes.Black, new RectangleF(p.Width / 2 + p.Left - 15, p.Height + p.Top + 1, 100, 100));
        }
EN

回答 1

Stack Overflow用户

发布于 2013-10-13 07:48:36

一个可能的原因可能是,由于CreateGraphics方法的性质,每次绘制时都会创建一个新的Graphics对象。这将是幸运的,因为哪一个被画出来了,所以我想它肯定会显着闪烁。

因此,获取p.CreateGraphics()的返回值并将其重用于所有绘制,而不是多次调用它;例如:

代码语言:javascript
复制
p.CreateGraphics().FillEllipse(...);
p.CreateGraphics().FillEllipse(...);
p.CreateGraphics().FillEllipse(...);

应该是:

代码语言:javascript
复制
Graphics graphics=p.CreateGraphics();
...
graphics.FillEllipse(...);
graphics.FillEllipse(...);
graphics.FillEllipse(...);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19340330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档