Usage of image buttons

Source: Internet
Author: User

1.Directly use the compiled picbutton. DLL to add references to the project. In the "toolbar", right-click and choose item --. NET Framework component -- browse -- find picbutton, add picbutton, and click the image button in the toolbar.

2.Edit the picbutton project in the project. In fact, the DLL file of the above method is the specific implementation method compiled here:

1.Create a new project (add a class library) in the project, such as the picbutton class library.

  

After adding:

Add a reference and edit class1.cs as follows:

     

After class1.cs is modified as follows, when class1 is changed to inherit control, it is noted that the file mark of class1 has changed:

Then generate a project. You can see the class1 control in the window (you can also change it to your favorite name as needed) and add it to the form to rewrite it.

 

 

Using system; using system. drawing; using system. drawing. imaging; using system. windows. forms; using system. collections. generic; using system. text; using system. componentmodel; using system. runtime. interopservices; namespace picbutton {public class class1: Control {// Private member private image imageori, imagehover; // indicates private bool bpushed, bhovered in the pressed state; // buffer bitmap private bitmap m_bmpoffscreen; // whether to automatically adjust the widget size to suit different sizes Small image private bool bautosetsize; Public class1 (): Base () {bpushed = false; bhovered = false; bautosetsize = true;} [description ("default image, size should be consistent with imagehover "), category (" Custom ")] public image imageori {get {return imageori;} set {imageori = value; If (bautosetsize) setsizetoimage (value); this. invalidate () ;}} [description ("image when hovering, the size should be consistent with imageoriginal"), category ("Custom")] public image imagehover {get {Return imagehover;} set {imagehover = value; If (bautosetsize) setsizetoimage (value); this. invalidate () ;}} [description ("whether to automatically adjust to the size of imageori"), category ("Custom")] public bool autosetsize {get {return bautosetsize ;} set {bautosetsize = value; If (bautosetsize) {setsizetoimage (this. imageori) ;}} protected override void onpaintbackground (painteventargs e) {// do not perform any operation} protected override v Oid onclick (eventargs e) {// update the UI before the click event is triggered to prevent the event from responding to the UI after it is processed. bpushed = false; this. bhovered = true; this. refresh (); base. onclick (E);} protected override void onpaint (system. windows. forms. painteventargs e) {base. onpaint (E); graphics gxoff; // rectangle imgrect; // image rectangle if (imageori = NULL) {e. graphics. clear (this. backcolor); return;} If (m_bmpoffscreen = NULL) // bitmap for double buffering {// see onresize method M. _ Bmpoffscreen = new Bitmap (clientsize. width, clientsize. height);} gxoff = graphics. fromimage (m_bmpoffscreen); gxoff. clear (parent. backcolor); imageattributes imageattr = new imageattributes (); image imagenow = imageori; // align the image with the control imgrect = new rectangle (this. width-imagenow. width)/2, (this. height-imagenow. height)/2, imagenow. width, imagenow. height); If (this. enabled & bpushed & imageh Over! = NULL) {imagenow = imagehover; imgrect = new rectangle (this. Width-imagenow. width)/2, (this. Height-imagenow. Height)/2,
Imagenow. Width, imagenow. Height); imgrect. offset (1, 1);} else if (this. enabled & bhovered & imagehover! = NULL) {imagenow = imagehover;} imageattr. setcolorkey (backgroundimagecolor (imagenow), backgroundimagecolor (imagenow); // if not available, convert the current image to a grayscale if (! This. enabled) getcolortrans (imageattr); // draws the image gxoff. drawimage (imagenow, imgrect, 0, 0, imagenow. width, imagenow. height, graphicsunit. pixel, imageattr); // draws E from the memory bitmap. graphics. drawimage (m_bmpoffscreen, 0, 0); gxoff. dispose (); gxoff = NULL;} private void setsizetoimage (image IMG) {If (IMG! = NULL) {system. drawing. rectangle r = new rectangle (0, 0, IMG. width, IMG. height); R. inflate (2, 2); this. width = R. width; this. height = R. height ;}} private color backgroundimagecolor (image) {bitmap BMP = new Bitmap (image); Return BMP. getpixel (0, 0);} private void getcolortrans (imageattributes) {// set the current R, G, the color component of B is transformed according to the formula gray = r * 0.39 + G * 0.50 + B * 0.11. // note that r = gray, G = gray, B = gray float [] [] colormatrixelements = {New float [] {0.39f, 0.39f, 0.39f, 0, 0}, // red scaling factor of 2new float [] {0.50f, 0.50f, 0.50f, 0, 0}, // green scaling factor of 1new float [] {0.11f, 0.11f, 0.11f, 0, 0 }, // blue scaling factor of 1new float [] {0, 0, 0, 1, 0}, // Alpha scaling factor of 1new float [] {0, 0, 0, 0, 1 }}; // Three Translations of 0.2 colormatrix = new colormatrix (colormatrixelements); imageattributes. setcolormatrix (colormatrix, colormatrixflag. default, coloradjusttype. bitmap );
        }       public new bool Enabled        {           get { return base.Enabled; }            set            {                base.Enabled = value;                this.Invalidate();            }      }       protected override void OnMouseEnter(EventArgs e)        {            base.OnMouseEnter(e);            bHovered = true;            this.Invalidate();        }        protected override void OnMouseLeave(EventArgs e)        {            base.OnMouseLeave(e);            bHovered = false;            this.Invalidate();
} Protected override void onmousemove (mouseeventargs e) {// when the mouse is pressed, the move message will still be received outside the customer zone, but it does not receive the leave message // Therefore, You need to determine whether the current mouse is in the customer zone in the move message, and trigger the corresponding onmouseleave or onmouseenter base. onmousemove (E); If (E. button = mousebuttons. left) {If (! This. clientrectangle. contains (E. x, E. y) {// simulate onmouseleave if (this. bhovered) {This. bpushed = false; onmouseleave (eventargs. empty) ;}} else {// when the mouse is pressed, it is moved from the customer area to the customer area, and then moved back to the customer area, simulating onmouseenter, and bpushed = true if (! This. bhovered) bpushed = true; onmouseenter (eventargs. empty) ;}/// The following is the added processing: else {// *** simulate onmouseenter. For more information, see the necessity of idle processing if (bhovered = false) {// *** when receiving the move message, the mouse is not necessarily in the customer zone. If (this. clientrectangle. contains (E. x, E. y) Judge if (this. clientrectangle. contains (E. x, E. Y ))
{Bhovered = true; this. invalidate () ;}}} protected override void onresize (eventargs e) {base. onresize (E);/* important. To save resources, instead of generating a buffer bitmap * every time you paint the image, you must regenerate the m_bmpoffscreen with the new size when the size changes */m_bmpoffscreen = NULL; this. invalidate ();} protected override void onmousedown (system. windows. forms. mouseeventargs e) {base. onmousedown (E); If (E. button = mousebuttons. left) {bpushed = true; this. invalidate () ;}} protected override void onmouseup (system. windows. forms. mouseeventargs e) {base. onmouseup (E); bpushed = false; this. invalidate ();} public void initialize mclick () {This. onclick (eventargs. empty );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.