代码如下  ,请大家帮我看下代码,IntPtr ChildHwnd = FindWindowEx(hwnd, 0, "Edit", null)这个语句是获取窗口的edit区域的句柄,但我每次获取的都是0,大家帮我看看是语法错误还是参数错误
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, string wParam, string lParam);
        public delegate void DataReceivedDelegate(object sender, EventArgs e);
        public const int WM_SETTEXT = 0x0C; 
        //public const EM_REPLACESEL = &HC2;
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(100);
            int bytes = serialPort1.BytesToRead;
            byte[] buffer = new byte[bytes];
            if (bytes == 0)
            { return; }
            serialPort1.Read(buffer, 0, bytes);
            string s100 = ByteToString(buffer, Encoding.GetEncoding("GB2312"));
            IntPtr hwnd = FindWindow("Notepad", null);
            if (hwnd != IntPtr.Zero)
            {
                IntPtr ChildHwnd = FindWindowEx(hwnd, 0, "Edit", null);
                SendMessage(ChildHwnd, WM_SETTEXT, null, s100);
            }
            else
            {
                MessageBox.Show("没有找到记事本窗口");
            }  
            //IntPtr ptrWnd = WindowFromPoint(Cursor.Position);
            //SendMessage(ptrWnd, WM_COPYDATA, (IntPtr)0, s100);
        }

解决方案 »

  1.   

    实际上你不需要用Win32的这些消息,有Win32的API可以用,
    GetWindowText
    SetWindowText.你要先Get,再Set,
      

  2.   

                System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
                byte[] bs1 = converter.GetBytes("你好");
                //---------------------------------------
                //假设bs1是你GetWindowText得到的。前面的代码只是测试用的。
                string textGot = converter.GetString(bs1);
                string textSet = textGot + "吗?";            byte[] bytesToSent = converter.GetBytes(textSet);
                //SetWindowText or SendMessage
      

  3.   

    函数原型:
    HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,
    LPCTSTR lpszClass,LPCTSTR lpszWindow);   
    参数:
    hwndParent:要查找子窗口的父窗口句柄。   
    如果hwndParent为NULL,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。   
    Windows NT5.0 and later:如果hwndParent是HWND_MESSAGE,函数仅查找所有消息窗口。   
    hwndChildAfter :子窗口句柄。查找从在Z序中的下一个子窗口开始。
    子窗口必须为hwndParent窗口的直接子窗口而非后代窗口。
    如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。
    如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。
      

  4.   

    FindWindowEx句柄
    IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
    // 传递数据给记事本
    SendMessage(vHandle, WM_SETTEXT, 0, "Message");
    IntPtr hwnd = FindWindow ( null, "记事本" ); 
      

  5.   

    请问如何利用WM_GETTEXT来获取目标窗口的内容,并转化成字符串
      

  6.   

    我邮箱,[email protected],方便发下
      

  7.   


    //Created by wuyazhe @2010-7-20
    //引用时请保留作者声明using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Text;namespace CSharpConsole02
    {
        class Program
        {
            [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
            public static extern IntPtr FindWindowEx(IntPtr hWnd1, int hWnd2, string lpsz1, string lpsz2);
            [DllImport("user32.dll", EntryPoint = "GetWindowText")]
            public static extern IntPtr GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);
            [DllImport("User32 ")]
            public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
            [DllImport("User32 ")]
            public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam);
            public const int WM_GETTEXT = 0xD;
            public const int WM_SETTEXT = 0xC;        static void Main(string[] args)
            {
                //范例,启动记事本
                Process p1 = Process.Start("Notepad.exe");
                p1.WaitForInputIdle();//等待启动成功
                //查询记事本进程
                Process[] process_array = Process.GetProcesses();
                IntPtr hNotepad = IntPtr.Zero;
                //找到记事本进程
                foreach (Process p in process_array)
                {
                    if (p.MainWindowTitle.IndexOf("记事本") != -1)//找到了
                    {
                        hNotepad = p.MainWindowHandle;
                        break;
                    }
                }
                if (hNotepad != IntPtr.Zero)
                {
                    //获取控件句柄
                    IntPtr hEdit = FindWindowEx(hNotepad, 0, "Edit", "");
                    //写入文本
                    SendMessage(hEdit, WM_SETTEXT, 100, "这里是测试文本");
                    //读取文本内容
                    string w = " ";
                    IntPtr ptr = Marshal.StringToHGlobalAnsi(w);
                    if (SendMessage(hEdit, WM_GETTEXT, 100, ptr)) Console.WriteLine(Marshal.PtrToStringAnsi(ptr));
                }
                Console.ReadKey();
            }
        }
    }
      

  8.   

                IntPtr hwnd = FindWindow("Notepad", null);
                if (hwnd != IntPtr.Zero)
                {
                    //SendMessage(hwnd, WM_SETTEXT, null, "s100");
                    SetWindowText(hwnd, "s100");
                }
                else
                {
                    MessageBox.Show("没有找到记事本窗口");
                }
      

  9.   

                IntPtr hwnd = FindWindow("Notepad", null);
                if (hwnd != IntPtr.Zero)
                {
                    SendMessage(hwnd, WM_SETTEXT, null, "s100");
                    //SetWindowText(hwnd, "s100");
                }
                else
                {
                    MessageBox.Show("没有找到记事本窗口");
                }
      

  10.   

    [DllImport("user32.dll", EntryPoint = "FindWindow")]
      private static extern IntPtr FindWindowEx(IntPtr hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
    应该为[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
      private static extern IntPtr FindWindowEx(IntPtr hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);改了后, 自然就得到 FindWindowEx 正确的值了