C#如何制作网站挂机程序

这篇文章主要为大家展示了“C#如何制作网站挂机程序”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#如何制作网站挂机程序”这篇文章吧。

    一、程序界面(如下图)

    名称:模拟键盘程序,为什么不用挂机程序,是因为其功能弱小,针对的范围窄,而且,它作为副产品,真心不是为挂机而作。请注意我们的目标是:自动化网络测试。

    C#如何制作网站挂机程序

    二、使用说明

    1.界面说明

    1.应用程序路径,这里针对FireFox浏览器,所以需要放程序的地址。
    2.网站地址:符合URL格式的能直接访问的本地文件或者网址
    3.浏览器标题:FireFox程序已经对应用程序标题作了隐藏,如果看到标题栏显示:测试
    其实应用程序的标题应该是:测试 — Mozilla Firefox
    4.【启动浏览器】其实这个功能目前完全可以不去管,直接手动启动FireFox即可。
    5.【Start】按钮才是本质,这里将根据【浏览器标题】内容来查找
    到FireFox浏览网页的真正【句柄】,另外如果找到,将显示【句柄】的十进制整数值,如果显示0,表示未找到。
    6.【Stop】将定时器操作禁用。

    2.使用注意点

    1.显示【句柄】位置启动后,必须是非零值,如果是0,则修改【浏览器标题】内容,重新点【Start】
    2.必须保持FireFox浏览器在所有窗体的前面
    3.保证【计算机】不会进入【睡眠】或者进入【屏幕保护】状态

    三、程序开发过程

    1.测试网页

    1.文件名:test.html
    2.网页代码(如下):

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>测试</title>
    	<meta charset="utf-8" />
    </head>
    <body>
        <script language="javascript">
            alert("ok");
        </script>
    </body>
    </html>

    2.程序完整代码

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace 模拟键盘
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
            public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
             [DllImport("user32.dll", EntryPoint = "FindWindow")]
            private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
            //查找窗体控件
            public int iSeconds=30;
            public delegate bool CallBack(int hwnd, int lParam);
            public Process Proc = new Process();
            
            public System.Windows.Forms.Timer myTimer;
            public Form1()
            {
                InitializeComponent();
            }
            private void btnBrowser_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "*.exe|*.exe|所有文件|*.*";
                openFileDialog1.FileName = "";
                DialogResult dr = openFileDialog1.ShowDialog();
                if(DialogResult.OK==dr)
                {
                    txtFile.Text = openFileDialog1.FileName;
                }
            }
            string str = "Message";
            int iP = 0;
            private void btnStart_Click(object sender, EventArgs e)
            {
                
                IntPtr hnd = FindWindow(null, txtTitle.Text);//获取句柄
                lblMessage.Text = hnd.ToString();
                iSeconds = int.Parse(txtSeconds.Text.Trim());
                myTimer.Interval = 1000 * iSeconds;  //1秒=1000毫秒
                myTimer.Enabled = true;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                myTimer = new System.Windows.Forms.Timer();//实例化Timer定时器 
                myTimer.Tick += new EventHandler(CallBack2);//定时器关联事件函数
            }
            private void CallBack2(object sender,EventArgs e)//定时器事件
            {
                keybd_event(Keys.Return, 0, 0, 0);//模拟键盘输入:回车
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                myTimer.Enabled = false;//禁止定时器
            }
            private void btnStartBrowser_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(txtFile.Text)) return;
                try
                {
                    // 浏览器程序启动线程
                    Proc = new System.Diagnostics.Process();
                    Proc.StartInfo.FileName = txtFile.Text;
                    Proc.StartInfo.Arguments = txtNetAddr.Text;  //浏览器打开URL参数
                    Proc.StartInfo.UseShellExecute = false;
                    Proc.StartInfo.RedirectStandardInput = true;
                    Proc.StartInfo.RedirectStandardOutput = true;
                    Proc.Start();
                }
                catch
                {
                    Proc = null;
                }
            }
        }
    }

    四、程序开发的一些其它思路及问题

    1.采用通过已知进程获取主窗口句柄再遍历子窗口句柄

    下面的代码与本程序无关,都是片断,请不要直接复制使用,主要提供有兴趣作提升的人参考。

    if (string.IsNullOrEmpty(txtFile.Text)) return;
    try
    {
        Proc = new System.Diagnostics.Process();
        Proc.StartInfo.FileName =txtFile.Text;
        Proc.StartInfo.Arguments = txtNetAddr.Text;
        Proc.StartInfo.UseShellExecute = false;
        Proc.StartInfo.RedirectStandardInput = true;
        Proc.StartInfo.RedirectStandardOutput = true;
        Proc.Start();
        Thread.Sleep(2000);
    }
    catch
    {
        Proc = null;
    }
    if (Proc.MainWindowHandle != null)
    {
        //调用 API, 传递数据
        while (Proc.MainWindowHandle == IntPtr.Zero)
        {
            Proc.Refresh();
        }
    
        while (Proc.MainWindowHandle == IntPtr.Zero)
        {
            Proc.Refresh();
        }
        //执行代码略
    }

    【问题说明】:这里的线程是Proc,但这个线程并不是主窗体,这个线程的主窗体句柄需要通过Proc.MainWindowHandle获取。在使用过程中,针对自己开发的C#GUI程序,很容易获取到,对【记事本】程序也正常,但在针对【Chrome】浏览器的时候,则结果要么是0,要么异常,在针对【FireFox】的时候有时能正常获取,有时是异常。

    2.网上搜索C#操作API函数的东西很少,且不完整

    这里提供一个API函数的查询网站,基本把API函数一网打尽了。http://pinvoke.net/#

    3.如果想查看Windows下的API函数,还可以使用工具:

    1.DLL函数查看器

    C#如何制作网站挂机程序

    2.DLL Export Viewer

    C#如何制作网站挂机程序

    4.获取应用程序窗体的句柄、标题、类型的工具软件

    这里可以使用VS环境(C++)中的Spy++。

    C#如何制作网站挂机程序

    以上是“C#如何制作网站挂机程序”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!