日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

MFC和c#中模拟对另一进程的窗口按钮点击

發布時間:2023/12/18 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MFC和c#中模拟对另一进程的窗口按钮点击 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、

在自動化測試中經常要模擬窗口按鈕的點擊。

參考文章:http://blog.csdn.net/blackboyofsnp/article/details/3372719

有時我們需要這么做, 手動模擬按鈕按下的事件, 讓程序做出與按鈕真的按下時一樣的響應.

???設按鈕ID為ID_BTN_OK, 當前Dialog窗口.
???實際上系統并不能區分按鈕是真的按下了(如用鼠標點擊), 還是一個我們自己用代碼模擬出的一種"假象".
它所需要知道的只是哪個窗口(按鈕也是一種窗口)發生了哪一種事件, 然后在消息循環中處理它. 系統怎么才
能知道這些呢? 當然靠的是消息(message), 我們 只需按照Windows或者MFC的標準格式把相應的信息傳給系統,
系統就會"上當"了.
???向系統傳遞消息可以用SendMessage或PostMessage(可能還有其他很多函數哦), 但SendMessage執行后系統
會一直等待, 直到要發送的消息被處理掉. 而PostMessage可不管那么多, 發送消息后立即返回程序流程. 當
按鈕按下的響應函數中有很大一個循環時, 用SendMessage會出現許多問題, 尤其是要在程序初始化階段模擬時,
會導致窗體無法完成初始化. 所以我們用PostMessage(). 它的原型為:
BOOL PostMessage( ? ? ? ? ?HWND hWnd,
? ? UINT Msg,
? ? WPARAM wParam,
? ? ?LPARAM lParam
);

這樣寫;??

PostMessage(WM_COMMAND, MAKEWPARAM(ID_BTN_OK, BN_CLICKED), NULL);
這里, WM_COMMAND是要發送的消息, MAKEWPARAM宏是為了組成一個WPARAM,WM_COMMAND消息的WPARAM的低字為控件ID,高字為識別碼, 最后一個參數LPARAM可為NULL.相關定義可查看MSDN.

??這樣我們就把必需的信息格式化好發送給系統了. 當系統在消息循環中收到該消息時, 就知道哦, 你要引發控件ID_BTN_OK
的事件BN_CLICKED, 好的我幫你處理. 于是我們就驚喜地看到按鈕看起來真的按下去了, 并執行了和真正按下去時一樣的代碼.
?

看評論說不管用,我又試驗了一下。新建一個對話框工程,在對話框上添加一個按鈕,ID為IDC_BTN_TEST,單擊它,為它添加ON_BN_CLICKED消息響應函數:

void CtestDlg::OnBnClickedBtnTest()
{
? ? AfxMessageBox(_T("OK"));
}
然后在對話框的OnInitDialog()函數的return TRUE前加上:

PostMessage(WM_COMMAND, MAKEWPARAM(IDC_BTN_TEST, BN_CLICKED), NULL);
好了,再運行程序,會彈出個消息框 “OK”,說明模擬正確。

?

?2、?C#調用迅雷的時候 自動模擬點擊”下載按鈕” 關閉彈出窗口

網上 關于 “不彈出《建立任務》的對話框的方法 在迅雷5.9貌似不適用了” 那么我改了一下網上的“監聽方式”來進行模擬點擊

