C#实现主窗体工具栏上按钮两幅图片的交互效果
2024-07-21 02:18:25
供稿:网友
初次发文,敬请包涵。
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;
}