时间: 2021-07-31 作者:daque
using system;using system.collections;using system.componentmodel;using system.drawing;using system.data;using system.windows.forms;using system.diagnostics;namespace eastspider{/// <summary>/// summary description for vsnetbutton./// </summary>public class xpbutton : system.windows.forms.button{bool gotfocus = false;bool mousedown = false;bool mouseenter = false;bool usedisableimage = true;public xpbutton(){setstyle(controlstyles.allpaintinginwmpaint|controlstyles.userpaint|controlstyles.opaque, true);}public bool usedisableimage{get{return usedisableimage;}set{usedisableimage = value;}}protected override void onpaint(painteventargs pe){base.onpaint(pe);graphics g = pe.graphics;if ( mousedown ){drawselectedstate(g, colorutil.vsnetpressedcolor);return;}if (mouseenter) {drawselectedstate(g, colorutil.vsnetselectioncolor);return;}if ( enabled )drawnormalstate(pe.graphics);elsedrawdisablestate(pe.graphics);}protected override void onmouseenter(eventargs e){mouseenter = true;base.onmouseenter(e);invalidate();}protected override void onmouseleave(eventargs e){mouseenter = false;base.onmouseleave(e);invalidate();}protected override void onmousedown(mouseeventargs e){// don't call base class// it makes the painting to screw upbase.onmousedown(e);mousedown = true;invalidate();}protected override void onmouseup(mouseeventargs e){// don't call base class// it makes the painting to screw updebug.writeline("onmouseup callled...");base.onmouseup(e);mousedown = false;invalidate();}protected override void ongotfocus(eventargs e){base.ongotfocus(e);gotfocus = true;invalidate();}protected override void onlostfocus(eventargs e){base.onlostfocus(e);gotfocus = false;invalidate();}protected void drawnormalstate(graphics g){rectangle rc = clientrectangle;// draw backgroundg.fillrectangle(new solidbrush(systemcolors.control), rc);if ( image != null ){sizef sizef = image.physicaldimension;int imagewidth = (int)sizef.width;int imageheight = (int)sizef.height;// we are assuming that the button image is smaller than// the button itselfif ( imagewidth > rc.width || imageheight > rc.height){debug.writeline("image dimensions need to be smaller that button's dimension...");return;}int x = (width - imagewidth)/2;int y = (height - imageheight)/2;g.drawimage(image, x, y, imagewidth, imageheight);}}protected void drawdisablestate(graphics g){// draw disable imagerectangle rc = clientrectangle;// erase whaterver that was there beforeg.fillrectangle(new solidbrush(systemcolors.control), rc);// draw border rectangleg.drawrectangle(new pen(new solidbrush(systemcolors.controldark)), rc.left, rc.top, rc.width-1, rc.height-1);// draw disable imageif ( image != null ){sizef sizef = image.physicaldimension;int imagewidth = (int)sizef.width;int imageheight = (int)sizef.height;// we are assuming that the button image is smaller than// the button itselfif ( imagewidth > rc.width || imageheight > rc.height){debug.writeline("image dimensions need to be smaller that button's dimension...");return;}int x = (width - imagewidth)/2;int y = (height - imageheight)/2;controlpaint.drawimagedisabled(g, image, x, y, systemcolors.control);}}protected void drawselectedstate(graphics g, color selcolor){rectangle rc = clientrectangle;// erase whaterver that was there beforeg.fillrectangle(new solidbrush(selcolor), rc);// draw border rectangleg.drawrectangle(new pen(new solidbrush(systemcolors.highlight)), rc.left, rc.top, rc.width-1, rc.height-1);// draw imageif ( image != null ){sizef sizef = image.physicaldimension;int imagewidth = (int)sizef.width;int imageheight = (int)sizef.height;// we are assuming that the button image is smaller than// the button itselfif ( imagewidth > rc.width || imageheight > rc.height){debug.writeline("image dimensions need to be smaller that button's dimension...");return;}int x = (width - imagewidth)/2;int y = (height - imageheight)/2;int gap = 1;if ( selcolor != colorutil.vsnetpressedcolor ) {// draw disable image first to produce the popping image effectif ( usedisableimage ) {controlpaint.drawimagedisabled(g, image, x, y, systemcolors.control);}elsegap = 0;}else {gap = 0;}g.drawimage(image, x-gap, y-gap, imagewidth, imageheight);}}}}