首页
社区
课程
招聘
[原创]C#编写的网页自动刷新器
发表于: 2013-4-25 19:17 4502

[原创]C#编写的网页自动刷新器

2013-4-25 19:17
4502
第一次发布程序资源贴,不足之处,还望多多指教。

  C#的一个大好处就是界面做起来毫不费力,因此重点在于功能的实现,在这个小程序中,涉及到C#的不同窗体传参、传值,读取当前系统时间、读取程序运行了多长时间等等,在C#中对于网页(URL)有特别苛刻的要求,我的这个程序只考虑到了一小部分因素,因此,当输入非法链接的时候,程序有可能会报错。

  另外,程序的倒数计时是我额外添加的一个timer组件,由于程序运行过程中,线程的不同步,倒数计时的时间可能会与实际时间不同步,强迫症患者用这个软件可能会遭殃了,对于这些问题,我会后续改进,然后发布更完美的后续版本。

  如果你有任何问题,或者发现了程序的任何问题,请联系我:mail to me [EMAIL="zxyisc@foxmail.com"]zxyisc@foxmail.com[/EMAIL]

  本程序需要Microsoft .net framework 4.0支持,如果你的电脑上没有安装,请到这里下载安装。

程序界面:


  【还在为Kx太少而担忧吗?还在为一段时间无操作就被论坛版主踢下线而烦恼么?试试网页自动刷新工具吧!】
  诚然,很多浏览器都自带自动刷新的工具,或者开发一个插件便可实现的功能,我这也是只想试试检验一下自己的编程能力,看看能不能实现我心目中的功能,我这也是第一次正规地去编出一个实用型程序,还希望大家不吝赐教

