using using System Runtime Interop Services 14 namespace

  • Slides: 9
Download presentation

using ………. using System. Runtime. Interop. Services; 程式碼(1/4) namespace Post. Message. Test { class

using ………. using System. Runtime. Interop. Services; 程式碼(1/4) namespace Post. Message. Test { class Win. Message. Manager : IMessage. Filter { [Dll. Import("user 32. dll ", Char. Set = Char. Set. Auto)] static extern Int. Ptr Send. Message(Int. Ptr h. Wnd, uint Msg, int w. Param, int l. Param); [Dll. Import("user 32. dll ", Char. Set = Char. Set. Auto)] private static extern bool Post. Message(Int. Ptr hwnd, uint w. Msg, int w. Param, int l. Param); [Dll. Import("user 32. dll", Set. Last. Error = true, Char. Set = Char. Set. Auto)] static extern uint Register. Window. Message(string lp. String); Int. Ptr HWND_BROADCAST = new Int. Ptr(0 xffff); public uint MSG_ID; bool msg_OK = false; string _message. Name = "None"; public string Message. Name { set { if ((value == "") || (value == null)) msg_OK = false; else {_message. Name = value; MSG_ID = Register. Window. Message(_message. Name); msg_OK = true; } } get { return _message. Name; } } 堅毅誠樸

程式碼(2/4) static Dictionary<int, Data. Transfer> datas; static object obj. Lock; static int data. Key

程式碼(2/4) static Dictionary<int, Data. Transfer> datas; static object obj. Lock; static int data. Key = 0; Data. Transfer value; public bool Pre. Filter. Message(ref Message msg) { if (msg. Msg == MSG_ID) { if ((int)msg. LParam == 20) { Message. Box. Show("I get Integer = " + msg. WParam. To. String()); } if ((int)msg. LParam == 21) { Data. Transfer value = Get. Data(msg. WParam. To. Int 32()); } Message. Box. Show("I get string = " + value. transfer. Message); } } return false; } public void Send(int Value) { if (msg_OK) { //UInt. Ptr u. Ptr = new UInt. Ptr(Value); Send. Message(HWND_BROADCAST, MSG_ID, Value, 10); } } 堅毅誠樸

程式碼(3/4) public void Send(string str_msg) public void Post(string str_msg) { { if (msg_OK) {

程式碼(3/4) public void Send(string str_msg) public void Post(string str_msg) { { if (msg_OK) { { value = new Data. Transfer(); value. transfer. Message = str_msg; value. obj = "Fann's message"; lock (obj. Lock) { { data. Key++; datas. Add(data. Key, value); Send. Message(HWND_BROADCAST, Post. Message(HWND_BROADCAST, MSG_ID, data. Key, 11); MSG_ID, data. Key, 21); } } } 堅毅誠樸

程式碼(4/4) public void Post(int Value) { if (msg_OK) { Post. Message(HWND_BROADCAST, MSG_ID, Value, 20);

程式碼(4/4) public void Post(int Value) { if (msg_OK) { Post. Message(HWND_BROADCAST, MSG_ID, Value, 20); } } public static Data. Transfer Get. Data(int pick. Data. Key) { lock (obj. Lock) { Data. Transfer value = datas[pick. Data. Key]; datas. Remove(pick. Data. Key); return value; } } public Win. Message. Manager() { datas = new Dictionary<int, Data. Transfer>(); obj. Lock = new object(); Application. Add. Message. Filter(this); } } //*************************** public class Data. Transfer { public string transfer. Message; public object obj; } 堅毅誠樸

Form的主程式(1/2) protected override void Wnd. Proc(ref Message m) public partial class Form 1 :

Form的主程式(1/2) protected override void Wnd. Proc(ref Message m) public partial class Form 1 : Form { { if (MSG != null) Win. Message. Manager MSG; if (m. Msg == MSG_ID) public Form 1() { { if ((int)m. LParam == 10) Initialize. Component(); { } this. Text = “I got Integer = ” + m. WParam. To. String(); private void Form 1_Load(object sender, Event. Args e) } { if ((int)m. LParam == 11) MSG = new Win. Message. Manager(); { MSG. Message. Name = "Show Message"; Data. Transfer value = Win. Message. Manager. Get. Data } (m. WParam. To. Int 32()); this. Text = “I got string =” + value. transfer. Message; } } base. Wnd. Proc(ref m); } 堅毅誠樸

Form的主程式(2/2) private void button 1_Click(object sender, Event. Args e) { MSG. Send(System. Convert. To.

Form的主程式(2/2) private void button 1_Click(object sender, Event. Args e) { MSG. Send(System. Convert. To. Int 32(text. Box 2. Text)); } private void button 2_Click(object sender, Event. Args e) { MSG. Post(System. Convert. To. Int 32(text. Box 2. Text)); } private void button 3_Click(object sender, Event. Args e) { MSG. Post(text. Box 1. Text); } private void button 4_Click(object sender, Event. Args e) { MSG. Send(text. Box 1. Text); } 堅毅誠樸