-
Dec 5th, 2013, 07:26 PM
#1
Thread Starter
Addicted Member
[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?
-
Dec 5th, 2013, 09:47 PM
#2
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.
-
Dec 6th, 2013, 07:45 AM
#3
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.
-
Dec 6th, 2013, 08:22 AM
#4
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.
-
Dec 6th, 2013, 09:45 AM
#5
Thread Starter
Addicted Member
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:
-
Dec 6th, 2013, 09:54 AM
#6
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?
-
Dec 6th, 2013, 10:13 AM
#7
Thread Starter
Addicted Member
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.
-
Dec 6th, 2013, 10:24 AM
#8
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
-
Dec 6th, 2013, 11:47 AM
#9
Thread Starter
Addicted Member
Re: How to trigger the desktop context menu...
Thanks for the non-answer, tg. Way to contribute.
-
Dec 6th, 2013, 11:49 AM
#10
Thread Starter
Addicted Member
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.
-
Dec 6th, 2013, 12:01 PM
#11
Thread Starter
Addicted Member
Re: How to trigger the desktop context menu...
-
Dec 6th, 2013, 12:10 PM
#12
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?
-
Dec 6th, 2013, 01:12 PM
#13
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.
-
Dec 6th, 2013, 01:15 PM
#14
Thread Starter
Addicted Member
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.
-
Dec 6th, 2013, 01:54 PM
#15
Re: How to trigger the desktop context menu...
Not me (Sam)....been on this Forum only just over 15 months...musta been another Sam.
-
Dec 7th, 2013, 03:55 AM
#16
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
-
Dec 7th, 2013, 04:53 PM
#17
Thread Starter
Addicted Member
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.
-
Dec 8th, 2013, 05:29 AM
#18
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.
-
Dec 8th, 2013, 09:01 AM
#19
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
-
Dec 8th, 2013, 11:20 AM
#20
Thread Starter
Addicted Member
Re: [RESOLVED] How to trigger the desktop context menu...
So, there's no way to change the coordinates of the menu?
-
Dec 8th, 2013, 12:00 PM
#21
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).
-
Dec 8th, 2013, 02:56 PM
#22
Thread Starter
Addicted Member
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.
-
Dec 8th, 2013, 03:26 PM
#23
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
-
Dec 8th, 2013, 08:04 PM
#24
Thread Starter
Addicted Member
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?
-
Dec 9th, 2013, 04:30 AM
#25
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?
-
Dec 9th, 2013, 08:48 AM
#26
Thread Starter
Addicted Member
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
-
Dec 9th, 2013, 09:34 AM
#27
Re: [RESOLVED] How to trigger the desktop context menu...
Originally Posted by Conroy Vanderbluff
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)
-
Dec 9th, 2013, 09:50 AM
#28
Re: [RESOLVED] How to trigger the desktop context menu...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|