compactframework:.NET Compact Framework下的进程间通信的一 --Windows Message来源: 发布时间:星期四, 2009年3月19日 浏览:0次 评论:0
在Wince和Windows Moblie 下进程间通信可以由以下几种技术实现 1. Windows Message 2. Po-to-Po Message Queues 3. MSMQ 下面使用讲述.NET Compact Framework下使用Windows Message进行进程间通信 引用库在CF.net下进行Windows Message开发需要引用Microsoft.WindowsCE.Forms该DLL般存放于C:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\Microsoft.WindowsCE.Forms.dll发送消息usingMicrosoft.WindowsCE.Forms; publicpartialMsgForm:Form { [DllImport("coredll.dll",EntryPo="RegisterWindowMessage",SetLastError=true)] privateexternuRegisterWindowMessage(lpString); privateumsgUid=RegisterWindowMessage("MESSAGE_UID"); privatevoidSendMessage(objectsender) { Messagemsg=Message.Create((IntPtr)MSG_BROADCAST,()msgUid,IntPtr.Zero,IntPtr.Zero); MessageWindow.SendMessage(refmsg); } } 首先需要P/Invoke RegisterWindowMessage 每个发送message都有唯UID这样接收方才能根据UID进行监听和接收该Message 发送的前先create个Message对象参数为接收对象如果为进程间通信可以使用广播形式(MSG_BROADCAST)第 2个参数为messageUID接收方利用这表示辨别message第 3和第 4分别为WParam和LParam这是标准windows message传递参数 接收消息usingMicrosoft.WindowsCE.Forms; publicMsgWindow:MessageWindow { [DllImport("coredll.dll",EntryPo="RegisterWindowMessage",SetLastError =true)] privateexternuRegisterWindowMessage(lpString); privateumsgUid=RegisterWindowMessage("MESSAGE_UID"); protectedoverridevoidWndProc(refMessagemsg) { (msg.MsgmsgUid) { //handlethemessage. } } } 接收消息需要定义个继承类继承于MessageWindow同时他同样需要P/Invoke RegisterWindowMessage 定义接收message唯UID 重写WndProc然后通过msg.Msg 来辨别关心消息 使用Form处理Message如果接收方接收到message需要更新到form话就定义个formreference这样可以利用form来处理消息其实不定使用Form来处理message使用Form就能比较方便利用ui来反映message接收和处理情况publicpartialMsgForm:Form { privateMsgWindowMsgWin; publicMsgForm { //passtheformreferencetomessagewindow this.MsgWin=MsgWindow(this); } } publicMsgWindow:MessageWindow { privateMsgFormmsgForm; publicMsgWindow(MsgFormmsgForm) { this.msgForm=msgForm; } protectedoverridevoidWndProc( refMessagemsg) { (msg.MsgmsgUid) { //callformtohandlethemessage. msgForm.HandleMsg; } } } MsgWindow 保存MsgForm 引用这样当MsgWindow 接收到消息就可以使用form来处理 0
相关文章
读者评论发表评论 |