Results 1 to 8 of 8

Thread: [Delphi]How do I access menu commands in one program from a Delphi Form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    4

    [Delphi]How do I access menu commands in one program from a Delphi Form?

    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.
    Last edited by NoteMe; Jul 29th, 2005 at 09:56 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I access menu commands in one program from a Delphi Form?

    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 forum section. One of them may provide an answer to your question.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: How do I access menu commands in one program from a Delphi Form?

    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)...


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: How do I access menu commands in one program from a Delphi Form?

    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

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: How do I access menu commands in one program from a Delphi Form?

    Does this help? If you change it to Delphi somehow (I can't)...

    VB Code:
    1. 'Example by Daniel Kaufmann ([email protected])
    2.  
    3. 'Paste this code in a Form
    4. 'with a Menu named menu1 which has a menuitem named menu2
    5.  
    6. Private Type POINTAPI
    7.     x As Long
    8.     y As Long
    9. End Type
    10. Private Type RECT
    11.     Left As Long
    12.     Top As Long
    13.     Right As Long
    14.     Bottom As Long
    15. End Type
    16.  
    17. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    18. Private Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As Long
    19. Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    20.  
    21. Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
    22. Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    23. Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    24. Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
    25. Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
    26. Private Const MOUSEEVENTF_MIDDLEUP = &H40
    27. Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    28. Private Const MOUSEEVENTF_RIGHTUP = &H10
    29.  
    30. 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)
    31. Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
    32. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    33. Const SM_CXSCREEN = 0 'X Size of screen
    34. Const SM_CYSCREEN = 1 'Y Size of Screen
    35.  
    36. Private Sub Form_KeyPress(KeyAscii As Integer)
    37.     Dim mWnd As Long
    38.     mWnd = Me.hwnd
    39.    
    40.     Dim hMenu As Long, hSubMenu As Long
    41.  
    42.     hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
    43.     ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
    44.     hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
    45.     ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
    46.    
    47. End Sub
    48.  
    49.  
    50. Private Sub ScreenToAbsolute(lpPoint As POINTAPI)
    51. lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))
    52. lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
    53. End Sub
    54.  
    55. Private Sub Click(p As POINTAPI)
    56. 'p.X and p.Y in absolute coordinates
    57. 'Put the mouse on the point
    58.  
    59. mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, GetMessageExtraInfo()
    60. 'Mouse Down
    61. mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()
    62. 'Mouse Up
    63. mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()
    64. End Sub
    65.  
    66. Private Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos As Long)
    67. Dim ret As Long
    68. Dim r As RECT, p As POINTAPI
    69. ret = GetMenuItemRect(mWnd, hMenu, Pos, r)
    70. If ret = 0 Then Exit Sub
    71. p.x = (r.Left + r.Right) / 2
    72. p.y = (r.Top + r.Bottom) / 2
    73. ScreenToAbsolute p
    74. 'Click on p
    75. Click p
    76. End Sub
    77.  
    78. Private Sub Form_Load()
    79. Dim mWnd As Long, p As POINTAPI
    80. mWnd = Me.hwnd
    81. Dim hMenu As Long, hSubMenu As Long
    82. hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
    83. ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
    84. hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
    85. ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
    86. p.x = &HFFFF& / 2
    87. p.y = &HFFFF& / 2
    88. Click p
    89. Me.AutoRedraw = True
    90. Me.BackColor = vbWhite
    91. Print "Press any key"
    92. End Sub
    93.  
    94. Private Sub menu2_Click()
    95. MsgBox "Click"
    96. End Sub
    Last edited by manavo11; Jul 20th, 2005 at 08:15 AM.


    Has someone helped you? Then you can Rate their helpful post.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    4

    Re: How do I access menu commands in one program from a Delphi Form?

    Thanks MadBoy, I'll await your further reply. Yes you have a good handle on my goal.

  7. #7
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: How do I access menu commands in one program from a Delphi Form?

    Quote Originally Posted by manavo11
    Does this help? If you change it to Delphi somehow (I can't)...
    I thought you could hack Delphi

    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?

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: How do I access menu commands in one program from a Delphi Form?

    Moved to Other Programming Languages that covers Delphi questions


    Has someone helped you? Then you can Rate their helpful post.

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