Results 1 to 28 of 28

Thread: [RESOLVED] How to trigger the desktop context menu...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Resolved [RESOLVED] How to trigger the desktop context menu...

    I am wondering how to trigger the desktop context menu. If I right-click on my desktop, a context menu appears. I would like to right-click on an object on my form and trigger that same menu. How do I achieve this?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to trigger the desktop context menu...

    What are you asking for?

    It sounds like you want a right-click on your own program to open Explorer's desktop context menu. This action is normally meant to open a context menu for your program. What you seem to be asking for doesn't make sense.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to trigger the desktop context menu...

    Is this the popup you want?

    If so, it is related to the Desktop, not one of your controls on the form.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to trigger the desktop context menu...

    I DO know you can click the Shift & F10 keys to emulate a right-mouse event, but not sure how do do that using Sendkeys in VB6 'FROM THE DESKTOP'.
    Maybe an expert can tell you.
    EDIT: The syntax for Sendkeys for VB6 for the Shift and F10 keys combination is: Sendkeys "+{F10}", True
    However, your app must be running as admin or you turn off UAC in OS's that use that. Still, to get it to be FROM THE DESKTOP, good luck.
    Last edited by SamOscarBrown; Dec 6th, 2013 at 08:29 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    Sam, yes... that menu is exactly what I am talking about.

    Dil, yes... that is exactly what I want to do.

    This should clear it up: I want to be able to right-click on my own form and pop up the EXACT SAME menu that pops up when I right-click on my desktop. An "integration", if you will.

    Here's what it should look like:
    Name:  sample.jpg
