窗口消息传递的问题,API高手进。
#region 窗口消息处理相关引用 const Int32 WM_COPYDATA = 0x004A;
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
/// <summary>
/// 固定为 (IntPtr) 100?
/// </summary>
public IntPtr dwData;
/// <summary>
/// 消息长度+1,通过System.Text.Encoding.Default.GetByteCount函数取得字符串实际长度
/// </summary>
public int cbData;
/// <summary>
/// 字节数组表示的消息内容
/// </summary>
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string lpData;
}
/// <summary>
///
/// </summary>
/// <param name="hWnd">handle to destination window </param>
/// <param name="Msg">message identifier </param>
/// <param name="wParam">first message parameter </param>
/// <param name="lParam">second message parameter </param>
/// <returns> </returns>
[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
Int32 hWnd,
Int32 Msg,
Int32 wParam,
ref COPYDATASTRUCT lParam
);
/// <summary>
/// 该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回。
/// </summary>
/// <param name="hWnd">目标窗口句柄 </param>
/// <param name="Msg">message identifier </param>
/// <param name="wParam">第一个信息参数 </param>
/// <param name="lParam">第一个信息参数 </param>
/// <returns> </returns>
[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(
Int32 hWnd,
Int32 Msg,
Int32 wParam,
ref COPYDATASTRUCT lParam
);
//[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "GetWindowText")]
//private static extern int GetWindowText(
// Int32 hWnd, // {a handle to a window}
// [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
// string lpString, // {a pointer to a buffer to receive the string}
// Int32 nMaxCount // {the maximum number of characters to copy}
// ); // {returns the length of the copied string}
//[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "FindWindow")]
//private static extern int FindWindow(string lpClassName, string
//lpWindowName);
#endregion
//窗口消息处理
protected override void DefWndProc(ref Message m)//没接收到相应的窗口信息
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT cdsT = new COPYDATASTRUCT();
Type mytype = cdsT.GetType();
cdsT = (COPYDATASTRUCT)m.GetLParam(mytype);
//调试开始
tbContent.AppendText(cdsT.lpData); //在外部已经定义
MessageBox.Show("调试开始");
//调试结束
//cdsT.lpData
if (cdsT.lpData == ConstantVars.WindowMessageConst.sOptionChanged) LoadOption();
if (cdsT.lpData.StartsWith(ConstantVars.WindowMessageConst.sOpenFile))
{
Int32 I = cdsT.lpData.Length - ConstantVars.WindowMessageConst.sOpenFile.Length;
OpenFile(sflStoryFileList.DecreaseStoryFilesFrequecyBut(new StringBuilder(cdsT.lpData, ConstantVars.WindowMessageConst.sOpenFile.Length, I, I).ToString())); //在外部已经定义
}
else
{
if (cdsT.lpData == ConstantVars.WindowMessageConst.sOptionChanged)
{
LoadOption(); //在外部已经定义
}
}
MessageBox.Show(cdsT.lpData);
break;
default:
base.DefWndProc(ref m);
break;
}
}
/// <summary>
/// //找出以前的实例并发送信息
/// </summary>
/// <param name="sMessage"> </param>
private void SendMessageToBefore(string sMessage)//这个函数有调用,但没有结果
{
COPYDATASTRUCT csT = new COPYDATASTRUCT();
csT.dwData = (IntPtr)100;
csT.lpData = sMessage;
csT.cbData = System.Text.Encoding.Default.GetByteCount(sMessage) + 1;
System.Diagnostics.Process prcCurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
string sFileName = prcCurrentProcess.MainModule.FileName;
Int32 iID = prcCurrentProcess.Id;
System.Diagnostics.Process[] prcs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process prcT in prcs)
{
try
{
Int32 I;
if (prcT.MainModule.FileName == sFileName)
if (prcT.Id != iID)
I = PostMessage(prcT.MainWindowHandle.ToInt32(), WM_COPYDATA, 0, ref csT);
}
catch
{
}
}
}