一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

c# 調(diào)用API訪問其他窗體上的文本和執(zhí)行單擊事件等等

 長江黃鶴 2019-11-06

首先新建WinAPIuser32類(API聲明類)

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

 

namespace WinAPITest

{

    public class WinAPIuser32

    {

        /// <summary>

        /// 根據(jù)句柄得到控件ID

        /// </summary>

        /// <param name="hwndCtl"></param>

        /// <returns></returns>

        [DllImport("user32.dll")]

        public static extern int GetDlgCtrlID(IntPtr hwndCtl);

        /// <summary>

        /// 由控件ID獲得控件窗口句柄

        /// </summary>

        /// <param name="hDlg"></param>

        /// <param name="nIDDlgItem"></param>

        /// <returns></returns>

        [DllImport("user32.dll ", EntryPoint = "GetDlgItem")]

        public static extern IntPtr GetDlgItem(

        IntPtr hDlg,

        int nIDDlgItem

        );

        /// <summary>

        ///  查找句柄

        /// </summary>

        /// <param name="lpClassName"></param>

        /// <param name="lpWindowName"></param>

        /// <returns></returns>

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]

        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>

        /// 查找句柄

        /// </summary>

        /// <param name="hwndParent"></param>

        /// <param name="hwndChildAfter"></param>

        /// <param name="lpszClass">類名</param>

        /// <param name="lpszWindow">標(biāo)題</param>

        /// <returns></returns>

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]

        public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]

        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);

        /// <summary>

        /// 發(fā)送消息

        /// </summary>

        /// <param name="hwnd"></param>

        /// <param name="wMsg"></param>

        /// <param name="wParam"></param>

        /// <param name="lParam"></param>

        /// <returns></returns>

        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]

        public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

        [DllImport("User32.dll")]

        public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

        /// <summary>

        /// 發(fā)送文本消息

        /// </summary>

        /// <param name="hWnd"></param>

        /// <param name="Msg"></param>

        /// <param name="wParam"></param>

        /// <param name="lParam"></param>

        /// <returns></returns>

        [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]

        public static extern int SendTextMessage(

            IntPtr hWnd, // handle to destination window

            int Msg, // message

            int wParam, // first message parameter

            string lParam

            // int  lParam // second message parameter

        );

        /// <summary>

        /// 獲取焦點(diǎn)

        /// </summary>

        /// <param name="hwnd"></param>

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]

        public static extern void SetForegroundWindow(IntPtr hwnd);

 

        [DllImport("user32.dll")]

        public static extern int SetWindowText(int hwnd, string lpString);

 

        [DllImport("user32.dll")]

        public static extern int SetFocus(int hWnd);

 

        [DllImport("user32.dll")]

        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

        /// <summary>

        /// 回調(diào)業(yè)務(wù)

        /// </summary>

        public delegate void CallBusiness(IntPtr hwnd);

        public delegate bool CallBack(IntPtr hwnd, int lParam);

        /// <summary>

        /// 遍歷子窗體的父窗體句柄

        /// </summary>

        public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);

        /// <summary>

        /// 委托業(yè)務(wù),需要客戶端添加

        /// </summary>

        public static CallBusiness CallFuntion;

        /// <summary>

        /// 遍歷子窗體或控件

        /// </summary>

        /// <param name="hWnd"></param>

        /// <param name="lParam"></param>

        /// <returns></returns>

        public static bool EnumChildWindows(IntPtr hWnd, int lParam)

        {

            EnumChildWindows(hWnd.ToInt32(), callBackEnumChildWindows, 0);

            return true;

        }

        /// <summary>

        /// 獲取類名字

        /// </summary>

        /// <param name="hwnd">需要獲取類名的句柄</param>

        /// <param name="lpClassName">類名(執(zhí)行完成以后查看)</param>

        /// <param name="nMaxCount">緩沖區(qū)</param>

        /// <returns></returns>

        [DllImport("user32.dll", EntryPoint = "GetClassName")]

        public static extern int GetClassName(

            IntPtr hwnd,

            StringBuilder lpClassName,

            int nMaxCount

        );

        /// <summary>

        /// 遍歷子控件

        /// </summary>

        /// <param name="hwnd"></param>

        /// <param name="lParam"></param>

        /// <returns></returns>

        public static bool ChildWindowProcess(IntPtr hwnd, int lParam)

        {

            if (CallFuntion != null)

            {

                CallFuntion(hwnd);

            }

            return true;

        }

        [DllImport("user32.dll", EntryPoint = "PostMessage")]

        public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

 

        /// <summary>

        /// 最大化窗口(3),最小化窗口(2),正常大小窗口(1)第二個(gè)參數(shù);

        /// </summary>

        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]

        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

 

        /// <summary>

        /// 移動(dòng)窗體

        /// </summary>

        [DllImport("user32.dll", EntryPoint = "MoveWindow")]

        public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, int bRepaint);

 

        /// <summary>

        /// 鼠標(biāo)移動(dòng)

        /// </summary>

        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]

        public static extern int SetCursorPos(int x, int y);

 

        //[DllImport("user32.dll", EntryPoint = "SendMessage")]

        //public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);

 

        //[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]

        //private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

 

    }

}

