1 / 9

PostMessage 之元件

PostMessage 之元件. 劉崇汎 崑山科技大學 電腦與通訊系 http://www.ksu.edu.tw. 主程式畫面. 建立 WinMessageManager. 在專案中新增一個類別. 2. 1. 4. 3. 程式碼 (1/4). using ………. using System.Runtime.InteropServices; namespace PostMessageTest { class WinMessageManager : IMessageFilter {

gracier
Download Presentation

PostMessage 之元件

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PostMessage之元件 劉崇汎 崑山科技大學 電腦與通訊系 http://www.ksu.edu.tw

  2. 主程式畫面

  3. 建立WinMessageManager • 在專案中新增一個類別 2 1 4 3

  4. 程式碼(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;} }

  5. 程式碼(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); } }

  6. 程式碼(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); } } }

  7. 程式碼(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; }

  8. 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); }

  9. 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); }

More Related