【转载】C#实现主窗体工具栏上按钮两幅图片的交互效果

 1 using System.Runtime.InteropServices; 
 2 窗口类添加以下成员或函数 
 3 private static int buttonIndex;  
 4 
 5 [DllImport("User32", CharSet = CharSet.Auto)] 
 6 public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, ref Point lParam); 
 7 public const int TB_HITTEST = 1093
 8 //本例为四个按钮(注意:当添加属性style为separator的按钮后要作相应的变化) 
 9 //添加一个ImageList控件ImageList1(添加八个图片依次为07,顺序不能变, 
10 //注意交互时图片0,1,2,3分别对应图片4,5,6,7) 
11 //添加一个ToolBar控件ToolBar1(其ImageList属性为ImageList1, 
12 //四个按钮的imageindex分别为0,1,2,3) 
13 
14 //工具栏MouseMove事件 
15 private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
16 
17 Point pt = new Point(e.X, e.Y); 
18 IntPtr result = 
19 SendMessage(this.toolBar1.Handle, TB_HITTEST, 0ref pt); 
20 buttonIndex = result.ToInt32(); 
21 const int lowBtnIndex = 0
22 const int highBtnIndex =3
23 if((buttonIndex >= lowBtnIndex ) && (buttonIndex <= highBtnIndex)) 
24 
25 for(int u=0;u<4;u++
26 
27 if(u==buttonIndex) 
28 this.toolBar1.Buttons[buttonIndex].ImageIndex=4+buttonIndex; 
29 else 
30 this.toolBar1.Buttons[u].ImageIndex=u; 
31 
32 
33 else 
34 
35 for(int u=0;u<4;u++
36 this.toolBar1.Buttons[u].ImageIndex=u; 
37 
38 
39 
40 //工具栏MouseLeave事件 
41 private void toolBar1_MouseLeave(object sender, System.EventArgs e) 
42 
43 if((buttonIndex >= 0&& (buttonIndex <=3)) 
44 
45 this.toolBar1.Buttons[buttonIndex].ImageIndex=buttonIndex; 
46 
47 buttonIndex=4
48 
49 
posted @ 2009-02-20 14:36  波波的笔记  阅读(529)  评论(0)    收藏  举报