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

C#制作网站挂机程序的实现示例

程序员文章站 2022-06-18 13:43:43
目录前言一、程序界面(如下图)二、使用说明1.界面说明2.使用注意点三、程序开发过程1.测试网页四、程序开发的一些其它思路及问题1.采用通过已知进程获取主窗口句柄再遍历子窗口句柄2.网上搜索c#操作a...

前言

真实的想法是用c#gui开发一个自动化测试的软件,能帮助我完成500台计算机在线网络答题的压力测试。既要能自动鼠标定位单击操作,也能键盘模拟输入字符信息。但其实想法很丰满,现实很骨感。目前还有一些问题有待于解决。但在完成这个项目的过程中,也产生了一些副产品,也是挺有成就感的。现就开发过程中出现的问题,找到并甄别测试过的一些参考资料,理解和研究心得奉献给大家。特别奉献一个完整的例子:针对网上挂机出现对话框,按回车才能继续。制作了一个简单的“挂机程序”,以此抛砖引玉。希望大家广开思路,共同研究。

提示:以下是本篇文章正文内容,下面案例可供参考

一、程序界面(如下图)

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

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#使用api函数,因为个人爱好不同,参数类型也是千差万别。真正的体验类的文章也比较少。谬误就更难解决了。后期,准备作一深入体验,同时准备写一个系列文章。虽然笔者能力有限,到时谬误肯定也不少,但笔者坚持所有内容注重实践。程序必须调试通过,正常才会放到网上。有志同道合的读者不妨关注一下笔者博客,敬请期待,下期再见!

到此这篇关于c#制作网站挂机程序的实现示例的文章就介绍到这了,更多相关c# 网站挂机程序内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C# 网站挂机