PDA

Click to See Complete Forum and Search --> : Starting up Notepad


Jotaf98
Jan 12th, 2001, 05:39 PM
Hi!

I want to make my program start Notepad without the user doing anything (but not instantly, I want it to do it as if the user was doing it!). It's a kind of joke, I can't tell you because if someone tries to trick you with this it won't work :)

I know how to move the cursor smoothly to a location (using the same calculations used to draw lines), but I need the APIs to do it.

At first I wanted it to open up the Start menu, point to Programs » Accessories » Notepad, but I know it would be kinda hard to find the exact positions of all this.

So I thought of another thing: the user can edit the Start menu's shortcut, the shortcuts could be in another language/under another name, sorted in another order or even deleted. So pointing directly at the position where it was suposed to be wouldn't be very efficient.

While writing this (and know that I have re-writen it a dozen times because I'm always having new ideas :p ) I also came to the conclusion that using the mouse would be cool, but it wouldn't be easy (I think) to find all the handles to the objects I wanna point to (mainly submenus). I'll simply send keystrokes instead of moving the cursor *sigh*

So we would find the Start Menu button's handle, give it the focus and send it the Enter key to simulate a click, press the Up Arrow key 3 times to select Run, send the Enter key (to simulate a click again), type "notepad" and send the Enter key again to execute it, wait a couple of seconds and voila, Notepad is open.

Basically I need the API/APIs to find the Start Menu's button and give it focus. Unless you know how to do it using the mouse (by finding submenus' handles)... it would be awsome!

Please, if you have -any- clue on one of these solutions, even if it's just a tiny one, reply here. Thanks!

Jan 13th, 2001, 05:07 PM
This will click the start menu.

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const BM_CLICK = &HF5

Private Sub Command1_Click()
hbtn = FindWindowEx(FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 0, "Button", vbNullString)
PostMessage hbtn, BM_CLICK, 0, 0
End Sub

Jotaf98
Jan 16th, 2001, 11:04 AM
Thanks a lot! Now the code is much easier ;)

If it's not asking too much, do you have the code to find a menu's handle?...

Jan 16th, 2001, 02:19 PM
Sure. Use the GetMenu and GetSubMenu API's.

Private Declare Function GetMenu Lib "user32" Alias "GetMenu" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" Alias "GetSubMenu" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Sub Command1_Click()
Dim hMenu As Long
hMenu = GetMenu(Form1.hWnd)

MsgBox "The hWnd is: " & hMenu
End Sub

Jotaf98
Jan 17th, 2001, 12:38 PM
Again, thanks! Can I find a menu's handle and then use the same API to find the handle of one of its submenus?

Jan 17th, 2001, 02:19 PM
Use the GetSubMenu API to get the submenus.

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Sub Command1_Click()
Dim hMenu As Long. hSubMenu As Long
hMenu = GetMenu(Form1.hwnd)
hSubMenu = GetSubMenu(hMenu, 5) 'Get the SubMenu from position 5

MsgBox "The hWnd is: " & hMenu
End Sub

Jotaf98
Jan 21st, 2001, 06:47 AM
And sorry for asking so many questions ;)