欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

桌面浮动窗口(类似恶意广告)的实现详解

程序员文章站 2024-02-13 10:39:52
突然想起来flash有碰撞反弹飘动as控制的效果,所以想起来用c#也来做一个桌面飘动碰撞反弹无标题栏窗体。有点像中了恶意病毒广告效果。主要代码如下(使用了一timer控件和...
突然想起来flash有碰撞反弹飘动as控制的效果,所以想起来用c#也来做一个桌面飘动碰撞反弹无标题栏窗体。有点像中了恶意病毒广告效果。
主要代码如下(使用了一timer控件和一button(为了我自己控制),窗体的borderstyle设置为none):
复制代码 代码如下:

        int screenwidth = systeminformation.primarymonitormaximizedwindowsize.width;
        int screenheight = systeminformation.primarymonitormaximizedwindowsize.height;
        private int speedx = 4;
        private int speedy = 3;
        private bool canmove = true;
        int myswitch = 1;//为了我可以控制停止所以添加的飘与停的切换开关
        private void timer1_tick(object sender, eventargs e)
        {
            if (canmove)
            {
                this.desktoplocation = new point(this.desktoplocation.x + speedx, this.desktoplocation.y + speedy);
                if (this.desktoplocation.x + this.width >= screenwidth || this.desktoplocation.x < 0)
                {
                    speedx = -speedx;
                }
                if (this.desktoplocation.y + this.height >= screenheight || this.desktoplocation.y < 0)
                {
                    speedy = -speedy;
                }
            }
        }
        private void button1_click(object sender, eventargs e)
        {
            myswitch *= -1;
            if (myswitch == -1)
            {
                canmove = false;
                //button1.text = "飘动";
            }
            else
            {
                canmove = true;
                //button1.text = "停止";
            }
        }
        private void form1_load(object sender, eventargs e)
        {
        }
        private void form1_doubleclick(object sender, eventargs e)
        {
            application.exit();
        }

暂写这么多,有时间把它再增强下更像恶意广告。~