日期:2014-05-17 浏览次数:20817 次
A窗体:
const int WM_COPYDATA = 0x004A;
private struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
//发送内容Content
string FormName = "B";
IntPtr handle = FindWindow(null, windowsName);
if (handle != IntPtr.Zero)
{
byte[] s = Encoding.Default.GetBytes(Content);
int len = s.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = Content;
cds.cbData = len + 1;
SendMessage(handle, WM_COPYDATA, 0, ref cds);
}
B窗体:
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
string receiveMsg = mystr.lpData;
MessageBox.Show(receiveMsg);