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

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

初次发文,敬请包涵。

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航