Home > Net >  Want to GDI real-time update y curve, invalidate the picture is no display ()
Want to GDI real-time update y curve, invalidate the picture is no display ()

Time:02-25

Program using the array of every 3 s update timer PXY [10] data, expect real-time update data on the screen changes after the XY curve (XY curve on the Panel control), but after updating the data, perform invalidate () command, to deserve area becomes a blank,

Want to see the effect of


The effect of the actual appear



The code below
using System;
using System.Collections.Generic;
using System.ComponentModel;
Using System. The Data;
using System.Drawing;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
using System.Windows.Forms;


The namespace graph_XY
{
Public partial class Form1: Form
{
The Random randomX=new Random ();
The Random randomY=new Random ();
Point [] PXY_TEMP=new Point [10].
Int testint;
Public _click ()
{
InitializeComponent ();
}

Private void button1_Click (object sender, EventArgs e)
{
Timer1. Enabled=true;
}

Public const uint dashoff=20;//grid interval
Private void Form1_Paint (object sender, PaintEventArgs e)
{
Graphics g=panel1. CreateGraphics ();//create a Panel drawing object

Pen penX=new Pen (Color Black, 1);//X brush
PenX. EndCap=System. Drawing. Drawing2D. LineCap. ArrowAnchor;//arrow type line end
Pen penY=new Pen (Color. Red, 2);//Y brush
PenY. EndCap=System. Drawing. Drawing2D. LineCap. ArrowAnchor;//arrow type line end
Pen penXY=new Pen (Color. Gray, 1); Brush//grid lines
PenXY. DashStyle=System. Drawing. Drawing2D. DashStyle. Dash;//dash style
The Font fontbash=new Font (" tahoma ", 7);//grid digital identity style
Brush brushbash=new SolidBrush (Color Black); Digital identity brush//grid lines

G.D rawLine (penX, 20, panel1. The Size, Height - 20, panel1. Size. The Width - 20, panel1. The Size, Height - 20);//draw X
G.D rawLine (penY, 20, panel1. The Size, Height - 20, 20, 20).//draw Y
For (int I=1; I & lt; (panel1. Size. Width - 20)/dashoff - 1; I++)
{
G.D rawLine (penXY, 20 + I * dashoff, panel1. The Size, Height - 20, 20 + I * dashoff, 20).//draw the X axis gridlines
If ((I + 1) % 4==0)
G.D rawString (dashoff * (I). The ToString (), fontbash, brushbash, dashoff * I + 12, panel1. The Size, Height - 15);//draw the X axis gridlines digital id
}

For (int I=1; I & lt; (panel1. Size. Height - 20)/dashoff - 1; I++)
{
G.D rawLine (penXY, 20, panel1. The Size, Height - 20 - I * dashoff, panel1. Size. The Width - 20, panel1. The Size, Height - 20 - I * dashoff);//draw Y grid lines
If ((I + 1) % 4==0)
G.D rawString (dashoff * (I). The ToString (), fontbash, brushbash, 0, panel1. The Size, Height - 20 - I * dashoff);//to draw Y grid digital id
}
The Font fontXY=new Font (" blackbody, "10);//axis annotation style
Brush brushXY=new SolidBrush (Color Black);
G.D rawString (" distance ", fontXY brushXY, panel1. The Size, Width and 40, panel1. The Size, Height and 40);//X mark
G.D rawString (" torque ", fontXY, brushXY, 20, 0).//Y annotation

Pen Pen=new Pen (Color. Red, 1);
G.D rawLines (pen, PXY_TEMP);

}

Private void timer1_Tick (object sender, EventArgs e)
{

If (testint & gt; 2) testint=0;
The switch (testint)
{
Case 0:
//curve a
PXY_TEMP [0]=new Point (30, 30);
PXY_TEMP [1]=new Point (50, 50);
PXY_TEMP [2]=new Point (90, 90);
PXY_TEMP [3]=new Point (150, 150);
PXY_TEMP [4]=new Point (200, 180);
PXY_TEMP [5]=new Point (250, 250);
PXY_TEMP [6]=new Point (300, 300);
PXY_TEMP [7]=new Point (350, 100);
PXY_TEMP [8]=new Point (400, 150);
PXY_TEMP [9]=new Point (500, 200);
Testint++;
break;
Case 1:
//curve 2
PXY_TEMP [0]=new Point (30, 200);
PXY_TEMP [1]=new Point (50, 150);
PXY_TEMP [2]=new Point (90, 100);
PXY_TEMP [3]=new Point (150, 300);
PXY_TEMP [4]=new Point (200, 250);
PXY_TEMP [5]=new Point (250, 250);
PXY_TEMP [6]=new Point (300, 300);
PXY_TEMP [7]=new Point (350, 100);
PXY_TEMP [8]=new Point (400, 150);
PXY_TEMP [9]=new Point (500, 200);
Testint++;
break;
Case 2:
//curve of three
PXY_TEMP [0]=new Point (30, 300);
PXY_TEMP [1]=new Point (50, 250);
PXY_TEMP [2]=new Point (90, 300);
PXY_TEMP [3]=new Point (150, 150);
PXY_TEMP [4]=new Point (240, 180);
PXY_TEMP [5]=new Point (250, 250);
PXY_TEMP [6]=new Point (320, 300);
PXY_TEMP [7]=new Point (350, 100);
PXY_TEMP [8]=new Point (430, 150);
PXY_TEMP [9]=new Point (500, 200);
Testint++;
break;
}

//Invalidate ();
Invalidate (new Rectangle (panel1. The Location, panel1. Size), true);

}
}
}

CodePudding user response:

Suggested that Invalidate the specific meaning of the original poster carefully study the control, will understand why call later there will be a blank,

CodePudding user response:

Hurt, you brush with Invalidate, From_Paint must receive less than, want to use panel1_Paint

CodePudding user response:

Upstairs right, why not directly in panel1_Paint drawing?
  •  Tags:  
  • C#
  • Related