主窗体实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoRefresh
{

    public partial class FormMain : Form
    {
        public FormMain()
        {

            InitializeComponent();
        }

       // public void getid(string id)    //text文本框传值法则1(代码1)
       // {
        //   textBoxMainForm.Text = id;
      // }


        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 关于AutoRefreshAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAbout f1 = new FormAbout();
            f1.Size = new Size(400 , 200);
            f1.Text="关于网页刷新器";
            f1.FormBorderStyle = FormBorderStyle.FixedSingle;
            f1.MinimizeBox = false;
            f1.MaximizeBox = false;
            f1.ShowIcon = false;
            f1.Show();
        }
        private void 输入网址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //FormNew formnew1=new FormNew();
            //formnew1.FormBorderStyle = FormBorderStyle.FixedSingle;
            //formnew1.Show();
            FormNew form = new FormNew();
            form.GetForm(this);
            form.FormBorderStyle = FormBorderStyle.FixedSingle;
            form.Show();
        }
        public void GetText(String text)
        {
            string value = text;
        }

        public void textBox2_TextChanged(object sender, EventArgs e)
        {

        }


        public void textBoxMainForm_TextChanged(object sender, EventArgs e)
        {
           // String myurl = textBoxMainForm.Text;
           // string myurl = textBoxMainForm.Text;
           // webBrowser1.Url = new Uri(myurl.Trim());//加一个.trim去掉链接首尾空格
           // webBrowser1.Url = new Uri(myurl);
            webBrowser1.Url = new Uri(textBoxMainForm.Text);
           
        }


        private bool windowCreate = true;
        private void 后台运行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (windowCreate)
            {
                base.Visible = false;
                windowCreate = false;
            }
            this.Hide();
            base.OnActivated(e); 
        }
        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            //双击任务栏右侧通知区域的图标,程序的主窗口显示出来
            if (e.Button == MouseButtons.Left)   //如果双击的是鼠标左键,执行下列操作
            {
                if (this.Visible == true)
                {
                    //this.Visible = false;
                    //this.ShowInTaskbar = false;
                    if (windowCreate)      //调用后台运行的函数,隐藏所有窗口
                    {
                        base.Visible = false;
                        windowCreate = false;
                    }
                    this.Hide();
                    base.OnActivated(e); 
                }
                else
                {
                    this.Visible = true;
                    this.ShowInTaskbar = true;
                }
               
            }
            if (e.Button == MouseButtons.Right)   //如果双击的是鼠标右键,不执行任何操作
            { 

            }
        }

        FormTime ft;
        private void 刷新时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // FormTime ft = new FormTime(webBrowser1);//传参数到FormTime中去,使得FormTime可以调用FormMain的对象webbrowser
            ft = new FormTime();
            ft.GetForm(this);
            ft.Show();
            ft.FormBorderStyle = FormBorderStyle.FixedSingle;
            if (textBoxMainForm.Text == "")
                webBrowser1.Url = new Uri("http://www.hnu.edu.cn");  //为了防止无网页空刷新调用出错
           // else return;
        }


        [DllImport("user32", EntryPoint = "HideCaret")]
        private static extern bool HideCaret(IntPtr hWnd);       //隐藏地址栏鼠标光标
        private void textBoxMainForm_MouseDown(object sender, MouseEventArgs e)
        {
            HideCaret((sender as TextBox).Handle);
        }
        void textBoxMainForm_GotFocus(object sender, EventArgs e)    //隐藏地址栏鼠标光标
        {
            HideCaret((sender as TextBox).Handle);
        }

        DateTime dtstart = DateTime.Now;        //获取程序启动时的系统时间
        public void FormMain_Load(object sender, EventArgs e)   //当FormMain窗体运行时,要执行的操作
        {
            textBoxMainForm.GotFocus += textBoxMainForm_GotFocus;        //隐藏地址栏鼠标光标
            timercurrentTime.Start();              //显示当前系统时间的计时器开始计时
            timerProgramRUN.Start();              //从程序运行的那一刻起开始计时,显示程序运行了多长时间
        }

        int timeRemaining;
        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {
            timerAutoF5.Interval = Convert.ToInt32(Convert.ToSingle(textBox2.Text) * 1000);  //自动刷新的计时器的触发频率
            daojishi.Interval = 1000;    //设置倒计时器的触发频率是1s一次
            timeRemaining = Convert.ToInt32(Convert.ToSingle(textBox2.Text));  //当时间的textbox值改变时,直接把值赋给timeRemaining
            labelF5remain.Text = textBox2.Text;  //令labelF5remain的值初始显示为所设置的刷新间隔时间

            daojishi.Start();    //启动倒计时器,这里只是为了保证第一次就能正常计时
            timerAutoF5.Enabled = true;    //启动自动刷新计时器
            //daojishi.Stop(); 
        }

        
        private void daojishi_Tick(object sender, EventArgs e)
        {  
            //timeRemaining = Convert.ToInt32(Convert.ToSingle(textBox2.Text));
            /*
             * 将①放在②前面是为了使计时器从我们输入的值开始倒计时,而非输入的值减1开始倒计时
             * 这个if判断语句的目的是使第一次刷新时,label值显示正常
             */
               if (labelF5remain.Text == timeRemaining.ToString())
               {
                   timeRemaining--;                                //②
                   labelF5remain.Text = timeRemaining.ToString();  //①
               }
               else
               {
                labelF5remain.Text = timeRemaining.ToString();  //①
                timeRemaining--;                                //②
               }
        }


        int f5account = 0; //【重要!!】记住,一定要在函数外面初始化f5account!
        private void AutoF5_Tick(object sender, EventArgs e)
        {
            timeRemaining = Convert.ToInt32(Convert.ToSingle(textBox2.Text));
            daojishi.Start();    //启动倒计时器
          if(textBoxMainForm.Text=="")
                webBrowser1.Navigate(new Uri("http://www.hnu.edu.cn"));    //为了防止无网页的空刷新
         else
           
           webBrowser1.Navigate(new Uri(textBoxMainForm.Text));
          
           f5account = f5account + 1; //在这里做一个计数器的,计算刷新次数 
           this.labelF5account.Text = f5account.ToString();
        }

      //  public void shuaxinyemian()
      //  {
      //      if (textBoxMainForm.Text == "")
      //          webBrowser1.Navigate(new Uri("http://www.hnu.edu.cn"));    //为了防止无网页的空刷新
      //      else
      //          webBrowser1.Navigate(new Uri(textBoxMainForm.Text));
      //      f5account = f5account + 1; //在这里做一个计数器的,计算刷新次数
      //
      //      int nowaccount = f5account;
       //     this.labelF5account.Text = nowaccount.ToString();
       // }

        private void currentTime_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;     //实例化对象捕获系统当前时间
            string t = dt.ToLongTimeString();  //将此实例的值转化为等效时间字符串值
            this.LabelcurrentTime.Text = t;
            
        }

        public void timerProgramRUN_Tick(object sender, EventArgs e)
        {
            //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            //sw.Start();
            //int s = sw.Elapsed.Seconds;
            //int m = sw.Elapsed.Minutes;
            //int h = sw.Elapsed.Hours;
            //string timerun=h+":"+m+":"+s;
            DateTime dtnow = DateTime.Now;
            TimeSpan timerun1 = dtnow.Subtract(dtstart);            //获取当前系统时间与程序启动时间的差值,即为程序运行时间
            string timerun = timerun1.Hours.ToString() + ":"+timerun1.Minutes.ToString()+":"+timerun1.Seconds.ToString();
            this.labelProgramRUNTIME.Text = timerun;
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            //在任务栏图标单击鼠标左右键时,所触发的事件

        }

        private void 显示主窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.ShowInTaskbar = true;

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

       

        private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormReadMe form = new FormReadMe();
            form.FormBorderStyle = FormBorderStyle.FixedSingle;
            form.Show();
        }


 
    }
}



