您的位置:首页 > 其它

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

2014-04-22 17:45 281 查看
窗口程序接收系统消息,会有相应的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: