在Windows Service中接收Windows消息(WM_messages)

来源:互联网 发布:mysql like带_字符 编辑:程序博客网 时间:2024/05/01 21:27

窗口程序接收系统消息,会有相应的WndProc函数。在Service中则没有这样的函数,所以必须想办法进行消息的获取。

首先,我们建立一个类,类继承与System.Windows.Forms.Form。

#pragma once#using <System.dll>#using <System.Drawing.dll>#using <System.Windows.Forms.dll>#include <Windows.h>#include <fstream>using namespace System;using namespace System::Drawing;using namespace System::Windows::Forms;using namespace System::Security::Permissions;public ref class MessageForm : public System::Windows::Forms::Form{public:MessageForm(void){};~MessageForm(void){};protected: [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]      virtual void WndProc( Message% m ) override      {         // Listen for operating system messages.          switch ( m.Msg )         {   case WM_Message://Your code. break;         }         Form::WndProc( m ); stream.close();      }};
之后,我们在服务开启之后,建立这个对象。

Application::Run( gcnew MessageForm );
这样就能在服务中,进行Windows消息的处理了。

参考:http://bytes.com/topic/c-sharp/answers/610416-listening-windows-messages-windows-service

参考:http://msdn2.microsoft.com/en-us/library/system.windows.forms.message.aspx

0 0
原创粉丝点击