且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.Net Micro Framework研究—实现SideShow窗体界面

更新时间:2022-09-26 20:47:40

基于MF系统的Windows SideShow界面是非常炫的(如下图)。既然微软能用.Net Micro Framework实现这么棒的界面效果,我想我们也能做到。

.Net Micro Framework研究—实现SideShow窗体界面

(SideShow模拟器界面和游戏程序中的右键菜单—注意菜单弹出后,其它的界面变暗了)
现在的任务是设计一套支持鼠标(或触摸屏)的窗体框架(目前MF提供的Window类仅支持按键功能),所以正好把SideShow如此炫的界面元素也可以添加进来。
用过MF的人知道是用下面的方法来实现按键事件接收的,既然我们要支持鼠标功能,所以***也用类似的机理实现。
   


  1. //按键事件  
  2.     protected override void OnButtonDown(ButtonEventArgs e)  
  3.     {  
  4.         switch (e.Button)  
  5.         {  
  6.             //按下确定键  
  7.             case Button.Select:  
  8.                 break;  
  9.             //按下左键  
  10.             case Button.Left:  
  11.                 break;  
  12.             //按下右键  
  13.             case Button.Right:  
  14.                 break;  
  15.             //按向上  
  16.             case Button.Up:  
  17.                 break;  
  18.             //按向下  
  19.             case Button.Down:  
  20.                 break;  
  21.             //按下菜单  
  22.             case Button.Menu:  
  23.                 break;  
  24.             //按下返回键  
  25.             case Button.Back:  
  26.                 break;  
  27.         }  
  28.         base.OnButtonDown(e);  
  29.     }  

用反编译工具仔细研究了MF底层库代码(.Net FrameWork 太庞大了,一个人绝对短时间内看不完,其实也很难看下去,但是对刚刚起步的MF来说,.Net Micro FrameWork就简单多了),终于理清了头绪。主要原理是在鼠标信息处理线程中通过Application.Current.Windows 属性(该属性存放了当前实例所有派生于Window类的窗体)和应用实例的this.Dispatcher属性的BeginInvoke方法,外部调用窗体鼠标事件函数。充分利用基类虚函数的妙处来实现类似按键信息处理的功能。
在YFWindowBase类中声明如下虚拟鼠标事件函数。
 


  1. //鼠标移动  
  2.     public virtual void OnMouseMove(object sender, MouseEventArgs e)  
  3.     {  
  4.         if (MouseMove != null) MouseMove(sender, e);  
  5.     }  
  6.     //鼠标单击  
  7.     public virtual void OnMouseClick(object sender, MouseEventArgs e)  
  8.     {  
  9.         if (MouseClick != null) MouseClick(sender, e);  
  10.     }  
  11.     //按下  
  12.     public virtual void OnMouseDown(object sender, MouseEventArgs e)  
  13.     {  
  14.        if (MouseDown != null) MouseDown(sender, e);  
  15.     }  
  16.     //抬起  
  17.     public virtual void OnMouseUp(object sender, MouseEventArgs e)  
  18.     {  
  19.         if (MouseUp != null) MouseUp(sender, e);  
  20.     }  
  21.    

在鼠标信息处理函数中执行如下的代码即可。
   


  1. //处理鼠标消息  
  2.     private static void TransactMouse(MouseState state, int x, int y, MouseButtons button)  
  3.     {  
  4.         if (Application.Current == nullreturn;  
  5.         for (int i = Application.Current.Windows.Count - 1; i >= 0; i--)  
  6.         {  
  7.             try 
  8.             {  
  9.                 YFWindowBase mw = Application.Current.Windows[i] as YFWindowBase;  
  10.    
  11.                 if (mw.Enabled && mw.IsVisible)  
  12.                 {  
  13.                     //判断子窗体  
  14.                     bool bReturn = false;  
  15.                     for (int j = mw.Children.Count - 1; j >= 0; j--)  
  16.                     {  
  17.                         //仅最上层并且可视的控件接收鼠标消息  
  18.                         YFControl cl = mw.Children[j];  
  19.                         if (!bReturn && cl.Visible && IsRectContains(x, y, mw.Left + cl.Left, mw.Top + cl.Top, cl.Width, cl.Height))  
  20.                         {  
  21.                             if (cl.Enable) //Enable和Visible不一样,Enable即使无效,下层控件也没有机会获得鼠标消息  
  22.                             {  
  23.                                 if (!cl._EnterFlag)  
  24.                                 {  
  25.                                     cl._EnterFlag = true;  
  26.                                     _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseEnter), cl, new MouseEventArgs(button, x - cl.Left - mw.Left, y - cl.Top - mw.Top));  
  27.                                 }  
  28.                                 if ((state & MouseState.Move) > 0)  
  29.                                     _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseMove), cl, new MouseEventArgs(button, x - cl.Left - mw.Left, y - cl.Top - mw.Top));  
  30.                                 if ((state & MouseState.Down) > 0)  
  31.                                     _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseDown), cl, new MouseEventArgs(button, x - cl.Left - mw.Left, y - cl.Top - mw.Top));  
  32.                                 if ((state & MouseState.Up) > 0)  
  33.                                     _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseUp), cl, new MouseEventArgs(button, x - cl.Left - mw.Left, y - cl.Top - mw.Top));  
  34.                                 if ((state & MouseState.Click) > 0)  
  35.                                     _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseClick), cl, new MouseEventArgs(button, x - cl.Left - mw.Left, y - cl.Top - mw.Top));  
  36.                             }  
  37.                             //向主窗体传OnMouseEvent消息,为了绘制鼠标  
  38.                             if ((state & MouseState.Event) > 0)  
  39.                                 _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseEvent), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  40.                             bReturn = true;  
  41.                         }  
  42.                         else 
  43.                         {  
  44.                             if (cl._EnterFlag)  
  45.                             {  
  46.                                 cl._EnterFlag = false;  
  47.                                 _dispatcher.BeginInvoke(new MouseInputEventHandler(cl.OnMouseLeave), cl, new MouseEventArgs(button, x - cl.Left, y - cl.Top));  
  48.                             }  
  49.                         }  
  50.                     }  
  51.                     if (bReturn) return;  
  52.                 }  
  53.    
  54.                 //仅最上层并且可视的窗体接收鼠标消息  
  55.                 if (mw.IsVisible && IsRectContains(x, y, mw.Left, mw.Top, mw.Width, mw.Height))  
  56.                 {  
  57.                     if (!mw.Enabled) return;  
  58.    
  59.                     if ((state & MouseState.Move) > 0)  
  60.                         _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseMove), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  61.                     if ((state & MouseState.Down) > 0)  
  62.                         _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseDown), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  63.                     if ((state & MouseState.Up) > 0)  
  64.                         _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseUp), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  65.                     if ((state & MouseState.Click) > 0)  
  66.                         _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseClick), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  67.                     if ((state & MouseState.Event) > 0)  
  68.                         _dispatcher.BeginInvoke(new MouseInputEventHandler(mw.OnMouseEvent), mw, new MouseEventArgs(button, x - mw.Left, y - mw.Top));  
  69.                     return;  
  70.                 }  
  71.             }  
  72.             catch (Exception e)  
  73.             {  
  74.                 throw new Exception(e.Message.ToString(), e);  
  75.             }  
  76.         }  
  77.     }  

  用户程序的窗体类只要派生于YFWindowBase类,就可以直接支持鼠标和按键功能了。用户代码如下:
   


  1. //主窗体  
  2.    internal sealed class MFWindow :YFWindowBase  
  3.    {  
  4.        public YFLabel label1;  
  5.        YFButton button1, button2, button3, button4, button5;  
  6.        public MFWindow()  
  7.        {  
  8.            //标签  
  9.            label1 = new YFLabel("就绪", 0, Height - 25, Width, 25);  
  10.            label1.TextAlign = TextAlignment.Left;  
  11.            label1.BackColor = ColorUtility.ColorFromRGB(189, 235, 255);  
  12.            label1.BorderStyle = BorderStyle.FixedSingle;  
  13.            //添加按钮  
  14.            button1 = new YFButton("触摸屏校准", 30, 35, 90, 40);  
  15.            button1.MouseClick += new MouseInputEventHandler(button_MouseClick);  
  16.            button2 = new YFButton("计算器",200, 35, 90, 40);  
  17.            button2.MouseClick += new MouseInputEventHandler(button_MouseClick);  
  18.            button3 = new YFButton("简易记事本", 30, 135, 90, 40);  
  19.            button3.MouseClick += new MouseInputEventHandler(button_MouseClick);  
  20.            button4 = new YFButton("关于...", 200, 135, 90, 40);  
  21.            button4.MouseClick += new MouseInputEventHandler(button_MouseClick);  
  22.            button5 = new YFButton("主菜单", 125, 85, 70, 40);  
  23.            button5.MouseClick += new MouseInputEventHandler(button_MouseClick);  
  24.            Children.Add(button1);  
  25.            Children.Add(button2);  
  26.            Children.Add(button3);  
  27.            Children.Add(button4);  
  28.            Children.Add(button5);  
  29.            Children.Add(label1);  
  30.            button3.Enable = false;  
  31.            //button3.Visible = false;  
  32.            //设置菜单  
  33.            Menu.AddItem(new MenuItem("触摸屏校准"));  
  34.            Menu.AddItem(new MenuItem("-"));  
  35.            Menu.AddItem(new MenuItem("计算器"));  
  36.            Menu.AddItem(new MenuItem("简易记事本"));  
  37.            Menu.AddItem(new MenuItem("-"));  
  38.            Menu.AddItem(new MenuItem("关于..."));  
  39.            Menu[3].Enabled = false;  
  40.            //Menu[3].Visible = false;  
  41.        }          
  42.        //按钮事件  
  43.        void button_MouseClick(object sender, MouseEventArgs e)  
  44.        {  
  45.            YFButton button=((YFButton)sender);  
  46.            switch (button.Text)  
  47.            {  
  48.                case "主菜单":  
  49.                    //弹出菜单  
  50.                    this.Menu.Show();  
  51.                    break;  
  52.                default:  
  53.                    OnMenuClick(new MenuEventArgs(0, button.Text));  
  54.                    break;  
  55.            }                            
  56.        }  

运行后的界面如下:
 

.Net Micro Framework研究—实现SideShow窗体界面

图1:主界面(按钮即支持鼠标也可以用按键切换输入焦点(right键等同于PC平台上的Tab键),并用OK键触发按键事件)

 

.Net Micro Framework研究—实现SideShow窗体界面

图2:单击主菜单按钮或单击“Menu”就可以弹出主菜单

.Net Micro Framework研究—实现SideShow窗体界面


 

图3:不要小看了上图的蓝色小圆,是我费了好大劲才绘制出来的(目前MF仅支持矩形框的填充)。
 


  1. private void DrawCircle(Color c,int x, int y, int r, DrawingContext dc)  
  2.     {  
  3.         Pen p=new Pen(c);  
  4.         SolidColorBrush b = new SolidColorBrush(c);  
  5.         int Offset45=(int)(0.707*r);  
  6.         int Offset30 = r / 2;  
  7.         int Offset60 = (int)(0.866 * r);  
  8.    
  9.         for (int i = Offset45; i < r + 1; i++)  
  10.         {  
  11.             dc.DrawEllipse(null, p, x, y, i, i);  
  12.         }  
  13.         dc.DrawRectangle(b, null, x - Offset45, y - Offset45,Offset45*2, Offset45*2);  
  14.         dc.DrawRectangle(b, null, x - Offset60, y - Offset30, Offset60 * 2, Offset30 * 2);  
  15.         dc.DrawRectangle(b, null, x - Offset30, y - Offset60, Offset30 * 2, Offset60 * 2);  
  16.    
  17.         dc.DrawLine(p, x - Offset60, y - Offset30, x - Offset30, y - Offset60);  
  18.         dc.DrawLine(p, x+ Offset60 , y + Offset30, x + Offset30 ,y + Offset60 );  
  19.         dc.DrawLine(p, x - Offset60, y + Offset30, x - Offset30 , y + Offset60);  
  20.         dc.DrawLine(p, x + Offset60, y - Offset30, x + Offset30, y - Offset60);  
  21.    }  
  22.    
  23.    

图5:终于在MF上实现了计算器的功能,目前不仅支持鼠标,也可以用光标键和OK键进行输入计算了。
别小看了计算器程序,由于MF仅有数字转字符串功能,没有实现字符串转数字的功能,我自己自定义了一些函数,用了一些特殊的用法才编写完成。
 


  1. //数字输入的部分代码  
  2. if (strInput == "0.") dblInput = 0;  
  3.         if (strInput != "0.")  
  4.         {  
  5.             strInput += b.Text;  
  6.             if (strInput.IndexOf('.')<1)  
  7.             {  
  8.                 dblInput *= 10;  
  9.                 dblInput += ToDouble(b.Text);  
  10.             }  
  11.             else 
  12.             {  
  13.                 int index = strInput.Length - strInput.LastIndexOf('.') - 1;  
  14.                 dblInput += ToDouble(b.Text) /System.Math.Pow(10,index);  
  15.             }  
  16.         }  
  17.         else if (b.Text != "0")  
  18.         {  
  19.             strInput = b.Text;  
  20.             dblInput = ToDouble(b.Text);  
  21.      }  

   下面是窗体的界面搭建部分,你绝对想不到用这么短的代码就实现了上图的界面布局(看过我以前.Net Micro Framework研究的网友,应该对MF本身提供的控件有印象,正是因为系统的控件不好用,我专门重新写了一套MF控件类)。

.Net Micro Framework研究—实现SideShow窗体界面


   


  1.  YFButton[] button=new YFButton[20];  
  2.     YFLabel lblInput = null;  
  3.     string[] strText = new string[] { "7""8""9""/""CE""4""5""6""*""%""1""2""3""-""1/x""0""+/-"".""+""=" };  
  4.     public YFCalc(string Title,int Width,int Height,YFWindowBase Parent)  
  5.         : base(Title, Width, Height, Parent)  
  6.     {  
  7.         int x=ClientRect.X,y=ClientRect.Y;  
  8.    
  9.         lblInput = new YFLabel("0.", x + 5, y + 5, ClientRect.Width - 10, 20);  
  10.         lblInput.TextAlign = TextAlignment.Right;  
  11.         Children.Add(lblInput);   
  12.         for (int i = 0; i < 20; i++)  
  13.         {  
  14.             if(i % 5==0 && i!=0)  
  15.             {  
  16.                 x = ClientRect.X;  
  17.                 y = y + 32;  
  18.             }  
  19.             button[i] = new YFButton("", x + 5, y + 32, 32, 28);  
  20.             button[i].Text = strText[i];  
  21.             button[i].MouseClick += new MouseInputEventHandler(button_MouseClick);   
  22.             x += 37;  
  23.             Children.Add(button[i]);   
  24.         }                 
  25.     }  
  26.     //按钮单击  
  27.     void button_MouseClick(object sender, MouseEventArgs e)  
  28. {  
  29. }  

其实这段时间以来,我一直在研究MF,虽然目前它还不是很成熟,但是随着研究的深入,越来越对它痴迷,越来越发现很多MF的宝藏(如果你有耐心的话,一定要看看MF底层框架的源码(通过反编译工具Reflector),你会发现很多很有意思的功能)。
MF相对于Windows XP/Vista、Windows CE而言,还只能算一个婴孩,但就是这样,就如一个伟人所说:孩子就是未来的希望。所以有理由相信MF的明天会更好









本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/322900,如需转载请自行联系原作者