Button images
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Button images

  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Question Button images

    Hi All,

    I have a button, 'changed' the background image to image A
    as the mouse pointer enters the button area it changes to image B
    and if the mouse pointer leaves the area it changes back to image A
    All this using events.
    So far so good.

    But what I want is; when the pointer enters the area this should change to image B and after several milli sec. back to image A. (even when the pointer is still in the button area).
    Creating a sort of a flash. (images A and B are more or less the same, B is a lighter one)

    using;

    button.backgroundimage = A;
    button.backgroundimage = B;

    does not work

    where do I go wrong?

    regards,

    ger

  2. #2
    Join Date
    Apr 2010
    Posts
    131

    Re: Button images

    In order to get a flash effect, you need to put a delay in the code, and the delay has to be run on another thread (or the form will not update). Psuedocode:

    Code:
    //add reference to class
    using System.Threading;
    //class globals
    image imageA;
    image imageB;
    Button flashButton;
    //this code assumes you have loaded imageA and imageB with the images you want to use. If you run as-is, they will be null.  
    
    
    //add this 3-line snippet to your button event method
    flashButton = (Button)sender;
    Thread buttonFlashThread = new Thread(flashTheButton);
    buttonFlashThread.Start();
    
    //call this method when the mouse enters (this is done by adding the 3-line snippet above to your button event handler method)
    void flashTheButton(){
       BeginInvoke(new MethodInvoker(performFlash));
       Thread.Sleep(200);//increase/decrease for effect, this is a 2/10th second delay
       BeginInvoke(new MethodInvoker(performFlash));
    }
    void  performFlash(){
       if(flashButton.Image==imageA){
          flashButton.Image = imageB;
       }else{
          flashButton.Image = imageA;
      }
    }
    There are many more efficient ways to accomplish this (without using globals, particularly), but I don't know how familiar you are with threading, so I used globals and BeginInvoke to skirt delegates and other more complicated things. Good luck!

  3. #3
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    Hi mrgr8avill,

    Thanks for the reply !!

    I put the code into my project, and it works !!
    I will study the code the next days, as do not fully understand it.
    the BeginInvoke is still magic to me, have to figger that one out.

    Next;

    how do I change the standard focus dotted line, on the button.
    for this state I have also a button pic. avail.
    So I want to get rid of the dotted line and use my own.

    regards,

    Ger

  4. #4
    Join Date
    Apr 2010
    Posts
    131

    Re: Button images

    Ger - BeginInvoke is kind of the poor man's way of acting ont he main (form) thread from other threads without causing the processor to get *****y about trying to do two things at once. Sounds like you need to try a couple more images and use the same basic logic I provided on the Focus() event. The new Thread/Thread.Start() sets processing in the background, and BeginInvoke goes back to the main thread. Just make sure you put anything you actually do to the form controls inside a method you call with BeginInvoke and you should be golden. Let me know if you get stuck.

  5. #5
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    to mrgr8avill,

    can you give me a start, without giving all the details?
    I need to override the system focus, correct?


    regrads,

    ger

  6. #6
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    @mrgr8avill,

    I managed to change the focus image, with more or less the same code to change the image during the flash.
    Only one thing stays, the system focus dotted line, which I want to get rid off, as I use my own images.
    how to I get rid off the dotted line?

    regards,

    ger

  7. #7
    Join Date
    Apr 2010
    Posts
    131

    Re: Button images

    Ger,

    I'm unable to replicate the problem you're having - could you post your code and I'll see what I can do directly?

  8. #8
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    Hi mrgr8avill,

    open the msvs, and create a dialog/form put serveral buttons in it.
    now run the program.
    press the tab key, you see a dotted line on the button which has the keyboard focus.
    press the tab again, and the dotted line jumps to the next button.
    so if i use a custom image I do not need this dotted line.
    I hope this is clear enough, if not I will post the code.

    regards,

    ger

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Button images

    How will the user know which button has focus? Are you planning on having button images with focus rects?

  10. #10
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    yep,

    I have 3 images, one for neutral, one for keyboard focus and one for the mouse over the button event.
    or
    I can use the neutral image and place a custom rect on it, I do not like the standard dotted line one.

    regards,

    ger
    Last edited by TBBW; September 10th, 2012 at 12:45 AM.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Button images

    Search bing or google for "removing focus rect from a button C#"

  12. #12
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    Hi all,

    I found that I need to use this:


    Code:
    class CustomButton : System.Windows.Forms.Button 
        { 
            protected override bool ShowFocusCues 
            { 
                get 
                { 
                    return false; 
                } 
            } 
    }
    but how do I use this?

    regards,

    ger

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Button images

    You first override a Button class as shown, then you use that button class in your form.

  14. #14
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: Button images

    Hi,

    I do understand the override feature, but my knowledge is very thin, yet...
    I'm sorry but I do know how to implement this.

    This is what I found;

    http://blog.dotnetstep.in/2009/06/re...om-button.html

    It would be nice if I could use the second part of the article of the above mentioned link.
    so I can turn it off/on in the propertie page at design!!

    regards,

    ger

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Button images

    1) Add a new button derived class to your project from the code in the link.
    2) Compile the project
    3) Open the form in designer view
    4) Drag your custom button onto the form
    5) Highlight the button in the designer
    6) In the properties window, set the properties.
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center