再建一個(gè)API消息編碼類

using System;

using System.Collections.Generic;

using System.Text;

 

namespace WinAPITest

{

    /// <summary>

    /// 消息編碼

    /// </summary>

    public class MSCODE

    {

        /// <summary>

        /// 鼠標(biāo)左鍵按下

        /// </summary>

        public const int WM_LBUTTONDOWN = 0x201;

        /// <summary>

        /// 鼠標(biāo)左鍵抬起

        /// </summary>

        public const int WM_LBUTTONUP = 0x202;

        /// <summary>

        /// 設(shè)置文本

        /// </summary>

        public const int WM_SETTEXT = 0xC;

 

        public const int VK_TAB = 0x9;

        public const int WM_KEYDOWN = 0x100;

        public const int WM_KEYUP = 0x101;

        public const int WM_CHAR = 0x102;

        public const int WM_SETFOCUS = 0x007;

        /// <summary>

        /// 回車鍵

        /// </summary>

        public const int VK_RETURN = 13;

        /// <summary>

        /// 回車鍵

        /// </summary>

        public const int VK_RETURN1 = 0x0D;

 

        /// <summary>

        ///字母K

        /// </summary>

        public const int VK_K = 0x4B;

        /// <summary>

        ///F5

        /// </summary>

        public const int VK_F5 = 0x74;

        /// <summary>

        ///單擊事件

        /// </summary>

        public const int BM_CLICK = 0xF5;

 

    }

}

 

新建WINFORM和WINFORM1

WINFORM1的窗體名稱為Form1aa//隨便拖幾個(gè)控件

