Wince5.0自定义工具条
2007-3-18?? Wince5.0自定義ToolBar
做WinCE的開發(fā)時(shí),碰到一很頭疼的事(Coolpad機(jī)器)。它的菜單很難看,字體很大,樣式也太簡單,就選用了 CF2.0中的ToolBar控件。用戶使用后反應(yīng)很大,普遍說是用不方便,因?yàn)?/span>Toolbar沒有文本,只有圖標(biāo)。所以自定義一個(gè)Toolbar,這里不方便提供所有源碼,只是講述開發(fā)過程中碰到的幾點(diǎn)問題(主要問題),相信對(duì)其他朋友有幫助。
自定工具條ToolBarEx,我是模仿vs2005的工具條做,增加了文本功能。
1.????? 高度25像素,圖標(biāo)16×16;
2.????? vs2005的工具條中,頭部有4個(gè)點(diǎn),我模仿做時(shí),發(fā)現(xiàn)是8個(gè)方塊(4*4像素),4個(gè)灰色方塊,4個(gè)白色方塊;白色方塊向右下偏移1像素,這樣畫出來,就跟vs2005的工具條效果一致了;
3.????? 分隔線(Separator button)。v s2005的工具條,分隔線高度20像素,顏色我選用的是Gray;
4.????? 背景色選用漸變的顏色,CF2.0中沒有提供漸變的GDI+方法,所以采取調(diào)用GDI的API函數(shù)實(shí)現(xiàn),代碼如下:
WinApi.DrawNiceRectangle(hdc, frame);
?
internal class WinApi
??? {
??????? private const uint
????????? GRADIENT_FILL_RECT_H = 0x00000000,
????????? GRADIENT_FILL_RECT_V = 0x00000001,
????????? GRADIENT_FILL_TRIANGLE = 0x00000002,
????????? GRADIENT_FILL_OP_FLAG = 0x000000ff;
?
??????? [DllImport("coredll")]
??????? private static extern bool GradientFill(
????????? IntPtr hdc,?????????? // handle to DC
????????? TRIVERTEX[] pVertex,??? // array of vertices
????????? uint dwNumVertex,???? // number of vertices
????????? ref GRADIENT_RECT pMesh,?????????? // array of gradients
????????? uint dwNumMesh,?????? // size of gradient array
????????? uint dwMode?????????? // gradient fill mode
??????? );
?
??????? internal static void DrawNiceRectangle(IntPtr graphPort, Rectangle Frame, Color a, Color b)
??????? {
??????????? // Draw from whitish blue at the top, to a light blue at the bottom
??????????? //TRIVERTEX[] vert = new TRIVERTEX[]{
??????????? //??????? new TRIVERTEX(Frame.Left,Frame.Top,0xff00,0xff00,0xffff,0xff00),
??????????? //??????? new TRIVERTEX(Frame.Right,Frame.Bottom,0x0000,0x0000,0xff00,0xff00)};
??????????? TRIVERTEX[] vert = new TRIVERTEX[]{
??????????????????? new TRIVERTEX(Frame.Left,Frame.Top,ToColor16(a.R),ToColor16(a.G),ToColor16(a.B),(ushort)(0)),
??????????????????? new TRIVERTEX(Frame.Right,Frame.Bottom,ToColor16(b.R) ,ToColor16(b.G ),ToColor16(b.B) ,(ushort)(0))};
?
??????????? GRADIENT_RECT gRect =
??????????????????? new GRADIENT_RECT(0, 1);
?
??????????? WinApi.GradientFill(graphPort, vert, 2, ref gRect, 1, WinApi.GRADIENT_FILL_RECT_V);
??????? }
?
?????? ?private static ushort ToColor16(byte value)
??????? {
??????????? return (ushort)((int)value << 8);
??????? }
?
?
?
??????? internal static void DrawNiceRectangle(IntPtr graphPort, Rectangle Frame)
??????? {
??????????? Color a = Color.FromArgb(239, 239, 239);
??????????? Color b = Color.FromArgb(139, 139, 139);
??????????? DrawNiceRectangle(graphPort, Frame, a, b);
?}
}
?
這里采取的是從上至下的漸變,因?yàn)闈u變高度的問題,所以有些顏色可能看不到效果,測試時(shí)可以調(diào)整高度,會(huì)看到效果。
?
5.????? 點(diǎn)擊ToolBarExButton時(shí)的效果,vs2005中的效果是深色的邊框(border? color)+比邊框色淺一些的顏色,代碼如下:
?
//我采取的border color
Color border = Color.FromArgb(0, 0, 128);
//計(jì)算填充色
Color fillColor = GetHighlightColor(border);
???????
//原理就是將指定的Color淡化,請(qǐng)注意2個(gè)系數(shù)相加必須=1;用戶可以自己調(diào)整系數(shù),調(diào)節(jié)顏色的深度
private Color GetHighlightColor(Color cl)
??????? {
??????????? int num1 = (int)(cl.R * 0.2);
???? ???????int num4 = (int)(cl.G * 0.2);
??????????? int num7 = (int)(cl.B * 0.2);
??????????? int num2 = (int)(Color.White.R * 0.8);
??????????? int num5 = (int)(Color.White.G * 0.8);
??????????? int num8 = (int)(Color.White.B * 0.8);
??????????? return Color.FromArgb(num1 + num2, num4 + num5, num7 + num8);
}
6.? 當(dāng)Enabled=false時(shí)的效果,這個(gè)當(dāng)初也是困擾我很久,如何灰顯圖標(biāo),因?yàn)槲乙恢辈辉敢庾鰝€(gè)工具條,還用2套圖標(biāo)(正常和灰顯);灰顯圖標(biāo)的方法:
private static bool GrayImage(Bitmap b)
??????? {
??????????? BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
??????????? int stride = bmData.Stride;
??????????? System.IntPtr Scan0 = bmData.Scan0;
??????????? unsafe
??????????? {
??????????????? byte* p = (byte*)(void*)Scan0;
??????????????? int nOffset = stride - b.Width * 3;
??????????????? byte red, green, blue;
??????????????? for (int y = 0; y < b.Height; ++y)
??????????????? {
??????????????????? for (int x = 0; x < b.Width; ++x)
??????????????????? {
??????????????????????? blue = p[0];
???????????? ???????????green = p[1];
??????????????????????? red = p[2];
??????????????????????? p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
??????????????????????? p += 3;
??????????????????? }
??????????????????? p += nOffset;
???????????? ???}
??????????? }
??????????? b.UnlockBits(bmData);
??????????? return true; }
?
7.? ToolBarExButtonClick事件,上面所做的都是繪制工具條,現(xiàn)在進(jìn)入正題,如何觸發(fā)按鈕單擊事件。我是計(jì)算鼠標(biāo)按下時(shí)的位置,然后通過每個(gè)ToolBarExButton的Rect判斷是否為某個(gè)button,是則拋出事件。
8.? ToolBarEx代碼層次,僅供參考。
ToolBarEx;
ToolBarExButton;
ToolBarExButtonClickEventArgs;
ToolBarExButtonCollection;
ToolBarExButtonStyle;
ToolBarExTextAlign;
ToolBarExDelegate;
9.???????????? 效果如下:(通過PDA抓得圖,有點(diǎn)失真,不過大致效果還能看到)
(ToolBarEx效果)???????????????????? (CF2.0中 ToolBar效果)
?
?? 題外話,大家可以看到上面有一pda地圖的界面。近半年來一直在搞移動(dòng)地圖引擎的開發(fā),希望和從事相關(guān)行業(yè)的朋友交流,特別是移動(dòng)地圖的效率問題,大地圖文件時(shí)效率問題。
轉(zhuǎn)載于:https://www.cnblogs.com/michael-zhangyu/archive/2008/03/19/1112903.html
總結(jié)
以上是生活随笔為你收集整理的Wince5.0自定义工具条的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2008年初看的书[带简评]
- 下一篇: 财务一体化项目,进度与计划11