Views: 6447
Size:  31.9 KB

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to trigger the desktop context menu...

    Is this meant to be some sort of "screw up how Windows works as a 'joke' program" or what?

    It's a bit like asking how to make the computer play some random MP3 whenever the user minimizes a window or something. Why would you need this?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    This is not meant to be a 'joke' program. It is something that I am needing for quite a serious application I am working on and am extremely serious about. Your "why would you need this" question is irrelevant. This is a forum for asking questions. Either you have an answer or you don't. If you do, please provide either the code or where I might find it. If you don't, maybe refrain from replying.

    Sam, it's possible to create a menu, adding the following subitems to it and using the Shell event to run any of these:

    Code:
    'Screen Saver Settings
    Shell "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1"
    
    'Personalization
    Shell "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2"
    
    Screen Resolution
    Shell "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3"
    
    Window Color and Appearance
    Shell "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,5"
    However, I would rather just trigger the original menu instead of making a generic one.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to trigger the desktop context menu...

    Your "why would you need this" question is irrelevant.
    No.... not always... sometimes it is QUITE relevant... because sometimes the original question is the wrong one... or sometimes the poster gets it in their head about needing to to it in a particular way, when there is a better way... or they're tilting at windmills. I NEVER blindly accept requirements from a client... same thing applies here... in this case though, it's not a common request, so it seemingly comes out of left people, and people are left going "well now why in the heck would you want to do that?"

    This is a forum for asking questions.
    Questions go both ways.

    Either you have an answer or you don't.
    Yeah, I don't think I do... other than to say that what I think you're trying to do is counter to how Windows works - I'm not talking about user experience and what they would expect (although I'd argue that they may not expect the Desktop context menu to open when right-clicking on your app)... but rather how Windows itself works... it's all about the context... when you right-click on your form, the context is your form... NOT the desktop... and I don't think you can override that, not without seriously compromising Windows (ie, ALL forms would then experience the same thing, not just yours) or maybe getting into the byte code and re-wiring it yourslef...

    If you do, please provide either the code or where I might find it. If you don't, maybe refrain from replying.
    Ooops... my bad... guess I shouldn't have replied...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    Thanks for the non-answer, tg. Way to contribute.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    Back to the topic... if anyone has an answer, I'd really like to know. Thanks in advance.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    *bump*

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to trigger the desktop context menu...

    I'm still dubious that it has any value, but why not just handle the MouseUp event of your Form and then use GetDesktopWindow() to get the desktop's hWnd and then call SendMessage to send a right-click message to that hWnd?

    Sure, you'll need to translate the coordinates from Form-relative X and Y to Desktop coordinates. But aside from that it seems easy enough. Of course other controls may capture the mouse-click too, so ultimately you may have to subclass the Form if you want to capture 100% of these actions.

    Of course I have never heard of anyone needing it, can't imagine a reason to do so myself, and so I won't claim it works without actually trying it.

    Or is there something else you wanted to do?

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to trigger the desktop context menu...

    Did you attempt using Sendkeys in any way...? Maybe in conjuction with GetDesktopWindow()????
    Other than that suggestion, I can't assist any more.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    No, that's pretty much it. As to having never heard of anyone needing it, Sam here needed it 4 years ago and received zero responses. I'm looking for the same thing. If I find it, I'll post it here.

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to trigger the desktop context menu...

    Not me (Sam)....been on this Forum only just over 15 months...musta been another Sam.

  16. #16
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to trigger the desktop context menu...

    Something like this little horror maybe;

    Code:
    Private Declare Function GetShellWindow Lib "user32.dll" () As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Sub Command1_Click()
    SetForegroundWindow GetShellWindow
    CreateObject("WScript.Shell").SendKeys "+{F10}", True
    
    End Sub

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: How to trigger the desktop context menu...

    Wow... that was easy. Thank you so much Magic. Can't believe people said it couldn't be done. I knew better, just couldn't figure it out. A thousand props to you.

  18. #18
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] How to trigger the desktop context menu...

    Not that easy actually, there are a couple of flaws;
    1. The menu always drops at the Desktop's extreme top left.
    2. If an icon on the Desktop has pre-existing focus the context menu for that is dropped rather than the one for the Desktop.

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] How to trigger the desktop context menu...

    Yes, I noticed simply using the Sendkey combination, it always appeared top left.....good job Magic

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: [RESOLVED] How to trigger the desktop context menu...

    So, there's no way to change the coordinates of the menu?

  21. #21
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] How to trigger the desktop context menu...

    Oh yes...
    Code:
    Private Declare Function GetShellWindow Lib "user32.dll" () As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dY As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Const RIGHTDOWN = &H8, RIGHTUP = &H10
    
    Private Sub Command1_Click()
    
        SetForegroundWindow GetShellWindow
        SetCursorPos Screen.Width / Screen.TwipsPerPixelX / 2, Screen.Height / Screen.TwipsPerPixelY / 2  'eg. Centre of screen
        mouse_event RIGHTDOWN, 0&, 0&, 0&, 0&
        mouse_event RIGHTUP, 0&, 0&, 0&, 0&
    
    End Sub
    Still not perfect though; if there is an intervening icon or window you get the context menu for that (if any).

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: [RESOLVED] How to trigger the desktop context menu...

    Yeah, that doesn't work. I was wanting the menu to appear wherever the mouse is, as opposed to the top-left corner. Come on, Magic... do your... erm... magic.

  23. #23
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] How to trigger the desktop context menu...

    Ok but this is now getting really silly and this is my last silly on it...

    Code:
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Declare Function GetShellWindow Lib "user32.dll" () As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpoint As POINTAPI) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dY As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Const RIGHTDOWN = &H8, RIGHTUP = &H10
    
    Private Sub Command1_Click()
    
        Dim p As POINTAPI
    
        SetForegroundWindow GetShellWindow
        GetCursorPos p
        SetCursorPos p.x, p.y
        mouse_event RIGHTDOWN, 0&, 0&, 0&, 0&
        mouse_event RIGHTUP, 0&, 0&, 0&, 0&
    
    End Sub
    
    Private Sub Form_Load()
        Timer1.Interval = 2000
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Command1.Value = True
    End Sub
    You do not have to click the button; the Timer does the work
    Last edited by Magic Ink; Dec 8th, 2013 at 03:34 PM. Reason: extra info

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: [RESOLVED] How to trigger the desktop context menu...

    lol. Okay, Magic. But I do have a question for you pertaining to your original code which probably won't require any code-posting on your part. On the line:

    CreateObject("WScript.Shell").SendKeys "+{F10}", True

    ...is there a reference sheet somewhere that will tell me what else I can put on the right side of the CreateObject("WScript.Shell"). besides SendKeys?

  25. #25
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] How to trigger the desktop context menu...

    ...is there a reference sheet somewhere
    http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx

    The reason I used it above was that it enables you to use Sendkeys in the VB6 IDE, as you may of noted before the plain Sendkeys will not work there (after Windows XP); however it does work just fine in a vb6 exe.

    How did you get on with the code in post 23?

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: [RESOLVED] How to trigger the desktop context menu...

    The link you provided was kind of a blanket reference, as I have no idea where you found that you could use SendKeys afterward or what else you could use (because you definitely can't use MsgBox... I tried). Lots of links there but no "gps" as to where I can find specific things that can be used.

    As to post 23, it didn't really work for what I needed. Post 16 was the absolute best and works perfectly for my program. If I could change the position of the context menu using the code in post 16, that would be the icing on the cake, and a nice feature to my already finished program, which you can pick up here: http://www.udontknowtech.com

  27. #27
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] How to trigger the desktop context menu...

    Quote Originally Posted by Conroy Vanderbluff View Post
    The link you provided was kind of a blanket reference, as I have no idea where you found that you could use SendKeys afterward or what else you could use (because you definitely can't use MsgBox... I tried). Lots of links there but no "gps" as to where I can find specific things that can be used.
    CreateObject("WScript.Shell") returns a WshShell object which has these properties and methods.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  28. #28
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] How to trigger the desktop context menu...

    Apologies this is the required reference http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width