c#中使用sendMessage发送文字

qq759354 2009-03-30 01:03:46
在c#中使用sendMessage发送

[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(
int hwnd,
int wMsg,
int wParam,
int lParam
);

public const int WM_KEYDOWN = 0x100;

public const int WM_KEYUP = 0x101;

public const int WM_CHAR = 0x102;

public const int VK_RETURN = 0xD; 这个是否应该这样定义??

还有下面调用的时候,首先发送一个回车,激活聊天窗口,然后写入想发送的内容,最后回车发出,我按照以下的方式,结果是不成功的。应该怎么改一下。
SendMessage(hWnd, WM_KEYDOWN,0,VK_RETURN);
SendMessage(hWnd, WM_KEYUP,0,VK_RETURN);
SendMessage(hWnd, WM_CHAR, messageText, 0);
SendMessage(hWnd, WM_KEYDOWN, 0,VK_RETURN);
SendMessage(hWnd, WM_KEYUP, 0,VK_RETURN);

算了。我这个一定问题一堆,谁有比较完整的源码,发上来看下 ,给分。给分。
...全文
1701 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq759354 2009-03-30
  • 打赏
  • 举报
回复
嗨,从头说吧。我想在一个游戏里面做到自动喊话功能,这个游戏需要首先发送回车,激活说话窗口,然后写字,最后回车发送,我想使用sendMessage实现后台喊话功能,就这样了。但是我没办法把回车发送过去。所以来这里问下,看哪位朋友可以帮忙。
beargo 2009-03-30
  • 打赏
  • 举报
回复
发送回车??传说中的"\\r\\n"吗??
qq759354 2009-03-30
  • 打赏
  • 举报
回复
首先发送一个回车,然后发送一段字符串,最后再次发送一个回车,

谁给个发送回车的例子,我能发出字符串,但是发送不出回车。
Jessezu 2009-03-30
  • 打赏
  • 举报
回复
也可以使用socket实现
Jessezu 2009-03-30
  • 打赏
  • 举报
回复
客户端
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 FindWindow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hwnd_win;
IntPtr hwnd_button;

hwnd_win = FindWindow(null, "frmPassive");//查找标题为frmPassive的窗体句柄
hwnd_button = FindWindowEx(hwnd_win, new IntPtr(0), null, "button1");//查找句柄为hwnd_win的窗体默认标题为button1的控件句柄

const int BM_CLICK = 0x00F5;//单击
Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));//创建消息
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);//发送消息
}

private void button2_Click(object sender, System.EventArgs e)
{
const int WM_CHAR = 0x0102;//按键事件

IntPtr hwnd_win;
IntPtr hwnd_textbox;

hwnd_win = FindWindow(null, "frmPassive");//查找窗体
hwnd_textbox = FindWindowEx(hwnd_win, new IntPtr(0), null, "textBox1");//查找控件

string strtext = "按键事件|";
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(strtext));
Message msg;
foreach (char c in chars)
{
msg = Message.Create(hwnd_textbox, WM_CHAR, new IntPtr(c), new IntPtr(0));
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
}
}

private void button3_Click(object sender, EventArgs e)
{
IntPtr hwnd_win;
int WM_USER = 0x0400 + 111;
hwnd_win = FindWindow(null, "frmPassive");//查找标题为frmPassive的窗体句柄
string strtext = "测试|";
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(strtext));
Message msg;
foreach (char c in chars)
{
msg = Message.Create(hwnd_win, WM_USER, new IntPtr(c), new IntPtr(0));
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
}
}

private void button4_Click(object sender, EventArgs e)
{
const int WM_SETTEXT = 0x000C;//设置文本事件
IntPtr hwnd_win;
IntPtr hwnd_textbox;

hwnd_win = FindWindow(null, "frmPassive");//查找窗体
hwnd_textbox = FindWindowEx(hwnd_win, new IntPtr(0), null, "textBox1");//查找控件

SendMessage(hwnd_textbox, WM_SETTEXT, new IntPtr(0), "wfy");

}

}
}

服务器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace FindWindow1
{
public partial class Form1 : Form
{
//[StructLayout(LayoutKind.Sequential)]
//public struct Message
//{
// public IntPtr hWnd;
// public uint msg;
// public IntPtr wParam;
// public IntPtr lParam;
// public uint time;
// public System.Drawing.Point p;
//}
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("button1_Click");

}


private void getMsg()
{
Message msg = new Message();
uint WM_USER = 0x0400 + 111;
char[] bs = new char[100];
int i = 0;
UnicodeEncoding encode = new UnicodeEncoding();
//监听消息WM_USER
while (true)
{
if (PeekMessage(out msg, IntPtr.Zero, WM_USER, WM_USER, (uint)1))
{
char achar =(char)msg.WParam;

bs[i] = achar;

if (achar == '|')
{
MessageBox.Show(encode.GetString(encode.GetBytes(bs)));
break;
}
else
{
i++;
}

}

}

}


private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
getMsg();
}

}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