C# GDI+ 出现的令人诧异的问题,来看看,或许你也从未遇到过
Form1:C# code:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace yanshi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(dr));
th.Start();
}
public void dr()
{
int i = 0;
Graphics g = this.CreateGraphics();
Color col1 = Color.White;
Color col2 = Color.Black;
SolidBrush brus1 = new SolidBrush(col1);
SolidBrush brus2 = new SolidBrush(col2);
Rectangle rect = new Rectangle(0, 0, 0, 0);
rect.Location = new Point(100, 400);
rect.Width = 200; //Set rect's width
rect.Height = 200; //set rect's height
do
{
if (i % 2 == 0)
{
g.FillRectangle(brus1, rect);//Drawing white for high voltage
}
else
{
g.FillRectangle(brus2, rect);//Drawing black for low voltage
}
ys.AccurateTimer.AccurateSleep(50);
i++;
} while (i<1000);
}
}
}
ys.cs:
C# codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ys
{
public struct MSG //structure MSG
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
public class AccurateTimer
{
public static bool IsTimeBeginPeriod = false;
const int PM_REMOVE = 0x0001;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PeekMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin,
uint wMsgFilterMax, uint wRemoveMsg);
[DllImport("user32.dll", SetLastError = true)]
static extern bool TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll", SetLastError = true)]
static extern bool DispatchMessage(ref MSG lpMsg);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceCounter(ref Int64 count);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceFrequency(ref Int64 frequency);
public static int GetTimeTick()
{
return Environment.TickCount;
}
public static void AccurateSleep(float DlayTime)
{
Int64 Frequency = 0;
Int64 StartTime = 0;
Int64 EndTime = 0;
float PassedMSec = 0;
MSG msg;
AccurateTimer.QueryPerformanceCounter(ref StartTime); //get start time
AccurateTimer.QueryPerformanceFrequency(ref Frequency); //get cpu's frequency
do
{
if (AccurateTimer.PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE))
{
AccurateTimer.TranslateMessage(ref msg);
AccurateTimer.DispatchMessage(ref msg);
}
AccurateTimer.QueryPerformanceCounter(ref EndTime);
PassedMSec = ((float)(EndTime - StartTime) / (float)Frequency) * 1000;
} while (PassedMSec <= DlayTime); // delay DlayTime
}
}
}
就这两个,你们先看看效果,应该和我的一样问题:就是黑白在闪的时候,有一条东西从上而下的划过,肉眼能看清楚,像是以前黑白电视那种东西...能解决这个问题的,我估计只有超级高手了。