在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例桌面应用界面/GUI → 水波效果

水波效果

桌面应用界面/GUI

下载此实例
  • 开发语言:C#
  • 实例大小:0.07M
  • 下载次数:31
  • 浏览次数:429
  • 发布时间:2015-12-15
  • 实例类别:桌面应用界面/GUI
  • 发 布 人:ascecn
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 窗口 水波

实例介绍

【实例简介】

实现窗体的水波效果

【实例截图】


【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace SDEffectWaterWave
{
    public partial class FormMain : Form
    {
        #region 变量

        Bitmap m_bmp;               // 图片
        byte[] m_byArrClrInfo;      // 图片原始颜色信息
        byte[] m_byArrClrBuff;      // 图片新的颜色信息
        int[,] m_nArrWaveCurrent;   // 当前波形
        int[,] m_nArrWaveNext;      // 下一帧的波形
        int m_nBmpWidth;            // 图片宽度
        int m_nBmpHeight;           // 图片高度
        int m_nBmpWidthBySize;      // 图片每行占用字节数

        #endregion



        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public FormMain()
        {
            InitializeComponent();
        }

        #endregion







        #region 窗体事件

        /// <summary>
        /// 处理窗体加载事件
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件</param>
        private void FormMain_Load(object sender, EventArgs e)
        {
            // 加载图像 设置界面显示
            Bitmap bmp = new Bitmap("background.jpg");// 打开一张图将起转换为24位
            m_bmp = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), PixelFormat.Format24bppRgb);
            pictureBox.Image = m_bmp;
            pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            this.Width = pictureBox.Width   2 * pictureBox.Left   (this.Size - this.ClientSize).Width;
            this.Height = pictureBox.Height   2 * pictureBox.Top   (this.Size - this.ClientSize).Height;
            this.BackColor = Color.Black;
            // 加载图像信息 初始化变量
            BitmapData bmpData = m_bmp.LockBits(new Rectangle(0, 0, m_bmp.Width, m_bmp.Height), ImageLockMode.ReadOnly, m_bmp.PixelFormat);
            m_byArrClrInfo = new byte[bmpData.Stride * bmpData.Height];
            m_byArrClrBuff = new byte[m_byArrClrInfo.Length];
            m_nArrWaveCurrent = new int[m_bmp.Width, m_bmp.Height];
            m_nArrWaveNext = new int[m_bmp.Width, m_bmp.Height];
            m_nBmpWidth = m_bmp.Width;
            m_nBmpHeight = m_bmp.Height;
            m_nBmpWidthBySize = bmpData.Stride;
            Marshal.Copy(bmpData.Scan0, m_byArrClrInfo, 0, m_byArrClrInfo.Length);
            m_bmp.UnlockBits(bmpData);
            // 启动水波的模拟
            timerDraw.Interval = 25;        // 绘制水波
            timerDraw.Enabled = true;
            timerSetWave.Interval = 500;    // 随机产生波源
            timerSetWave.Enabled = true;
        }

        /// <summary>
        /// 处理定时器时间步事件[绘制水波]
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件</param>
        private void timerDraw_Tick(object sender, EventArgs e)
        {
            int nNewX = 0;
            int nNewY = 0;
            BitmapData bmpData = m_bmp.LockBits(new Rectangle(0, 0, m_bmp.Width, m_bmp.Height), ImageLockMode.ReadWrite, m_bmp.PixelFormat);
            Marshal.Copy(bmpData.Scan0, m_byArrClrBuff, 0, m_byArrClrBuff.Length);
            for (int y = 1; y < m_nBmpHeight - 1; y  )
            {
                for (int x = 1; x < m_nBmpWidth - 1; x  )
                {
                    m_nArrWaveNext[x, y] = ((               // 能量传递
                        m_nArrWaveCurrent[x - 1, y]         // 注意 能量传递是通过当前波形计算新的波形
                        m_nArrWaveCurrent[x   1, y]         // m_nArrWaveCurrent m_nArrWaveNext 不要弄翻
                        m_nArrWaveCurrent[x, y - 1]  
                        m_nArrWaveCurrent[x, y   1])
                        >> 1) - m_nArrWaveNext[x, y];
                    m_nArrWaveNext[x, y] -= m_nArrWaveNext[x, y] >> 5;  // 产生阻尼
                    // 像素偏移 (模拟折射)
                    nNewX = ((m_nArrWaveNext[x   1, y] - m_nArrWaveNext[x - 1, y]) >> 0)   x;   // 右移越大 折射变大
                    nNewY = ((m_nArrWaveNext[x, y   1] - m_nArrWaveNext[x, y - 1]) >> 0)   y;   // 左移也可 折射变小

                    if (nNewX == x && nNewY == y) continue;     // 没有产生像素偏移 直接跳过
                    if (nNewX < 0) nNewX = -nNewX;              // 也可将其赋值为 0
                    if (nNewX >= m_nBmpWidth) nNewX = m_nBmpWidth - 1;
                    if (nNewY < 0) nNewY = -nNewY;
                    if (nNewY >= m_nBmpHeight) nNewY = m_nBmpHeight - 1;
                    // 模拟光的反射 也可以跳过 不过波纹明暗度不明显
                    // m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3] = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3];
                    // m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3   1] = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3   1];
                    // m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3   2] = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3   2];
                    // continue;
                    int nIncrement = m_nArrWaveNext[x, y];      // 用当前像素点的能量作为光线明暗度变化标志
                    nIncrement >>= nIncrement < 0 ? 5 : 3;      // 如果负数变暗 正数变量 (适当的位移一下不然差距太大)
                    // 重置RGB值
                    int r = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3]   nIncrement;
                    int g = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3   1]   nIncrement;
                    int b = m_byArrClrInfo[nNewY * m_nBmpWidthBySize   nNewX * 3   2]   nIncrement;
                    if (nIncrement < 0)
                    {       // 如果是负数便是变暗 则不能让其越界 0 - 255 
                        r = r < 0 ? 0 : r;
                        g = g < 0 ? 0 : g;
                        b = b < 0 ? 0 : b;
                    }
                    else
                    {
                        r = r > 255 ? 255 : r;
                        g = g > 255 ? 255 : g;
                        b = b > 255 ? 255 : b;
                    }
                    m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3] = (byte)r;
                    m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3   1] = (byte)g;
                    m_byArrClrBuff[y * m_nBmpWidthBySize   x * 3   2] = (byte)b;
                }
            }
            Marshal.Copy(m_byArrClrBuff, 0, bmpData.Scan0, m_byArrClrBuff.Length);
            m_bmp.UnlockBits(bmpData);
            pictureBox.Refresh();
            // 交换能量缓存 将新产生的波形 赋值给当前波形的缓存 计算下一帧的波形
            int[,] temp = m_nArrWaveCurrent;
            m_nArrWaveCurrent = m_nArrWaveNext;
            m_nArrWaveNext = temp;
        }

        /// <summary>
        /// 处理定时器时间步事件[设置水波]
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件</param>
        private void timerSetWave_Tick(object sender, EventArgs e)
        {
            Random rd = new Random();
            for (int i = 0; i < 5; i  )         // 随机产生五个波源
            {
                SetWavePoint(rd.Next(m_nBmpWidth - 1), rd.Next(m_nBmpHeight - 1), rd.Next(5, 10), rd.Next(32, 128));
            }
        }

        /// <summary>
        /// 处理定时器时间步事件[绘制水波]
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件</param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            Random rd = new Random();           // 将鼠标滑动过的位置产生波源
            SetWavePoint(e.X, e.Y, rd.Next(5, 10), rd.Next(32, 128));
        }

        #endregion







        #region 辅助调用

        /// <summary>
        /// 设置波源
        /// </summary>
        /// <param name="x">波源坐标X</param>
        /// <param name="y">波源坐标Y</param>
        /// <param name="r">波源半径</param>
        /// <param name="h">波源的能量大小</param>
        public void SetWavePoint(int x, int y, int r, int h)
        {
            // 判断波源所在矩形位置是否越出图像 以便将越出部分坐标重置
            int nXStart = x - r < 0 ? 0 : x - r;        // 波源矩形位置x轴起点
            int nYStart = y - r < 0 ? 0 : y - r;        // 波源矩形位置y轴起点
            int nXLen = x   r >= m_nBmpWidth ? m_nBmpWidth - 1 : x   r;     // 波源x轴矩形长度
            int nYlen = y   r >= m_nBmpHeight ? m_nBmpHeight - 1 : y   r;   // 波源y轴矩形长度
            for (int posX = nXStart; posX < nXLen; posX  )
            {
                for (int posY = nYStart; posY < nYlen; posY  )
                {    // 以点(x,y)半径为r内的点赋值一个能量
                    if ((posX - x) * (posX - x)   (posY - y) * (posY - y) < r * r)
                        m_nArrWaveCurrent[posX, posY] = -h;
                }
            }
        }

        #endregion
    }
}


标签: C# 窗口 水波

实例下载地址

水波效果

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警