输入刷新间隔时间的窗体中,对于输入的正则判断以及父子窗体传值代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace AutoRefresh
{
    public partial class FormTime : Form
    {
      //  public WebBrowser webb;   //本来是作为传参用的,结果发现了bug
        public FormTime()
        {
            InitializeComponent();
        }
       // public FormTime(WebBrowser w)   //传参数,使得类2可以调用类1的对象(formTime调用FormMain的对象webbrowser)
       // {
       //   webb = w;
       //     InitializeComponent();
       //  }
        public int f5time = 0;                 //向父Form的textbox中传值
        public FormMain form = null;              //向父Form的textbox中传值
        public void GetForm(FormMain theform)     //向父Form的textbox中传值,getform函数
        {
            form = theform;
        }

        private void buttonTimeCancel_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }
        
        private void buttonTimeOK_Click(object sender, EventArgs e)
        {

            if (textBoxTime.Text == "")
                MessageBox.Show("请输入刷新间隔时间!");
            else if (textBoxTime.Text == "0" || textBoxTime.Text == "1" || textBoxTime.Text == "2" || textBoxTime.Text == "3" || textBoxTime.Text == "4")
                MessageBox.Show("请输入一个不小于5的数!");
            else
            {
               // webb.Url = new Uri("http://www.hnu.edu.cn");  
                form.textBox2.Text = textBoxTime.Text;
                this.Close();
            }
        }


        private void textBoxTime_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("^[0-9]{1,}$");
            Regex r0 = new Regex("^[1-9]{1,}$");
           // if (textBoxTime.Text == "")
            //{
                // MessageBox.Show("请输入刷新间隔时间!");
           //     return;
          //  }
           // else
            if (textBoxTime.Text.Length == 1)
            {
                if (!r0.IsMatch(textBoxTime.Text))   //控制首位不能为0!
                {
                    MessageBox.Show("首位请不要输入非1-9的数!");
                    // textBoxTime.Text = textBoxTime.Text.Substring(0, textBoxTime.Text.Length - 1);
                    textBoxTime.Text = "";
                    return;
                }
                else
                {
                    // textBoxTime.Text = textBoxTime.Text.Substring(0, textBoxTime.Text.Length - 1);
                    return;
                }
            }
            else
            {
                if (!r.IsMatch(textBoxTime.Text))
                {
                    if (textBoxTime.Text.Length <= 1)
                        return;
                    else
                    {
                        textBoxTime.Text = textBoxTime.Text.Substring(0, textBoxTime.Text.Length - 1);
                        MessageBox.Show("请输入数字!");
                        textBoxTime.Focus();
                        textBoxTime.SelectionStart = this.textBoxTime.TextLength;      //锁定光标的位置在字段尾部

                    }
                }
            }
        }
     


        private void FormTime_InputLanguageChanging(object sender, EventArgs e)
        {

        }



    }
}


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

上传的附件:
收藏
免费 0
支持
分享
最新回复 (11)
雪    币: 19
活跃值: (74)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
楼主,图挂了。
2013-4-25 21:02
0
雪    币: 496
活跃值: (286)
能力值: ( LV13,RANK:400 )
在线值:
发帖
回帖
粉丝
3
输入参数不校验,
规定最低刷新间隔是5
可是我只要输入一个01就突破了。。

刷新后没有等待Server的response。

为何数据输入非要弹出一个窗口才可以呢?这种体验不是很好。

努力。。改进改进
2013-4-25 21:14
0
雪    币: 106
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
嗯,蕴藏的bug肯定还有,我这技术还亟待提升,回去优化去!

关键是时间函数很坑,我设计了好久也只能用这种弃车保帅的方式控制倒计时刷新……

找了好久都没找到如何读取timer.Interval的即时时间
2013-4-25 21:53
0
雪    币: 398
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
俺就是用的傲游浏缆器打开这个论坛,然后用浏览器有自带的自动刷新功能,然后我就升为正式会员了。。在这里自己偷笑一下。。
2013-4-25 22:07
0
雪    币: 84
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
好像不少浏览器都自带吧?
2013-4-26 09:45
0
雪    币: 102
活跃值: (452)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
啥时候完善呀,等源码呢
2013-6-1 22:55
0
雪    币: 428
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
我想知道C#该用什么逆向工具??OD貌似不好用啊!
2013-6-3 00:07
0
雪    币: 28
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
试试。。。。。。。。。
2013-6-3 17:05
0
雪    币: 86
活跃值: (17)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
搂主原码拿给我学习学习。QQ:82272756
2013-6-3 23:03
0
雪    币: 0
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
刷新我一直都用  suasua.com
2014-4-9 19:00
0
雪    币: 0
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
刚好有别的地方需要,省得再造轮子,谢谢!
2019-8-26 11:50
0
游客
登录 | 注册 方可回帖
返回
//