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;
}