COMbox 句柄的一些写法。

来源:互联网 发布:公共网络怎么改成家庭 编辑:程序博客网 时间:2024/05/17 04:35

http://www.16aspx.com/

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;


namespace findwindow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //################################################


        //##  更多源码下载请访问 http://www.xiaoyaolife.com


        //##  运行本例前,请先打开目录下的 使用说明.txt


        //################################################
        #region 获取子窗口相关.


        [DllImport("User32.dll")]
        public extern static IntPtr GetWindow(IntPtr hWnd, int wCmd);


        #endregion;
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        private static extern int SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, out  Rect lpRect);
        [DllImport("user32.dll")]
        static extern void BlockInput(bool Block);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, EntryPoint = "EnableWindow")]
        static extern long EnableWindow(IntPtr hwnd, bool fEnable);


        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        [DllImport("user32.dll", EntryPoint = "GetMenu")]
        public static extern int GetMenu(int hwnd);


        [DllImport("user32.dll", EntryPoint = "GetSubMenu")]
        public static extern int GetSubMenu(int hMenu, int nPos);
        // 键盘点击api
        [DllImport("user32.dll")]
        public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
        [DllImport("user32.dll", EntryPoint = "GetMenuItemID")]
        public static extern int GetMenuItemID(int hMenu, int nPos);
        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
        public static extern void SetForegroundWindow(IntPtr hwnd);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendTxtMessage(
            IntPtr hWnd, // handle to destination window 


            int Msg, // message 


            IntPtr wParam, // first message parameter 


            char[] lParam
            // int  lParam // second message parameter 


        );
        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
        const int BM_CLICK = 0x111;
        private readonly int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标移动
        private readonly int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下
        private readonly int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标左键抬起
        private readonly int MOUSEEVENTF_ABSOLUTE = 0x8000;//鼠标绝对位置
        private readonly int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 
        private readonly int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 
        private readonly int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下 
        private readonly int MOUSEEVENTF_MIDDLEUP = 0x0040;// 模拟鼠标中键抬起 




        public struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        [DllImport("User32.Dll")]
        public static extern int GetDlgCtrlID(IntPtr hWndCtl);
        [DllImport("user32.dll")]
        public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);






        [StructLayout(LayoutKind.Sequential)]
        public struct COMBOBOXINFO
        {
            public Int32 cbSize;
            public Rect rcItem;
            public Rect rcButton;
            public ComboBoxButtonState buttonState;
            public IntPtr hwndCombo;
            public IntPtr hwndEdit;
            public IntPtr hwndList;
        }


        public enum ComboBoxButtonState
        {
            STATE_SYSTEM_NONE = 0,
            STATE_SYSTEM_INVISIBLE = 0x00008000,
            STATE_SYSTEM_PRESSED = 0x00000008
        }
        private void button1_Click(object sender, EventArgs e)
        {


            #region 脚垫
            IntPtr hWnd = FindWindow(null, "无标题 - JDPaint5.21");
            Rect lpRect = new Rect();
            GetWindowRect(hWnd, out  lpRect);
            int p1 = lpRect.Left;
            int p2 = lpRect.Right;
            int p3 = lpRect.Top;
            int p4 = lpRect.Bottom;
            int dx = 31;
            int dy = 35;
            SetCursorPos(lpRect.Left + dx, lpRect.Top + dy);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            keybd_event((byte)Convert.ToInt32(73), 0, 0, 0);
            keybd_event((byte)Convert.ToInt32(73), 0, 0x2, 0);


            keybd_event((byte)Convert.ToInt32(73), 0, 0, 0);
            keybd_event((byte)Convert.ToInt32(73), 0, 0x2, 0);
            System.Threading.Thread.Sleep(0x3e8);
            IntPtr hWnd2 = FindWindow("#32770", "输入");
            IntPtr def_ImportComboBoxEx = FindWindowEx(hWnd2, 0, "ComboBoxEx32", null);
            IntPtr def_ImportComboBox = FindWindowEx(def_ImportComboBoxEx, 0, "ComboBox", null);
            string _forder_name = "E:\\ADQ502-1422-主料-136-倒顺+.plt";
            // 向导入窗口中的Edit添加内容,获取Edit对象 .
            IntPtr def_Import = FindWindowEx(def_ImportComboBox, 0, "Edit", null);
            SendTxtMessage(def_Import, 0xC, IntPtr.Zero, _forder_name.ToCharArray());
            IntPtr def_ComboBox = FindWindowEx(hWnd2, 0, "Static", "文件类型(&T):");
            def_ComboBox = GetWindow(def_ComboBox, 2);
            IntPtr hComboBox = def_ComboBox;
            //ComboBox的句柄
          
            const int CB_SETCURSEL = 0x014e;
            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x101;
            const int VK_RETURN = 0xD;
         
           //这是可以修改成功的,只是没触发事件,但如果加上后面的代码,
            Thread.Sleep(600);
            SendMessage(hComboBox, CB_SETCURSEL, 8, 0);//这是可以修改成功的,只是没触发事件,但如果加上后面的代码,


            ///


            COMBOBOXINFO cbi = new COMBOBOXINFO();


            cbi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(cbi);


            if (GetComboBoxInfo(hComboBox, ref cbi))
            {


               
                //66536 消息宏(sp++消息监视WM_Commd消息)
                SendMessage(hComboBox, 0x0111, 66536, (int)cbi.hwndList);


            }
            //
            SendMessage(hComboBox, WM_KEYDOWN, VK_RETURN, 0);
            Thread.Sleep(600);
            SendMessage(hComboBox, WM_KEYUP, VK_RETURN, 0);


            SendTxtMessage(def_ComboBox, 0xC, IntPtr.Zero, _forder_name.ToCharArray());
            Thread.Sleep(600);
            IntPtr def_btn = FindWindowEx(hWnd2, 0, "Button", "打开(&O)");
           // SendMessage(def_btn, 0x00F5, 0, 0); 
            #endregion
           


           
        }


        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
        public void SelectItem(int index)
        {


          


        }
      
    }
}
0 0