90 likes | 105 Views
PostMessage 之元件. 劉崇汎 崑山科技大學 電腦與通訊系 http://www.ksu.edu.tw. 主程式畫面. 建立 WinMessageManager. 在專案中新增一個類別. 2. 1. 4. 3. 程式碼 (1/4). using ………. using System.Runtime.InteropServices; namespace PostMessageTest { class WinMessageManager : IMessageFilter {
E N D
PostMessage之元件 劉崇汎 崑山科技大學 電腦與通訊系 http://www.ksu.edu.tw
建立WinMessageManager • 在專案中新增一個類別 2 1 4 3
程式碼(1/4) using ………. using System.Runtime.InteropServices; namespace PostMessageTest { class WinMessageManager : IMessageFilter { [DllImport("user32.dll ", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll ", CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern uint RegisterWindowMessage(string lpString); IntPtr HWND_BROADCAST = new IntPtr(0xffff); public uint MSG_ID; bool msg_OK = false; string _messageName = "None"; public string MessageName { set { if ((value == "") || (value == null)) msg_OK = false; else {_messageName = value; MSG_ID = RegisterWindowMessage(_messageName); msg_OK = true; } } get { return _messageName;} }
程式碼(2/4) static Dictionary<int, DataTransfer> datas; static object objLock; static int dataKey = 0; DataTransfer value; public bool PreFilterMessage(ref Message msg) { if (msg.Msg == MSG_ID) { if ((int)msg.LParam == 20) { MessageBox.Show("I get Integer = " + msg.WParam.ToString()); } if ((int)msg.LParam == 21) { DataTransfer value = GetData(msg.WParam.ToInt32()); } MessageBox.Show("I get string = " + value.transferMessage); } } return false; } public void Send(int Value) { if (msg_OK) { //UIntPtr uPtr = new UIntPtr(Value); SendMessage(HWND_BROADCAST, MSG_ID, Value, 10); } }
程式碼(3/4) public void Send(string str_msg) { if (msg_OK) { value = new DataTransfer(); value.transferMessage = str_msg; value.obj = "Fann's message"; lock (objLock) { dataKey++; datas.Add(dataKey, value); SendMessage(HWND_BROADCAST, MSG_ID, dataKey, 11); } } } public void Post(string str_msg) { if (msg_OK) { value = new DataTransfer(); value.transferMessage = str_msg; value.obj = "Fann's message"; lock (objLock) { dataKey++; datas.Add(dataKey, value); PostMessage(HWND_BROADCAST, MSG_ID, dataKey, 21); } } }
程式碼(4/4) public void Post(int Value) { if (msg_OK) { PostMessage(HWND_BROADCAST, MSG_ID, Value, 20); } } public static DataTransfer GetData(int pickDataKey) { lock (objLock) { DataTransfer value = datas[pickDataKey]; datas.Remove(pickDataKey); return value; } } public WinMessageManager() { datas = new Dictionary<int, DataTransfer>(); objLock = new object(); Application.AddMessageFilter(this); } } //***************************************************** public class DataTransfer { public string transferMessage; public object obj; }
Form的主程式(1/2) public partial class Form1 : Form { WinMessageManager MSG; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MSG = new WinMessageManager(); MSG.MessageName = "Show Message"; } protected override void WndProc(ref Message m) { if (MSG != null) if (m.Msg == MSG.MSG_ID) { if ((int)m.LParam == 10) { this.Text = “I got Integer = ” +m.WParam.ToString(); } if ((int)m.LParam == 11) { DataTransfer value = WinMessageManager.GetData (m.WParam.ToInt32()); this.Text = “I got string =” +value.transferMessage; } } base.WndProc(ref m); }
Form的主程式(2/2) private void button1_Click(object sender, EventArgs e) { MSG.Send(System.Convert.ToInt32(textBox2.Text)); } private void button2_Click(object sender, EventArgs e) { MSG.Post(System.Convert.ToInt32(textBox2.Text)); } private void button3_Click(object sender, EventArgs e) { MSG.Post(textBox1.Text); } private void button4_Click(object sender, EventArgs e) { MSG.Send(textBox1.Text); }