Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net在向面板绘制许多形状时的闪烁效果(需要优化)_.net_Panel_System.drawing_Drawing2d - Fatal编程技术网

.net在向面板绘制许多形状时的闪烁效果(需要优化)

.net在向面板绘制许多形状时的闪烁效果(需要优化),.net,panel,system.drawing,drawing2d,.net,Panel,System.drawing,Drawing2d,为了实现这种类型的交互式艺术品,您可以拖动方框并在它们之间绘制连接线,我使用下面的代码 基本上,当拖动鼠标(移动框)或通过单击新框在面板上进行新选择时,我会清除面板,绘制所有矩形、所有文本,然后使用计时器绘制所有线条连接(大约每秒20次)。然而,重新绘制面板组件的速度似乎很慢,并且会产生非常恼人的闪烁效果。有没有办法优化此代码?/让绘图更有效率 public MainForm() { // Create surface and pen to use for al

为了实现这种类型的交互式艺术品,您可以拖动方框并在它们之间绘制连接线,我使用下面的代码

基本上,当拖动鼠标(移动框)或通过单击新框在面板上进行新选择时,我会清除面板,绘制所有矩形、所有文本,然后使用计时器绘制所有线条连接(大约每秒20次)。然而,重新绘制面板组件的速度似乎很慢,并且会产生非常恼人的闪烁效果。有没有办法优化此代码?/让绘图更有效率

    public MainForm()
    {
        // Create surface and pen to use for all drawing
        surface = NodesPanel.CreateGraphics();
        surface.InterpolationMode = InterpolationMode.Default;
        surface.SmoothingMode = SmoothingMode.Default;

        penl = new Pen(Color.Blue, 2.0f);
        ....

        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 60;              // Timer call
        timer.Enabled = true;                       // Enable the timer
        timer.Start();
     }





    void timer_Tick(object sender, EventArgs e)
    {
        if (refreshScreen) // when dragging a box of making a new selection
        {
            RenderScreen();
        }
    }



    public void RenderScreen()
    {
        surface.Clear(Color.White);

        drawUnselectedNodes();

        if (showSearchGate)
        {
            drawGates();
        }

        if (currentlyClickedNode != null && !(currentlyClickedNode is Gate))
        {
            currentlyClickedNode.drawMyConnections(surface, penl); // Draw in a different colour
            currentlyClickedNode.drawMe(surface, penl);

            if (drawingLine)
            {
                DrawingHelper.DrawLine(surface, penl, currentlyClickedNode.getGatePosition(), linePosition, "regular");
            }
        }
    }



    private void drawUnselectedNodes()
    {
        foreach (QuestNode q in nodes)
        {
            if (q != currentlyClickedNode)
            {
                q.drawMe(surface, penl);
            }
        }

    }



 // The node structure
public class QuestNode
{
    ...
    public void drawMe(Graphics g, Pen p)
    {
        DrawingHelper.DrawRoundRectWithText
             (g, p, pos.x, pos.y, NODE_WIDTH * currentScale, NODE_HEIGHT *
                  currentScale, GATE_RADIUS * currentScale, name, description, true,
                       (selected ? RECTANGLE_SELECTED_COLOR : ANGLE_UNSELECTED_COLOR));
    }

    public void drawMyConnections(Graphics g, Pen p) 
    {
        foreach (QuestNode qn in relatedQuests)
        {
            DrawingHelper.DrawLine(g, p, pos, qn.getGatePosition(), "regular");
        }
    }
 }




static class DrawingHelper
{
    public static SolidBrush STANDARD_BRUSH = new SolidBrush(RECTANGLE_COLOR);
    public static int PRIMARY_TEXT_FONT_SIZE = 10;
    public static Font PRIMARY_FONT_STYLE = new Font("Arial", PRIMARY_TEXT_FONT_SIZE);
    public static int SECONDARY_TEXT_FONT_SIZE = 8;
    public static Font SECONDARY_FONT_STYLE = new Font("Arial", SECONDARY_TEXT_FONT_SIZE);


     public static void DrawRoundRectWithText(Graphics g, Pen p, float x, float y,
             float width, float height, float gateRadius, string textFirstLike, 
                 string textSecondsLine, bool hasGate, Color color)
    {
        Graphics formGraphics = g;
        g.InterpolationMode = InterpolationMode.Default;
        g.SmoothingMode = SmoothingMode.Default;

        SolidBrush myBrush = STANDARD_BRUSH;
        myBrush.Color = color;

        p.DashPattern = new float[] { 1 };
        Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
        p.Width = RECTANGLE_BORDER;

        formGraphics.FillRectangle(myBrush, rect);
        p.Color = RECTANGLE_COLOR;
        formGraphics.DrawRectangle(p, rect);


        DrawString(g, textFirstLike, x + TEXT_OFFSET_X, y + height / 4, PRIMARY_TEXT_FONT_SIZE, (int)width);
        DrawString(g, textSecondsLine, x + TEXT_OFFSET_X, y + height / 2, SECONDARY_TEXT_FONT_SIZE, (int)width);

        int gateWidth = (int)gateRadius;
        int gateHeight = (int)gateRadius;

        if (hasGate)
        {

            myBrush.Color = GATE_COLOR;
            Rectangle circle = new Rectangle((int)(x + width) - gateWidth/2, (int)(y + height) - gateHeight/2, gateWidth, gateHeight);

            formGraphics.FillEllipse(myBrush, circle);
            formGraphics.DrawEllipse(p, circle);

        }

        Point[] diamondPoints = new Point[4];
        diamondPoints[0] = new Point((int)(x - gateWidth / 2), (int)y);
        diamondPoints[1] = new Point((int)x, (int)(y - gateHeight / 2));
        diamondPoints[2] = new Point((int)(x + gateWidth / 2), (int)y);
        diamondPoints[3] = new Point((int)x, (int)(y + gateHeight / 2));

        myBrush.Color = Color.LightGray;
        formGraphics.FillPolygon(myBrush, diamondPoints);
        formGraphics.DrawPolygon(p, diamondPoints);
    }
  }

    public static void DrawLine(Graphics g, Pen p, Vector2D origin, Vector2D end, string style)
    {
        Pen myPen = p;
        p.Color = Color.Black;

        Graphics formGraphics = g;

        g.InterpolationMode = InterpolationMode.Default;
        g.SmoothingMode = SmoothingMode.Default;

        myPen.Width = LINE_BORDER;
        if (style == "dotted")
        {
            myPen.DashPattern = new float[] { 1, 1, 0.5f };
        }

        formGraphics.DrawLine(myPen, origin.x, origin.y, end.x, end.y);

    }

    private static void DrawString(Graphics g, string text, float posX, float posY, int fontSize, int stringMaxWidth)
    {

        string drawString = text;
        Font drawFont = (fontSize == PRIMARY_TEXT_FONT_SIZE) ? PRIMARY_FONT_STYLE : SECONDARY_FONT_STYLE;
        SolidBrush drawBrush = STANDARD_BRUSH;
        drawBrush.Color = Color.Black;

        float x = posX;
        float y = posY;

        float charHeight = drawFont.GetHeight() / 2;
        int maxChars = (int)(stringMaxWidth / charHeight);

        if (drawString.Length > (maxChars - 3))
        {
            drawString = drawString.Substring(0, maxChars - 3) + "...";
        }

        g.DrawString(drawString, drawFont, drawBrush, x, y);
    }

切勿使用不能双缓冲的CreateGraphics()。始终在绘制事件处理程序中绘制。谢谢。我已将所有绘图功能移动到面板的“绘制”事件中,并将事件的图形对象用于所有绘图。这一点,并使面板双缓冲(使用这里的信息),已消除所有闪烁。谢谢