Listening to OS messages in C#

大憨熊 提交于 2020-12-12 18:12:33

问题


Is there any methods in C# similar to WndProc method to listen to the OS messages.I cant use WndProc because,my class is neither Form nor Inherited from Control(Its DLL)

protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        switch (m.Msg)
        {
            // listen os messages


            // Ueye Message
            case uEye.IS_UEYE_MESSAGE:
                //fetch frame
                break;
        }
        base.WndProc(ref m);
    }

回答1:


WMI will do if you want to listen for specific messages. I once had a project (see comment on question) that listened for removeable USB drives and WMI worked just fine.

You can use interop as well but I find it messy but YMMV.




回答2:


The standard approach to receiving windows messages in the absence of a visible window is to create a non-visible window to receive messages.




回答3:


You should use Windows.Interop

to have access to Win API




回答4:


Check this: http://social.msdn.microsoft.com/Forums/en-IE/winforms/thread/b44f06fb-fc4a-4fac-87cd-48b2953ea5fa

It seems to be possible to override WndProc, but I haven't tried it myself!




回答5:


If you have a Form (visible or otherwise), look at Form.WndProc.

If not, you could try using Application.AddMessageFilter to add a message filter to monitor Windows messages.



来源:https://stackoverflow.com/questions/5591371/listening-to-os-messages-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!