然后再單擊事件中寫代碼

 

        private void button1_Click(object sender, EventArgs e)

        {

            Random rand = new Random();

            IntPtr hwndCalc = WinAPIuser32.FindWindow(null, "Form1aa");//獲取窗體的句柄

            //WinAPIuser32.ShowWindow(hwndCalc, 1);//最小化窗體

            IntPtr hwndThree = WinAPIuser32.FindWindowEx(hwndCalc, 0, null, "123");

            WinAPIuser32.SendMessage(hwndThree, MSCODE.BM_CLICK, 0, 0);//單擊Form1aa中的123按鈕

            //WinAPIuser32.PostMessage(hwndThree, MSCODE.BM_CLICK, 0, 0);//

            //WinAPIuser32.PostMessage(hwndThree, MSCODE.WM_LBUTTONDOWN,0 , 0);//鼠標(biāo)按下

            //WinAPIuser32.PostMessage(hwndThree, MSCODE.WM_LBUTTONUP, 0, 0);//鼠標(biāo)臺(tái)起 組成單擊事件

 

            //int x = rand.Next(800);

            //int y = rand.Next(600);

            //int nWidth = rand.Next(50, 500);

            //int nHeight = rand.Next(50, 500);

            //WinAPIuser32.MoveWindow(hwndThree, x, y, nWidth, nHeight, 1);//移動(dòng)窗體

            //WinAPIuser32.SetCursorPos(x, y);//移動(dòng)鼠標(biāo)

 

            if (hwndCalc != IntPtr.Zero)

            {

                List<IntPtr> list = new List<IntPtr>();//存儲(chǔ)所有控件的緩沖區(qū)

                WinAPIuser32.CallFuntion = delegate(IntPtr enumIntPtr)//定義委托業(yè)務(wù)

                {

                    StringBuilder s = new StringBuilder(2000);

                    WinAPIuser32.GetClassName(enumIntPtr, s, 255);

                    list.Add(enumIntPtr);

                };

                WinAPIuser32.EnumChildWindows(hwndCalc, 0);//把獲取窗體控件的句柄寫入緩沖區(qū)

                WinAPIuser32.CallFuntion = null;

                foreach (IntPtr a in list)

                {

                    WinAPIuser32.SendTextMessage(a, MSCODE.WM_SETTEXT, 0, "mextb1860第二個(gè)文本框");

                }//寫入所有文本框的

 

                //WinAPIuser32.PostMessage(list[2], MSCODE.WM_KEYDOWN, MSCODE.VK_RETURN, 0);//發(fā)送回車鍵

                //WinAPIuser32.PostMessage(list[2], MSCODE.WM_KEYDOWN, MSCODE.VK_K, 0);//發(fā)送K

                //WinAPIuser32.PostMessage(list[3], MSCODE.WM_KEYDOWN, MSCODE.VK_RETURN1, 0);//

                //2個(gè)文本框

                //IntPtr hwndThree = WinAPIuser32.FindWindowEx(hwndCalc, 0, null, "確定");

                // WinAPIuser32.SendMessage(hwndCalc, MSCODE.BM_CLICK, 0, 0);

                //3個(gè)文本框

                //WinAPIuser32.SendTextMessage(list[1], MSCODE.WM_SETTEXT, 0, "mextb1860第三個(gè)文本框");//發(fā)送文本

            }

        }

 

 

代碼粗略~!有問題留言

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    亚洲av熟女国产一区二区三区站 | 午夜福利视频六七十路熟女| 国产精品久久三级精品| 大香蕉伊人一区二区三区| 隔壁的日本人妻中文字幕版| 蜜臀人妻一区二区三区| 国产日产欧美精品视频| 欧美日不卡无在线一区| 欧美一区二区三区十区| 欧美午夜视频免费观看| 日韩精品综合免费视频| 国产不卡视频一区在线| 深夜福利亚洲高清性感| 国产亚洲午夜高清国产拍精品| 亚洲在线观看福利视频| 国产精品自拍杆香蕉视频| 99热在线精品视频观看| 国产真人无遮挡免费视频一区| 国产av精品一区二区| 能在线看的视频你懂的| 区一区二区三中文字幕| 日韩精品中文字幕亚洲| 久久免费精品拍拍一区二区| 久久精品久久精品中文字幕| 免费性欧美重口味黄色| 日韩三极片在线免费播放 | 午夜精品黄片在线播放| 又色又爽又无遮挡的视频 | 狠色婷婷久久一区二区三区| 国产精品亚洲二区三区| 亚洲日本中文字幕视频在线观看| 黄片在线观看一区二区三区| 亚洲欧美日本成人在线| 97人妻人人揉人人躁人人| 亚洲欧美日韩熟女第一页| 国产精欧美一区二区三区久久| 日本免费一区二区三女| 国产国产精品精品在线| 青青免费操手机在线视频| 高清不卡视频在线观看| 欧美日韩少妇精品专区性色|