微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何利用 Winforms Paint Event 创建图形 - 从另一个类访问简单的等离子效果

如何解决如何利用 Winforms Paint Event 创建图形 - 从另一个类访问简单的等离子效果

我是 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();
    }

plasmadraw 类中,我有以下设置:

命名空间 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 举报,一经查实,本站将立刻删除。