Control Menus of other apps (works now)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by wayne1.

;this is an example how to control the menus of
;other applications the program itself serves no purpose other than
;to show how to do it. when this program starts up it will
;open notepad and list all the menus and their handles
;when you select an item in the list box and click send message
;it will operate the menu of notepad, you can try other apps
;by entering the window title and clicking analyse menu
;the window title must be exactly as shown except case doesnt matter
;EXAMPLE open Windows calculator type in calculator and click analyse menu the items
;will then be listed NOTE changing the view(standard/scientific) will
;change the handle so you need to reanalyse the menu to get new handle
;manually resizing has no affect
;##################################################
Procedure Msg(message.s)

messagerequester("Message",message,0)

EndProcedure
;####################################################
Procedure.s Trim(string.s)

string=StripLead(string)
string=StripTrail(string)

ProcedureReturn string

EndProcedure
;######################################################
Procedure.s String(Data.s,howMany.w)
stringTemp.s
For i = 0 To howMany - 1
stringTemp=stringTemp + Data
Next

ProcedureReturn stringTemp

EndProcedure
;########################################################
;globals for GetMenus()
Global hnd$
Global regMenu.b
Global curWin.w
Global menustring2.s,Spaces20.s
Spaces20=String(" ",20)
menustring2=String(" ",38)


Procedure GetMenus(menuhnd.l)

; This routine can analyze up to 32 popup sub-menus
MF_BYPOSITION = 1024
MF_BYCOMMAND = 0
WM_COMMAND=273

Dim trackpopups(32)

slPopupMenu = 0

;Find out how many entries are in the menu.
slNumberOfMenus = GetMenuItemCount_(menuhnd)

For slMenu = 0 To slNumberOfMenus - 1
;Get the ID for this menu
;It's a command ID, -1 for a popup, 0 for a seperator
slMenuID = GetMenuItemID_(menuhnd, slMenu)
Select slMenuID
Case 0 ;It's a seperator
AddGadgetItem(1, -1,String("-",30)+String(" ",25) + hnd$+String(" ",4)+ str(regMenu-1))
regMenu=regMenu+1

Case -1 ;It's a popup menu
; Save it in the list of popups
trackpopups(slPopupMenu) = slMenu
slPopupMenu = slPopupMenu + 1
regMenu=1
slMenuInfo = GetMenuString_(menuhnd, slMenu,menustring2, 127, MF_BYPOSITION)
slMenuFlags = GetMenuState_(menuhnd, slMenu,MF_BYPOSITION)
AddGadgetItem(1,-1,Left(menustring2, slMenuInfo) +Spaces20+ str(GetSubMenu_(menuhnd, slMenu))+" -1")
hnd$= str(GetSubMenu_(menuhnd, slMenu))

;At least one popup was found
If slPopupMenu > 0
For slXcnt = 0 To slPopupMenu - 1
slMenuID = trackpopups(slXcnt)

GetMenus(GetSubMenu_(menuhnd, slMenuID));call procedure again


Next slXcnt
slPopupMenu = 0

EndIf

Default ;A regular entry
slMenuInfo = GetMenuString_(menuhnd, slMenuID,menustring2, 127, MF_BYCOMMAND)
slMenuFlags = GetMenuState_(menuhnd, slMenuID,MF_BYCOMMAND)

AddGadgetItem(1, -1,String(" ",3)+Trim(menustring2)+ String(" ",25) + hnd$+String(" ",4)+str(regMenu-1))
regMenu=regMenu+1



EndSelect
Next slMenu

SetGadgetState(1,2)
EndProcedure

;#####################################################
;BEGIN PROGRAM


If OpenWindow(0, 30, 85, 395, 360, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "Analyse Menu")


initgadget(5)
CreateGadgetList(WindowID())
ListViewGadget(1,30,30,330,170)
ButtonGadget(2, 80, 260, 105, 25, "Send Message")
ButtonGadget(3, 220, 260, 105, 25, "Analyse New Menu")
StringGadget(4, 40, 225, 305, 25, "Untitled - Notepad")
TextGadget(5, 10, 200, 405, 25, String(" ",13)+"Enter window title EXACTLY as it appears (Case is unimportant)")


RunProgram("notepad.exe","",0)
delay(500)
curWin=FindWindow_(0,"Untitled - Notepad")
menuhnd = GetMenu_(curWin)
GetMenus(menuhnd)
t= FindWindow_(0, "Analyse Menu")
;Msg(str(t))
i= SetWindowPos_(curWin,-1,430,85,300 ,300,0 );Set notepad location\size
;Msg(str(i))
i= SetWindowPos_(t,-1,30,85,395 ,360,0 );set analyse menu location\size
SetGadgetText(5,String(" ",13)+"Enter window title EXACTLY as it appears (Case is unimportant)")
;Msg(str(i))




Repeat
EventID.l = WaitWindowEvent()

If EventID = #PB_EventGadget

Select EventGadgetID()
Case 2
handle$=right(GetGadgetText(1),15)
handle$=Left(handle$,11)
handle$=Trim(handle$)
;Msg(handle$)
index$=Right(GetGadgetText(1),2)
index$=Trim(index$)
;Msg(index$)
slMenuID = GetMenuItemID_(val(handle$), val(index$))
;Msg(str(slMenuID))
;Msg(str(curWin))
SendMessage_(curWin, 273, slMenuID, 0);WM_COMMAND=273

Case 3

curWin=FindWindow_(0,GetGadgetText(4))

If curWin0
ClearGadgetItemList(1)
menuhnd = GetMenu_(curWin)
GetMenus(menuhnd)
delay(1500)
t= FindWindow_(0, "Analyse Menu")

Else
Msg("Failed, App not running or title is incorrect")
EndIf

EndSelect

EndIf

Until EventID = #PB_EventCloseWindow
curWin=FindWindow_(0,"Untitled - Notepad")

PostMessage_(curWin,31, 0, 0);WS_CANCELMODE
;kill Untitled note-pad if exists
PostMessage_(curWin,16 , 0, 0);WM_CLOSE

EndIf


End




Edited by - wayne1 on 10 August 2001 03:12:50