溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#如何制作網(wǎng)站掛機(jī)程序

發(fā)布時(shí)間:2021-11-01 09:09:02 來源:億速云 閱讀:213 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“C#如何制作網(wǎng)站掛機(jī)程序”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C#如何制作網(wǎng)站掛機(jī)程序”這篇文章吧。

    一、程序界面(如下圖)

    名稱:模擬鍵盤程序,為什么不用掛機(jī)程序,是因?yàn)槠涔δ苋跣?,針對的范圍窄,而且,它作為副產(chǎn)品,真心不是為掛機(jī)而作。請注意我們的目標(biāo)是:自動化網(wǎng)絡(luò)測試。

    C#如何制作網(wǎng)站掛機(jī)程序

    二、使用說明

    1.界面說明

    1.應(yīng)用程序路徑,這里針對FireFox瀏覽器,所以需要放程序的地址。
    2.網(wǎng)站地址:符合URL格式的能直接訪問的本地文件或者網(wǎng)址
    3.瀏覽器標(biāo)題:FireFox程序已經(jīng)對應(yīng)用程序標(biāo)題作了隱藏,如果看到標(biāo)題欄顯示:測試
    其實(shí)應(yīng)用程序的標(biāo)題應(yīng)該是:測試 — Mozilla Firefox
    4.【啟動瀏覽器】其實(shí)這個功能目前完全可以不去管,直接手動啟動FireFox即可。
    5.【Start】按鈕才是本質(zhì),這里將根據(jù)【瀏覽器標(biāo)題】內(nèi)容來查找
    到FireFox瀏覽網(wǎng)頁的真正【句柄】,另外如果找到,將顯示【句柄】的十進(jìn)制整數(shù)值,如果顯示0,表示未找到。
    6.【Stop】將定時(shí)器操作禁用。

    2.使用注意點(diǎn)

    1.顯示【句柄】位置啟動后,必須是非零值,如果是0,則修改【瀏覽器標(biāo)題】內(nèi)容,重新點(diǎn)【Start】
    2.必須保持FireFox瀏覽器在所有窗體的前面
    3.保證【計(jì)算機(jī)】不會進(jìn)入【睡眠】或者進(jìn)入【屏幕保護(hù)】狀態(tài)

    三、程序開發(fā)過程

    1.測試網(wǎng)頁

    1.文件名:test.html
    2.網(wǎng)頁代碼(如下):

    <!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();//實(shí)例化Timer定時(shí)器 
                myTimer.Tick += new EventHandler(CallBack2);//定時(shí)器關(guān)聯(lián)事件函數(shù)
            }
            private void CallBack2(object sender,EventArgs e)//定時(shí)器事件
            {
                keybd_event(Keys.Return, 0, 0, 0);//模擬鍵盤輸入:回車
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                myTimer.Enabled = false;//禁止定時(shí)器
            }
            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參數(shù)
                    Proc.StartInfo.UseShellExecute = false;
                    Proc.StartInfo.RedirectStandardInput = true;
                    Proc.StartInfo.RedirectStandardOutput = true;
                    Proc.Start();
                }
                catch
                {
                    Proc = null;
                }
            }
        }
    }

    四、程序開發(fā)的一些其它思路及問題

    1.采用通過已知進(jìn)程獲取主窗口句柄再遍歷子窗口句柄

    下面的代碼與本程序無關(guān),都是片斷,請不要直接復(fù)制使用,主要提供有興趣作提升的人參考。

    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)
    {
        //調(diào)用 API, 傳遞數(shù)據(jù)
        while (Proc.MainWindowHandle == IntPtr.Zero)
        {
            Proc.Refresh();
        }
    
        while (Proc.MainWindowHandle == IntPtr.Zero)
        {
            Proc.Refresh();
        }
        //執(zhí)行代碼略
    }

    【問題說明】:這里的線程是Proc,但這個線程并不是主窗體,這個線程的主窗體句柄需要通過Proc.MainWindowHandle獲取。在使用過程中,針對自己開發(fā)的C#GUI程序,很容易獲取到,對【記事本】程序也正常,但在針對【Chrome】瀏覽器的時(shí)候,則結(jié)果要么是0,要么異常,在針對【FireFox】的時(shí)候有時(shí)能正常獲取,有時(shí)是異常。

    2.網(wǎng)上搜索C#操作API函數(shù)的東西很少,且不完整

    這里提供一個API函數(shù)的查詢網(wǎng)站,基本把API函數(shù)一網(wǎng)打盡了。http://pinvoke.net/#

    3.如果想查看Windows下的API函數(shù),還可以使用工具:

    1.DLL函數(shù)查看器

    C#如何制作網(wǎng)站掛機(jī)程序

    2.DLL Export Viewer

    C#如何制作網(wǎng)站掛機(jī)程序

    4.獲取應(yīng)用程序窗體的句柄、標(biāo)題、類型的工具軟件

    這里可以使用VS環(huán)境(C++)中的Spy++。

    C#如何制作網(wǎng)站掛機(jī)程序

    以上是“C#如何制作網(wǎng)站掛機(jī)程序”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI