织梦CMS - 轻松建站从此开始!

罗索

Wince5.0自定义ToolBar

落鹤生 发布于 2010-09-03 21:51 点击:次 
做WinCE的开发时,菜单很难看,字体很大,样式也太简单,就选用了 CF2.0中的ToolBar控件。用户使用后反应很大,普遍说是用不方便,因为Toolbar没有文本,只有图标。所以自定义一个Toolbar,这里不方便提供所有源码,只是讲述开发过程中碰到的几点问题,相信对其他朋友有
TAG:

2007-3-18   Wince5.0自定义ToolBar

做WinCE的开发时,碰到一很头疼的事(Coolpad机器)。它的菜单很难看,字体很大,样式也太简单,就选用了 CF2.0中的ToolBar控件。用户使用后反应很大,普遍说是用不方便,因为Toolbar没有文本,只有图标。所以自定义一个Toolbar,这里不方便提供所有源码,只是讲述开发过程中碰到的几点问题(主要问题),相信对其他朋友有帮助。

自定工具条ToolBarEx,我是模仿vs2005的工具条做,增加了文本功能。

1.高度25像素,图标16×16;

2.vs2005的工具条中,头部有4个点,我模仿做时,发现是8个方块(4*4像素),4个灰色方块,4个白色方块;白色方块向右下偏移1像素,这样画出来,就跟vs2005的工具条效果一致了;

3.分隔线(Separator button)。v s2005的工具条,分隔线高度20像素,颜色我选用的是Gray;

4.背景色选用渐变的颜色,CF2.0中没有提供渐变的GDI+方法,所以采取调用GDI的API函数实现,代码如下:

  1. WinApi.DrawNiceRectangle(hdc, frame); 
  2. internal class WinApi 
  3. private const uint 
  4.   GRADIENT_FILL_RECT_H = 0x00000000, 
  5.   GRADIENT_FILL_RECT_V = 0x00000001, 
  6.   GRADIENT_FILL_TRIANGLE = 0x00000002, 
  7.   GRADIENT_FILL_OP_FLAG = 0x000000ff; 
  8.   
  9. [DllImport("coredll")] 
  10. private static extern bool GradientFill( 
  11.   IntPtr hdc,   // handle to DC 
  12.   TRIVERTEX[] pVertex,// array of vertices 
  13.   uint dwNumVertex, // number of vertices 
  14.   ref GRADIENT_RECT pMesh,   // array of gradients 
  15.   uint dwNumMesh,   // size of gradient array 
  16.   uint dwMode   // gradient fill mode 
  17. ); 
  18.   
  19. internal static void DrawNiceRectangle(IntPtr graphPort,
  20.  Rectangle Frame, Color a, Color b) 
  21. // Draw from whitish blue at the top, to a light blue at the bottom 
  22. //TRIVERTEX[] vert = new TRIVERTEX[]{ 
  23. //new TRIVERTEX(Frame.Left,Frame.Top,0xff00,0xff00,0xffff,0xff00), 
  24. //new TRIVERTEX(Frame.Right,Frame.Bottom,0x0000,0x0000,0xff00,0xff00)}; 
  25. TRIVERTEX[] vert = new TRIVERTEX[]{ 
  26. new TRIVERTEX(Frame.Left,Frame.Top,ToColor16(a.R),ToColor16(a.G),
  27. ToColor16(a.B),(ushort)(0)), 
  28. new TRIVERTEX(Frame.Right,Frame.Bottom,ToColor16(b.R) ,ToColor16(b.G ),
  29. ToColor16(b.B) ,(ushort)(0))}; 
  30.   
  31. GRADIENT_RECT gRect = 
  32. new GRADIENT_RECT(0, 1); 
  33.   
  34. WinApi.GradientFill(graphPort, vert, 2, ref gRect, 1, WinApi.GRADIENT_FILL_RECT_V); 
  35.   
  36. private static ushort ToColor16(byte value) 
  37. return (ushort)((int)value << 8); 
  38.  
  39.  
  40. internal static void DrawNiceRectangle(IntPtr graphPort, Rectangle Frame) 
  41. Color a = Color.FromArgb(239, 239, 239); 
  42. Color b = Color.FromArgb(139, 139, 139); 
  43. DrawNiceRectangle(graphPort, Frame, a, b); 

这里采取的是从上至下的渐变,因为渐变高度的问题,所以有些颜色可能看不到效果,测试时可以调整高度,会看到效果。 

5.点击ToolBarExButton时的效果,vs2005中的效果是深色的边框(border  color)+比边框色浅一些的颜色,代码如下:

  1. //我采取的border color 
  2. Color border = Color.FromArgb(0, 0, 128); 
  3. //计算填充色 
  4. Color fillColor = GetHighlightColor(border); 
  5.     
  6. //原理就是将指定的Color淡化,请注意2个系数相加必须=1;用户可以自己调整系数,调节颜色的深度 
  7. private Color GetHighlightColor(Color cl) 
  8. int num1 = (int)(cl.R * 0.2); 
  9. int num4 = (int)(cl.G * 0.2); 
  10. int num7 = (int)(cl.B * 0.2); 
  11. int num2 = (int)(Color.White.R * 0.8); 
  12. int num5 = (int)(Color.White.G * 0.8); 
  13. int num8 = (int)(Color.White.B * 0.8); 
  14. return Color.FromArgb(num1 + num2, num4 + num5, num7 + num8); 

6.  当Enabled=false时的效果,这个当初也是困扰我很久,如何灰显图标,因为我一直不愿意做个工具条,还用2套图标(正常和灰显);灰显图标的方法:

  1. private static bool GrayImage(Bitmap b) 
  2. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), 
  3.  ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
  4. int stride = bmData.Stride; 
  5. System.IntPtr Scan0 = bmData.Scan0; 
  6. unsafe 
  7. byte* p = (byte*)(void*)Scan0; 
  8. int nOffset = stride - b.Width * 3; 
  9. byte red, green, blue; 
  10. for (int y = 0; y < b.Height; ++y) 
  11. for (int x = 0; x < b.Width; ++x) 
  12. blue = p[0]; 
  13. green = p[1]; 
  14. red = p[2]; 
  15. p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue); 
  16. p += 3; 
  17. p += nOffset; 
  18. b.UnlockBits(bmData); 
  19. return true

7.  ToolBarExButtonClick事件,上面所做的都是绘制工具条,现在进入正题,如何触发按钮单击事件。我是计算鼠标按下时的位置,然后通过每个ToolBarExButton的Rect判断是否为某个button,是则抛出事件。

8.  ToolBarEx代码层次,仅供参考。

ToolBarEx;

ToolBarExButton;

ToolBarExButtonClickEventArgs;

ToolBarExButtonCollection;

ToolBarExButtonStyle;

ToolBarExTextAlign;

ToolBarExDelegate;

9.效果如下:(通过PDA抓得图,有点失真,不过大致效果还能看到)

(ToolBarEx效果)                     (CF2.0中 ToolBar效果)

题外话,大家可以看到上面有一pda地图的界面。近半年来一直在搞移动地图引擎的开发,希望和从事相关行业的朋友交流,特别是移动地图的效率问题,大地图文件时效率问题。

(michael-zhangyu)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201009/10088.html]
本文出处:博客园 作者:michael-zhangyu
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容