程式利用定時器每隔3s更新坐標陣列PXY[10]得資料,期望螢屏上實時更新資料變化后的XY曲線(XY曲線分布在Panel控制元件上),但是更新完資料后,執行invalidate()指令,對應得區域就變為空白了。
希望看到的效果

實際出現的效果

代碼如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace graph_XY
{
public partial class Form1 : Form
{
Random randomX = new Random();
Random randomY = new Random();
Point[] PXY_TEMP = new Point[10];
int testint;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public const uint dashoff=20;//網格線間隔
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = panel1.CreateGraphics();//創建Panel內繪圖物件
Pen penX = new Pen(Color.Black, 1);//X軸畫筆
penX.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;//箭頭式線尾
Pen penY = new Pen(Color.Red, 2);//Y軸畫筆
penY.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;//箭頭式線尾
Pen penXY = new Pen(Color.Gray, 1);//網格線畫筆
penXY.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;//短橫線樣式
Font fontbash = new Font("宋體", 7);//網格線數字標識樣式
Brush brushbash = new SolidBrush(Color.Black);//網格線數字標識畫刷
g.DrawLine(penX, 20, panel1.Size.Height - 20, panel1.Size.Width - 20, panel1.Size.Height - 20);//畫X軸
g.DrawLine(penY, 20, panel1.Size.Height - 20, 20, 20);//畫Y軸
for (int i = 1; i < (panel1.Size.Width - 20) / dashoff - 1; i++)
{
g.DrawLine(penXY, 20 + i * dashoff, panel1.Size.Height - 20, 20 + i * dashoff, 20);//畫X軸網格線
if ((i + 1) % 4 == 0)
g.DrawString((dashoff * i).ToString(), fontbash, brushbash, dashoff * i + 12, panel1.Size.Height - 15);//畫X軸網格線數字標識
}
for (int i = 1; i < (panel1.Size.Height - 20) / dashoff - 1; i++)
{
g.DrawLine(penXY, 20, panel1.Size.Height - 20 - i * dashoff, panel1.Size.Width - 20, panel1.Size.Height - 20 - i * dashoff);//畫Y軸網格線
if ((i + 1) % 4 == 0)
g.DrawString((dashoff * i).ToString(), fontbash, brushbash, 0, panel1.Size.Height - 20 - i * dashoff);//畫Y軸網格線數字標識
}
Font fontXY = new Font("黑體", 10);//坐標軸標注樣式
Brush brushXY = new SolidBrush(Color.Black);
g.DrawString("距離", fontXY, brushXY, panel1.Size.Width - 40, panel1.Size.Height - 40);//X軸標注
g.DrawString("扭矩", fontXY, brushXY, 20, 0);//Y軸標注
Pen pen = new Pen(Color.Red, 1);
g.DrawLines(pen, PXY_TEMP);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (testint > 2) testint = 0;
switch (testint)
{
case 0:
//曲線一
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:
//曲線二
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:
//曲線三
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.Location, panel1.Size), true);
}
}
}
uj5u.com熱心網友回復:
建議樓主仔細研究一下控制元件的 Invalidate 具體含義,就明白為什么呼叫之后會出現空白了。uj5u.com熱心網友回復:
害, 你用Invalidate刷,From_Paint肯定接收不到嘍,要用 panel1_Paintuj5u.com熱心網友回復:
樓上說得對,為什么不直接在panel1_Paint里面繪制呢?轉載請註明出處,本文鏈接:https://www.uj5u.com/net/263146.html
標籤:C#
下一篇:請問ado引數化設定問題
