初次发文,敬请包涵。 using System.Runtime.InteropServices; 窗口类添加以下成员或函数 private static int buttonIndex; [DllImport("User32", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, ref Point lParam); public const int TB_HITTEST = 1093; //本例为四个按钮(注意:当添加属性style为separator的按钮后要作相应的变化) //添加一个ImageList控件ImageList1(添加八个图片依次为0...7,顺序不能变, //注意交互时图片0,1,2,3分别对应图片4,5,6,7) //添加一个ToolBar控件ToolBar1(其ImageList属性为ImageList1, //四个按钮的imageindex分别为0,1,2,3)
//工具栏MouseMove事件 private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Point pt = new Point(e.X, e.Y); IntPtr result = SendMessage(this.toolBar1.Handle, TB_HITTEST, 0, ref pt); buttonIndex = result.ToInt32(); const int lowBtnIndex = 0; const int highBtnIndex =3; if((buttonIndex >= lowBtnIndex ) && (buttonIndex <= highBtnIndex)) { for(int u=0;u<4;u++) { if(u==buttonIndex) this.toolBar1.Buttons[buttonIndex].ImageIndex=4+buttonIndex; else this.toolBar1.Buttons[u].ImageIndex=u; } } else { for(int u=0;u<4;u++) this.toolBar1.Buttons[u].ImageIndex=u; } }
//工具栏MouseLeave事件 private void toolBar1_MouseLeave(object sender, System.EventArgs e) { if((buttonIndex >= 0) && (buttonIndex <=3)) { this.toolBar1.Buttons[buttonIndex].ImageIndex=buttonIndex; } buttonIndex=4; }

|