using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace wangwangmsg
{
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
}
const int WM_Lbutton = 0x0; //定义了鼠标的左键点击消息
public const int USER = 0x000;// 是windows系统定义的用户消息
const int WM_MOUSEMOVE = 0x00;
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int 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 = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint 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 = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wparam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, bool wparam, int lParam);//首先在program中声明sendmessage方法
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
// [DllImport("user32.dll")]
// public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
//[DllImport("user32.dll")]
// public static extern int EnumWindows(CallBack lpfn, int lParam);
[DllImport("user32.dll ", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
const int WM_SHOWDROPDOWN = 0x014D;//在窗体中声明消息常量
const int WM_SETCURSOR = 0x14F;
const int CB_FINDSTRING = 0x14C;
public bool PreFilterMessage(ref Message m)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if (m.Msg == WM_MOUSEMOVE) //||m.Msg == WM_LBUTTONDOWN
{
//MessageBox.Show("Ignoring Escape...");
return true;
}
return false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//调用:
StringBuilder sb = new StringBuilder();
sb.Append("温温热");
int WINDOW_HANDLER = FindWindow(null, textBox1.Text);//1.txt - Notepat
this.Text = WINDOW_HANDLER.ToString();
if (WINDOW_HANDLER!=0)
{
SendMessage((IntPtr)WINDOW_HANDLER, WM_SETTEXT, 0, sb.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
int hwind = FindWindow(null, textBox1.Text);
int hwind = FindWindow(null,"1.txt - Notepat" );
// hwind = 1247530;
if (hwind != 0)
{
StringBuilder sb = new StringBuilder(4321);
SendMessage((IntPtr)hwind, WM_GETTEXT, sb.Capacity, sb); //取得文本
MessageBox.Show(sb.ToString());
}
}
}
}