#region Dll Import 需要導入的api 聲明。
?
? ? ? ? [DllImport("User32.dll", EntryPoint = "FindWindow")]
? ? ? ? private static extern IntPtr FindWindow(string lpClassName,
? ? ? string lpWindowName);
?
? ? ? ? [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
? ? ? ? private static extern IntPtr FindWindowEx(IntPtr hwndParent,
? ? ? IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
?
? ? ? ? [DllImport("User32.dll", EntryPoint = "SendMessage")]
? ? ? ? private static extern int SendMessage(IntPtr hWnd,
? ? ? int Msg, IntPtr wParam, string lParam);
? ? ? ? const int WM_GETTEXT = 0x000D;
? ? ? ? const int WM_SETTEXT = 0x000C;
? ? ? ? const int WM_CLICK = 0x00F5;
? ? ? ? #endregion
? ? ? ? //關消息的方法
? ? ? ? private void ClosePopWindow(object obj)
? ? ? ? {
? ? ? ? ? ? //這些用spy++可以看到
? ? ? ? ? ? string lpszParentClass = "#32770"; //整個窗口的類名
? ? ? ? ? ? string lpszParentWindow = "建立新的下載任務"; //窗口標題
? ? ? ? ? ? string lpszClass_Submit = "Button"; //需要查找的Button的類名
? ? ? ? ? ? string lpszName_Submit = "立即下載"; //需要查找的Button的標題 ? ? ? ? ?
?
? ? ? ? ? ? IntPtr ParenthWnd = new IntPtr(0);
? ? ? ? ? ? IntPtr EdithWnd = new IntPtr(0);
? ? ? ? ? ? int i = 0;
? ? ? ? ? ? while (true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //查到窗體,得到整個窗體
? ? ? ? ? ? ? ? ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
? ? ? ? ? ? ? ? //判斷這個窗體是否有效
? ? ? ? ? ? ? ? if (!ParenthWnd.Equals(IntPtr.Zero))
? ? ? ? ? ? ? ? {
?
? ? ? ? ? ? ? ? ? ? //得到第一級子窗口
? ? ? ? ? ? ? ? ? ? EdithWnd = FindWindowEx(ParenthWnd,
? ? ? ? ? ? ? ? ? ? ? ? new IntPtr(0), "#32770", "");
? ? ? ? ? ? ? ? ? ? //Console.WriteLine("第一級-"+EdithWnd.ToString());
? ? ? ? ? ? ? ? ? ? //得到Button這個子窗體,并觸發它的Click事件
? ? ? ? ? ? ? ? ? ? EdithWnd = FindWindowEx(EdithWnd,
? ? ? ? ? ? ? ? ? ? ? ? new IntPtr(0), lpszClass_Submit, lpszName_Submit);
? ? ? ? ? ? ? ? ? ? //Console.WriteLine("第二級-" + EdithWnd.ToString());
? ? ? ? ? ? ? ? ? ? if (!EdithWnd.Equals(IntPtr.Zero))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Thread.Sleep(1000);
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? // ?Console.WriteLine("第"+i.ToString()+"次檢查"); 5秒都沒顯示出來就推出循環
? ? ? ? ? ? ? ? if (i > 15)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }?
?
//需要導入如下類庫
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
?
//在迅雷提交前添加一個方法
ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));

?3、

#include <iostream>
#include <fstream>
#include <math.h>
#include <cctype>
#include <string>
#include <windows.h>
?
using namespace std;
?
int main()
{
? ? //cout << "Hello world!" << endl;
? ? HWND hwnd = FindWindow( 0, "文件窗口" ?);
?
?
? ? //HWND hWnd2 = ?GetDlgItem( hwnd, 1001);
?
? ? char* strs = new char[ 255 ];
? ? HWND hWnd2 = ::FindWindowEx(hwnd,NULL,"Button",NULL);
?
? ? while ( hWnd2 )
? ? {
? ? ? ? GetWindowText( hWnd2, strs, 255 );
?
?
? ? ? ? cout << strs << endl;
?
?
?
? ? ? ?/* if ( strcasecmp( strs, "確定" ) == 0 ) {
? ? ? ? ? ? break;
? ? ? ? }*/
?
? ? ? ? hWnd2 = FindWindowEx( hwnd, hWnd2, "Button", NULL );
? ? }
?
?
?
?
? ? UINT nCtrlID = ::GetDlgCtrlID(hWnd2);
? ? ::PostMessage(hWnd2, WM_COMMAND, (WPARAM)(BN_CLICKED << 16 | nCtrlID), (LPARAM)hWnd2);
? ? ::PostMessage(hWnd2,WM_MOUSEMOVE, MK_LBUTTON, MAKELONG(0,0) );
? ? ::PostMessage(hWnd2,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(0,0));
? ? ::PostMessage(hWnd2,WM_LBUTTONUP,MK_LBUTTON,MAKELPARAM(0,0));
?
? ? return 0;
}

?

4、往編輯框中寫入文件(可實現)

SetWindowText
?

總結

以上是生活随笔為你收集整理的MFC和c#中模拟对另一进程的窗口按钮点击的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。