[Delphi]How do I access menu commands in one program from a Delphi Form?
7 answers - 534 bytes -

I am a neophyte programmer. I have figured out how to generate a simple Delphi 7 form with a button in it which will run an executable.
procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(Form1.Handle, 'open', 'c:\E3238s\bin\E3238s.exe',
nil, nil, SW_SHOWNORMAL);
end;
end.
My problem is I need to know how to access the menus that are a part of this executable. For example how would I access the FILE menu in order to select EXIT.
No.1 | | 427 bytes |
| 
Welcome to the forums. :)
I have no idea what the answer to your question is as I have less than zero experience with Delphi, but, we have a very active member that is into Delphi, and has posted all sorts of Delphi routines in out CodeBank.
Take a look at what MadBoy has posted in this (http://www.vbforums.com/forumdisplay.php?f=48) forum section. One of them may provide an answer to your question.
No.2 | | 243 bytes |
| 
Instead of selecting the menu to close down the form, you can probably send the WM_CLOSE message or something similar to the other window... Although it might not be exactly the same cause you won't have the unload events run (I think)...
No.3 | | 480 bytes |
| 
well i think there is a SendMessage API which can be used to get the handle ID of the menu. I had the code somewhere but i no longer posess it. If you can be pacient ill try and dig it up or re-write a method for this.
Just to clarify, you basically want to execute a menu from your app, via the menu extension of another app?
Example: In your app, you could click the button and it would execute Help, About in Notepad?
Please be clear if i am wrong :)
No.4 | | 3722 bytes |
| 
Does this help? If you change it to Delphi somehow (I can't)...
'Example by Daniel Kaufmann (daniel@i.com.uy)
'Paste this code in a Form
'with a Menu named menu1 which has a menuitem named menu2
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
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)
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0 'X Size of screen
Const SM_CYSCREEN = 1 'Y Size of Screen
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim mWnd As Long
mWnd = Me.hwnd
Dim hMenu As Long, hSubMenu As Long
hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
End Sub
Private Sub ScreenToAbsolute(lpPoint As POINTAPI)
lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))
lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
End Sub
Private Sub Click(p As POINTAPI)
'p.X and p.Y in absolute coordinates
'Put the mouse on the point
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, GetMessageExtraInfo()
'Mouse Down
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()
'Mouse Up
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()
End Sub
Private Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos As Long)
Dim ret As Long
Dim r As RECT, p As POINTAPI
ret = GetMenuItemRect(mWnd, hMenu, Pos, r)
If ret = 0 Then Exit Sub
p.x = (r.Left + r.Right) / 2
p.y = (r.Top + r.Bottom) / 2
ScreenToAbsolute p
'Click on p
Click p
End Sub
Private Sub Form_Load()
Dim mWnd As Long, p As POINTAPI
mWnd = Me.hwnd
Dim hMenu As Long, hSubMenu As Long
hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
p.x = &HFFFF& / 2
p.y = &HFFFF& / 2
Click p
Me.AutoRedraw = True
Me.BackColor = vbWhite
Print "Press any key"
End Sub
Private Sub menu2_Click()
MsgBox "Click"
End Sub
No.5 | | 90 bytes |
| 
Thanks MadBoy, I'll await your further reply. Yes you have a good handle on my goal.
No.6 | | 257 bytes |
| 
Does this help? If you change it to Delphi somehow (I can't)...
I thought you could hack Delphi :p
Neo, i have been pretty busy lately but if i get some spare time ill get round to it, have you tried searching it through Google? :ehh:
No.7 | | 68 bytes |
| 
Moved to Other Programming Languages that covers Delphi questions :)