日期:2014-05-18 浏览次数:21209 次
public static class MyMessage
{
public const int USER_MESSAGE = 0x0400;
public const int WM_OPEN_ATTRIBUTE = USER_MESSAGE + 1;
public const int WM_CLOSE_ATTRIBUTE = USER_MESSAGE + 2;
}
public static class MessageController
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
public static void SendMsgToMainForm(int MSG, IntPtr wParam, IntPtr lParam)
{
int WINDOW_HANDLER = FindWindow(null, @"mainform");
if (WINDOW_HANDLER == 0)
{
throw new Exception("Could not find Main window!");
}
SendMessage(WINDOW_HANDLER, MSG, wParam, lParam);
}
}
MessageController.SendMsgToMainForm(MyMessage.WM_OPEN_ATTRIBUTE, (IntPtr)0, (IntPtr)0);
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case MyMessage.WM_OPEN_ATTRIBUTE: //处理消息
//...
break;
case MyMessage.WM_CLOSE_ATTRIBUTE:
//...
break;
default:
base.DefWndProc(ref m);//调用基类函数处理非自定义消息。
break;
}
}