+ Reply to Thread
Results 1 to 12 of 12

VBA Image MouseDown + Form MouseMove

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    VBA Image MouseDown + Form MouseMove

    I'm trying to make an image behave as a button, not just the 'click and do macro' but the look.

    What I have is something like this (not actual code)...
    Declare Clicking=0
    Image.Picture=Pic0
    
    
    UserForm MouseMove Event{
    Image.Picture=Pic0
    }
    
    Image MouseMove Event{
    If Clicking=0 Then Image.Picture=Pic1
    }
    
    Image MouseDown Event{
    Clicking=1
    Image.Picture=Pic2
    }
    
    Image MouseUp Event{
    Clicking=0
    }
    The problem I have is that when I'm clicking (Image MouseDown Activated, Image MouseUp Deactivated) and I move the mouse out of the image, it doesn't trigger the UserForm Mouse Move Event, staying with the pic2 instead of the pic0.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Image MouseDown + Form MouseMove

    Mouse Down & Mouse Up events are a 'matched' pair. The control that receives a Mouse down event will be the control that receives the next Mouse Up event regardless where the mouse pointer actually is.

    You can see this with a standard button. Click (and hold) and the button is depressed, move the mouse off the button then release the mouse. The button receives the Up events and redraws. However, if the mouse is not over the button when released, the Click event is not raised (The Click event is only raised in the MouseUp event). This is the standard way to cancel a button click in Windows. Ever click a button and realise 'Oh [expletive deleted]'?? If you're still holding the button down move the pointer off the button and release.
    Last edited by cytop; 05-11-2016 at 05:08 AM.

  3. #3
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    Quote Originally Posted by cytop View Post
    Mouse Down & Mouse Up events are a 'matched' pair. The control that receives a Mouse down event will be the control that receives the next Mouse Up event regardless where the mouse pointer actually is.

    You can see this with a standard button. Click (and hold) and the button is depressed, move the mouse off the button then release the mouse. The button receives the Up events and redraws. However, if the mouse is not over the button when released, the Click event is not raised (The Click event is only raised in the MouseUp event). This is the standard way to cancel a button click in Windows. Ever click a button and realise 'Oh [expletive deleted]'?? If you're still holding the button down move the pointer off the button and release.

    Also, that code is from Visual Studio, not VBA. No difference here as that behaviour is controlled by Windows, but it is a VBA forum, after all.
    I know that taking the mouse outside the button/image makes it cancel the click, the code is pseudocode (but originally VBA) and I posted the VBA now...
    What I want is when I get the mouse of the button/image (like I was gonna cancel the click but not releasing it), the image to change.

  4. #4
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    EDITED:
    Double post, sorry.
    Last edited by rdrmdr; 05-11-2016 at 05:12 AM.

  5. #5
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    If anyone wanna test it easier just open excel (I'm using 2007), add a UserForm1, a Image1 and insert this code on the UserForm:
    Dim Clicking As Boolean
    Private Sub UserForm_Initialize()
        Clicking = 0
        Image1.BackColor = RGB(150, 0, 0) 'Pic0 / Red
    End Sub
    Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Image1.BackColor = RGB(150, 0, 0) 'Pic0 / Red
    End Sub
    Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If Clicking = 0 Then Image1.BackColor = RGB(200, 0, 0) 'Pic1 / Light Red
    End Sub
    Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Clicking = 1
        Image1.BackColor = RGB(50, 0, 0) 'Pic2 / Dark Red
    End Sub
    Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Clicking = 0
    End Sub
    Last edited by rdrmdr; 05-11-2016 at 05:29 AM.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Image MouseDown + Form MouseMove

    the code is pseudocode
    Yep - realised that after re-reading your original. I just saw the {'s. My post edited.

    Taking the mouse outside the image does not cancel the Click. Becasue you clicked on the Image, that control will receive Mouse events until you release the mouse. The Click event is only raised when you release the Mouse OVER the image.

    I don't understand where you're having a problem. You mouse down on the Image, the displayed pic changes. The Image will receive the next Mouse Up so you don't need to track mouse moves on the userform. Or perhaps I'm misunderstanding

  7. #7
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    Quote Originally Posted by cytop View Post
    Or perhaps I'm misunderstanding
    A little. What I want is this:
    I have the mouse on the form, image = 0
    I have mouse on the image, image = 1
    I have the mouse on the image + I'm pressing but not releasing, image = 2
    I have the mouse on the form but I'm still pressing from the image and moved the mouse to outside the image (to the form), image = 0

    P.S.: I'm gonna edit 1 line on the code in VBA, you don't need the if on UserForm MouseMove Event...

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Image MouseDown + Form MouseMove

    First off, Image controls have very poor performance when it comes to displaying different images - you will be forced to repaint the userform which will cause flickering if a lot of changes happen in a short while.

    You also will have issues trying to determine if the mouse is still over the image as it moves. It's no problem to determine the left and top position of the mouse as the X & Y parameters of the mouse move event will be positive (If negative then you are off the control). You will have issues calculating if the mouse has moved off the right hand edge or the bottom edge and the X & Y co-ordinates are not the same unit of measurement as the control width and height - there's lots of comments all over the place about this, but I basically gave up on that a long time ago.

    I would suggest you will make things a lot easier for yourself if you just use the MouseDown and MouseUp events to flip pictures as a MouseUp event will be raised for the last control to receive a MouseDown.

  9. #9
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    I didn't had to do all of this if the buttons's customizations were more, ..., ?embracing?
    It's like they want us to use there looks and NEVER change.
    Why would you pair the mouse events when some are from moving and others from clicking. Isn't that how drag and move works, I'm not gonna set a timer to be always checking.
    Last edited by rdrmdr; 05-11-2016 at 07:14 AM.

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Image MouseDown + Form MouseMove

    If VBA was a standalone development environment, I'd agree with you, but it's not. It's essentially a freebie offered to extend the capabilities of Excel - I think it's pretty amazing for what it is, warts and all.

    And then there's Visual Studio for when you really want control.

    The way the mouse down & up events work is a function of Windows, not VBA, and there's always the DragOver event for Drag/Drop operations. By the way, the only Timer available in VBA is an API timer, another little restriction.
    Last edited by cytop; 05-11-2016 at 07:38 AM. Reason: Corrected typos from phone - 'another' became 'aborted'!

  11. #11
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Image MouseDown + Form MouseMove

    I know I may look like I'm always going to the point of VS but what I mean is that I don't like the programs's limitations. If the programs were made that way, there wouldn't be necessary to have half of the forum's posts.
    Only posts asking for others to say, it's this, not, you can't do that but you can fake it/do it in another way/APIs/nope, you really can't.
    A perfect digital world .
    When you learn how to program you can program almost in any language, the problem is that the information is never displayed in one place or not in the best way, or doesn't exist at all.
    Last edited by rdrmdr; 05-11-2016 at 07:36 AM.

  12. #12
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Image MouseDown + Form MouseMove

    A perfect ... world
    (of whatever kind)

    May your eventual crash landing be soft and in slow motion... on a very big air bag.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Add MouseMove action to dynamically created form controls
    By AdLoki in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-19-2013, 09:09 AM
  2. Gathering coords from an image and using MouseDown
    By scooteriggy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-27-2013, 11:34 PM
  3. MouseMove & Click event over an image control
    By furbiuzzu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2006, 04:50 AM
  4. Mousedown Macro
    By mabbutt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2006, 03:03 AM
  5. [SOLVED] VBA to display image on a form - image is defined in Range Name
    By Tan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2006, 10:45 AM
  6. MouseDown()-MouseUp()
    By skrol in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-22-2005, 06:01 AM
  7. [SOLVED] Mousedown / Charting
    By Mark Dawson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-01-2005, 04:05 PM
  8. [SOLVED] get reference to cell from mousedown event
    By David in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2005, 09:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1