代码如下 ,请大家帮我看下代码,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);
}
[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);
}
解决方案 »
- excle数据导入到sql server数据库中,该如何实现
- C#论坛里面怎么实现用户点击标题和回复记录
- 如何获取日期控件(MonthCalendar或DateTimePicker、或C1控件等)上的周次?
- 求Listbox字体问题
- 除了自己form 桌面背景 都变成黑白的50%透明的怎么做?
- 请问怎么改变程序的外框?
- 关于combobox的问题,解决不了
- Array.ForEach方法不能改变元素的值吗?
- DataGrid中如何加入DropDownList?
- 请问如何将多次剪贴板中的数据(部分图象)加到一起形成一个完整的图象并显示出来呢?
- 急~如何从datagridview中把被选中CheckBox的多行存入到LIST中?
- 关于ado.net的事务迷惑,请教大侠该写在哪里?
GetWindowText
SetWindowText.你要先Get,再Set,
byte[] bs1 = converter.GetBytes("你好");
//---------------------------------------
//假设bs1是你GetWindowText得到的。前面的代码只是测试用的。
string textGot = converter.GetString(bs1);
string textSet = textGot + "吗?"; byte[] bytesToSent = converter.GetBytes(textSet);
//SetWindowText or SendMessage
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,则函数查找所有的顶层窗口及消息窗口。
IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
// 传递数据给记事本
SendMessage(vHandle, WM_SETTEXT, 0, "Message");
IntPtr hwnd = FindWindow ( null, "记事本" );
//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();
}
}
}
if (hwnd != IntPtr.Zero)
{
//SendMessage(hwnd, WM_SETTEXT, null, "s100");
SetWindowText(hwnd, "s100");
}
else
{
MessageBox.Show("没有找到记事本窗口");
}
if (hwnd != IntPtr.Zero)
{
SendMessage(hwnd, WM_SETTEXT, null, "s100");
//SetWindowText(hwnd, "s100");
}
else
{
MessageBox.Show("没有找到记事本窗口");
}
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 正确的值了