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);
        }
  }
}

转载于:https://www.cnblogs.com/qingshui/archive/2011/02/16/1862452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值