您的位置:首页 > 编程语言 > C#

c# winform 怎么给程序的按钮上也加上小盾牌图标

2013-04-22 13:43 405 查看
需要调用Win32API了

要调用API么,要先引用命名空间
usingSystem.Runtime.InteropServices;


然后调用API
[DllImport("user32.dll")]
privatestaticexternIntPtrSendMessage(HandleRefhWnd,uintMsg,IntPtrwParam,IntPtrlParam);

///////////////////////////////////////////////////////////////////////
///<summary>
///Enablestheelevatedshieldicononthegivenbuttoncontrol
///</summary>
///<paramname="ThisButton">
///Buttoncontroltoenabletheelevatedshieldiconon.
///</param>
///////////////////////////////////////////////////////////////////////
privatevoidEnableElevateIcon_BCM_SETSHIELD(ButtonThisButton)
{
//Inputvalidation,validatethatThisControlisnotnull
if(ThisButton==null)
{
return;
}

//DefineBCM_SETSHIELDlocally,declaredoriginallyinCommctrl.h
uintBCM_SETSHIELD=0x0000160C;

//Setbuttonstyletothesystemstyle
ThisButton.FlatStyle=FlatStyle.System;

//SendtheBCM_SETSHIELDmessagetothebuttoncontrol
SendMessage(newHandleRef(ThisButton,ThisButton.Handle),BCM_SETSHIELD,newIntPtr(0),newIntPtr(1));
}


在Form上拖个Button,拖大一点哦,小了图标看不清





然后在Form1_Load里,用API把图标加到Button1上
privatevoidForm1_Load(objectsender,EventArgse)
{
EnableElevateIcon_BCM_SETSHIELD(button1);
}

最后执行看下效果吧!





恩?盾牌为啥有点不一样呢,上面那个图标是server08上的,win7上应该是下面这样:





有错误的地方欢迎指出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: