问题描述
我是 C# 编程的初学者,我正在尝试使用 Winforms 创建一些图形效果。我试图产生简单的“等离子效果”,但是使用 Paint Event 真的很令人沮丧(或者我只是愚蠢)。
在我的示例中,我有两个类(Form1 和 plasmadraw)。
在Form1中我具有以下设置:
命名空间 plasma_effect { 公共部分类 Form1 :表单 { 公共 Form1() { 初始化组件(); this.Width = 1920; this.Height = 1200; FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Maximized; this.DoubleBuffered = true; }
private void Form1_Paint(object sender,PaintEventArgs e)
{
plasmadraw plasma = new plasmadraw();
plasma.Draw(e.Graphics);
Invalidate();
}
private void Form1_Load(object sender,EventArgs e)
{
// Why I can't create instance of plasmadraw class once here,I have to do it in Paint event ??
}
private void Form1_MouseClick(object sender,MouseEventArgs e)
{
this.Close();
}
命名空间 plasma_effect { 类等离子绘图 {
int y;
int x;
double i;
double pii = 3.1415;
public void Draw(Graphics gfx)
{
// Should I use For loop here in creating the plasma,I managed to create full screen plasma with it,but no animation ?
x = x + 10; // this should increase the value,however the value is not increased
i = i + 1;
if (x == 1920)
{
x = 0;
y = y + 10;
}
if (y == 1200)
{
y = 0;
}
double v = Math.Sin((x * 0.5) + i * 0.001);
double c = v * pii;
double d = c + (2 * pii / 3);
double f = c + (6 * pii / 3);
double r = 255 * Math.Abs(Math.Sin(c));
double g = 255 * Math.Abs(Math.Sin(d));
double b = 255 * Math.Abs(Math.Sin(f));
int r1 = (int)r;
int g1 = (int)g;
int b1 = (int)b;
Color e = Color.FromArgb(r1,g1,b1);
SolidBrush brush = new SolidBrush(e);
Rectangle rect = new Rectangle(x,y,10,10);
gfx.FillRectangle(brush,rect);
}
}
}
基本上我正在尝试创建连续循环,我可以在其中运行效果。问题是程序只创建了一个矩形并在此之后停止。我应该怎么做才能让它连续运行? 之前我已经成功通过基本创建对象的列表(例如精灵),然后使用的foreach在Paint事件吸引他们&无效创建一些运动效果()。但是,我想学习使用其他方法,而不是总是使用相同(复制)的方式。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)