//这个用户自定义控件继承自button类
//实现了在按纽左侧显示图片的任务
//和点击后显示不同样式的功能
//下面就是人家的了
//本人加如了一些注释
using system;
using system.windows.forms;
using system.drawing;
namespace nettoggle
{
/// <summary>
/// summary description for customflatbutton.
/// </summary>
public class customflatbutton : system.windows.forms.button
{
//used to set the type of image that is displayed with the button
public enum toggle_type{ none, xml_toggle, html_toggle };//显示类型
private bool bselected = false;//是否被选择
private system.windows.forms.imagelist flatbtnimagelist;//从工具栏拖进来的了
//他的一些东西有vs.net工具自动生成
private system.componentmodel.icontainer components;
private int toggletype = 0;
public int toggletype
{
get{ return toggletype; }
set{ toggletype = value; }
}
public bool bchangeselection
{
get{ return bselected; }
set{ bselected = value; invalidate(); }
}
public customflatbutton()
{
initializecomponent();
}
private void initializecomponent()
{
this.components = new system.componentmodel.container();
system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(customflatbutton));
this.flatbtnimagelist = new system.windows.forms.imagelist(this.components);
//
// flatbtnimagelist
//
this.flatbtnimagelist.colordepth = system.windows.forms.colordepth.depth8bit;
this.flatbtnimagelist.imagesize = new system.drawing.size(9, 9);
this.flatbtnimagelist.imagestream = ((system.windows.forms.imageliststreamer)(resources.getobject("flatbtnimagelist.imagestream")));
this.flatbtnimagelist.transparentcolor = system.drawing.color.transparent;
//
// customflatbutton
//
this.flatstyle = system.windows.forms.flatstyle.flat;
this.font = new system.drawing.font("tahoma", 8.25f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
this.size = new system.drawing.size(57, 19);
}
protected override void onmouseenter( system.eventargs e )
{
return;
}
protected override void onclick( system.eventargs e)
{
bselected = true;//触发bchangeselection的写属性–调用this.invalidate();–引起对onpaint()的调用
base.onclick(e);
}
protected override void onmousedown( system.windows.forms.mouseeventargs e)
{
onclick(e);
}
protected override void onpaint( system.windows.forms.painteventargs e )
{
rectangle rect = e.cliprectangle;
graphics g = e.graphics;
if( bselected )
{
g.fillrectangle( new solidbrush(color.white), rect );
g.drawstring( this.text, this.font, new solidbrush(this.forecolor), 20, (float)2.5 );
g.drawrectangle( new pen(system.drawing.systemcolors.highlight, 2), rect );
}
else
{
g.fillrectangle( new solidbrush(system.drawing.systemcolors.control), rect );
g.drawstring( this.text, this.font, new solidbrush(this.forecolor), 20, (float)2.5 );
}
//now draw the correct image if one is specified
if( toggletype == (int)toggle_type.xml_toggle )
g.drawimage( this.flatbtnimagelist.images[0], 8, 5 );
else if( toggletype == (int)toggle_type.html_toggle )
g.drawimage( this.flatbtnimagelist.images[1], 7, 5 );
}
}
}
