111,125
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace test
{
class sendMessage
{
#region Dll Import
[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);
// SendMessage
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wmsg, int wparam, StringBuilder lparam);
[DllImport("User32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);
/* GetWindowText
[DllImport("User32.dll")]
static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(int hwnd, StringBuilder lpString, int cch);
*/
#endregion
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
const int WM_GETTEXTLENGTH = 0x000E;
//得到与一个窗口有关的文本的长度(不包含空字符)
const int buffer_size = 1024;
#region SearchWindow
private int SearchWindow()
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//下面的这些参数都可以用Spy++查到
string lpszParentClass = "#32770"; //"#32770" 整个窗口的类名
string lpszParentWindow = "Bake Ice LineageII ";//窗口标题
string lpszClass = "Static"; //需要查找的子窗口的类名,也就是输入框
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
IntPtr EdithWnd2 = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到User Name这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, null);
if (!EdithWnd.Equals(IntPtr.Zero))
{
//调用SendMessage方法设置其内容
//!!!!这里设置后,实际该文本内容显示却没有改变,但是用 <窗口类名查看器>查看却是设置成功了,
string txt =DateTime.Now.ToString();
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)255, txt);
//MessageBox.Show(txt);
/******************奇怪的地方************************
* 这里获取文本的内容为什么不是显示出来的那个呢?
* 但是假如获取其它文本内容却又正常....
* 如果想获取显示出来的那个要怎么做呢?请高人指点,谢谢
****************************************************/
StringBuilder buffer = new StringBuilder(buffer_size);
SendMessage(EdithWnd, WM_GETTEXT, buffer_size, buffer);
MessageBox.Show(buffer.ToString());
retval++;
}
}
return retval;
}
#endregion
#region Load
private Thread thread;
/// <summary>
///搜索窗体
///</summary>
public void Run()
{
//运行
thread = new Thread(new ThreadStart(Watch));
thread.IsBackground = true;
thread.Start();
}
private void Watch()
{
//循环查找这个窗口,直到成功为止
while (true)
{
int i = this.SearchWindow();
if (i == 1)
//MessageBox.Show("找到该窗体,要退出线程了");
break;
}
}
#endregion
}
}
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wmsg, int wparam, StringBuilder lparam);
private void button3_Click(object sender, EventArgs e)
{
int WM_GETTEXT = 0x000D;
IntPtr hwnd = this.label1.Handle;
const int buffer_size = 1024;
StringBuilder buffer = new StringBuilder(buffer_size);
SendMessage(hwnd, WM_GETTEXT, buffer_size, buffer);
MessageBox.Show(buffer.ToString());
}