没有学过英文么?get是获取得到,set是设置。

解决方案 »

  1.   


     SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, string.Format("当前时间是:{0}", DateTime.Now)); //赋值没问题,表示句柄正确
    这段代码,是我为了确保句柄没错,给这个文本赋值,
      

  2.   

    上面代码有点乱,我贴全部的代码,各位可以试试看。 public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        const int WM_GETTEXT = 0x000D;  //13
            const int WM_SETTEXT = 0x000C; //12
            const int WM_CLICK = 0x00F5; //245        //方法声明
            [DllImport("user32.dll")]
            static extern int SendMessage(IntPtr Handle, int WParam, int LParam);        [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);        [DllImport("user32.dll ", EntryPoint = "GetDlgItem")]
            public static extern IntPtr GetDlgItem(IntPtr hParent, int nIDParentItem);        [DllImport("user32.dll", EntryPoint = "GetWindowText")]
            public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern int GetWindowTextLength(IntPtr hWnd);        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    SearchWindow();
                }
                catch { }
            }        private void SearchWindow()
            {
                //下面的这些参数都可以用Spy++查到 
                string lpszParentWindow = "统一动态库工具 v1.1"; //窗口标题             IntPtr ParenthWnd = new IntPtr(0);
                IntPtr EdithWnd = new IntPtr(0);            //查到窗体,得到整个窗体 
                ParenthWnd = FindWindow(null, lpszParentWindow);            //判断这个窗体是否有效 
                if (!ParenthWnd.Equals(IntPtr.Zero))
                {
                    //控件id
                    int controlId = 0x000003F4;
                    EdithWnd = GetTxtSourceIntPtr(ParenthWnd, controlId);                if (!EdithWnd.Equals(IntPtr.Zero))
                    {
                        SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, string.Format("当前时间是:{0}", DateTime.Now)); //赋值没问题,表示句柄正确
                        int length = GetWindowTextLength(EdithWnd);
                        StringBuilder stringBuilder = new StringBuilder(length + 1);
                        GetWindowText(EdithWnd, stringBuilder, stringBuilder.Capacity);
                        MessageBox.Show(string.Format("取到的值是:{0}", stringBuilder.ToString()));//取值一直是空字符串
                    }            }
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="ParenthWnd">父窗口</param>
            /// <param name="controlId">子窗口控件id</param>
            /// <returns></returns>
            private IntPtr GetTxtSourceIntPtr(IntPtr ParenthWnd, int controlId)
            {
                IntPtr EdithWnd = new IntPtr(0);
                if (ParenthWnd != null && !ParenthWnd.Equals(IntPtr.Zero))
                {
                    IntPtr Tab1 = new IntPtr(0);
                    Tab1 = FindWindowEx(ParenthWnd, Tab1, "SysTabControl32", "Tab1");
                    if (!Tab1.Equals(IntPtr.Zero))
                    {
                        IntPtr dialog = new IntPtr(0);
                        dialog = FindWindowEx(Tab1, dialog, null, null);
                        if (!dialog.Equals(IntPtr.Zero))
                        {
                            //获取子窗口句柄
                            EdithWnd = GetDlgItem(dialog, controlId);
                        }
                    }
                }
                return EdithWnd;
            }
        }
      

  3.   

    GetWindowText对于文本框返回空,因为它代表窗口标题。文本框不是窗口没有标题。
      

  4.   


    额,那我可能被spy+误导了,那请问如果要取文本框的值,需要用哪个函数?
      

  5.   


    额,那我可能被spy+误导了,那请问如果要取文本框的值,需要用哪个函数?
    不是告诉你3次了
    WM_GETTEXT
      

  6.   


    额,那我可能被spy+误导了,那请问如果要取文本框的值,需要用哪个函数?
    不是告诉你3次了
    WM_GETTEXT
    SendMessage(EdithWnd, WM_GETTEXT, (IntPtr)0, str);这样也获取不到值,能否给一点示范性的代码,谢谢!