WinCE中使用C#时使用WndProc方法的处理

  在使用C#为WinCE编程时,有时需要override一个方法——WndProc。比如用它来监视是否有U盘插入或拔除。但在WinCE下,并不能直接override它。它需要引用两个命名空间:

using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

 换言之,System.Windows.Forms里不提供消息处理类。查阅MSDN上的示例文章(http://msdn.microsoft.com/zh-cn/library/5143xwd8%28v=VS.80%29.aspx),
微软的实现方法是:自定义消息处理类,并继承Microsoft.WindowsCE.Forms.MessageWindow类, 再在类中override WndProc方法。同时把目标Form作为自定义类的构
造函数参数传入,具体代码如下:
1. CustomMessageWindow.cs类

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;

namespace UDiskDemo
{
    class CustomMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
    {
    
//定义目标窗体实例
        private MessageFrom msgForm;       
        /// <summary>
        /// Constructor(传入目标窗体对象)
        /// </summary>
        /// <param name="frmForm">Start Form</param>
        public CustomMessageWindow(MessageFrom frmForm)
        {
            this.msgForm = frmForm;
        }


        /// <summary>
        ///
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message m)
        {           
            try
            {
                switch (m.Msg)
                {
                    case (int)CMD_DBT.WM_DEVICECHANGE:   //设备改变
                        switch (m.WParam.ToInt32())
                        {
                            case (int)CMD_DBT.WM_DEVICECHANGE://
                                break;
                            case (int)CMD_DBT.DBT_DEVICEARRIVAL://U盘插入
                                {
                                    //这里可以插入应用代码                             
                                }
                                break;
                            case (int)CMD_DBT.DBT_CONFIGCHANGECANCELED:
                                break;
                            case (int)CMD_DBT.DBT_CONFIGCHANGED:
                                break;
                            case (int)CMD_DBT.DBT_CUSTOMEVENT:
                                break;
                            case (int)CMD_DBT.DBT_DEVICEQUERYREMOVE:
                                break;
                            case (int)CMD_DBT.DBT_DEVICEQUERYREMOVEFAILED:
                                break;
                            case (int)CMD_DBT.DBT_DEVICEREMOVECOMPLETE: //U盘卸载  
                                {
                                    //这里可以插入应用代码
                                }
                                break;
                            case (int)CMD_DBT.DBT_DEVICEREMOVEPENDING:
                                break;
                            case (int)CMD_DBT.DBT_DEVICETYPESPECIFIC:
                                break;
                            case (int)CMD_DBT.DBT_DEVNODES_CHANGED://可用,设备变化时                               
                                break;
                            case (int)CMD_DBT.DBT_QUERYCHANGECONFIG:
                                break;
                            case (int)CMD_DBT.DBT_USERDEFINED:
                                break;
                            default:
                                break;
                        }
                        break;
                }
            }
            catch
            {

            }
            base.WndProc(ref m);
        }
    }
}

2. MessageForm.cs
using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;


namespace UDiskDemo
{
    public partial class MessageForm: Form
    {
   private CustomMessageWindow customMessageWindow;    //监视U盘插入/移除类
   public MessageForm()
        {           
            InitializeComponent();
     customMessageWindow = new CustomMessageWindow(this);
        }
  }
}

你可能感兴趣的:(WinCE)