Occupy Window from External Process, but Menu Bar is Missing

Just starting out? Need help? Post your questions and find answers here.
User avatar
Stefan Schnell
User
User
Posts: 85
Joined: Wed May 07, 2003 2:53 pm
Location: Germany - Oberirsen
Contact:

Occupy Window from External Process, but Menu Bar is Missing

Post by Stefan Schnell »

Hello community,

I am using the following code to occupy windows from external processes. It works well but the menu bars of the windows are missing.

Code: Select all

; Begin-----------------------------------------------------------------

  ; Directives----------------------------------------------------------
    EnableExplicit


  ; Enumeration---------------------------------------------------------
    Enumeration
      #MainWin
      #MDI
    EndEnumeration


  ; Variables-----------------------------------------------------------
    Global Notepad.i, hNotepad.i
    Global Calc.i, hCalc.i


  ; Function MyWindowCallback-------------------------------------------
    Procedure WinCallback(hWin, uMsg, wParam, lParam)

      ; Variables-------------------------------------------------------
        Protected Result.i

      Result = #PB_ProcessPureBasicEvents

      Select uMsg
        Case #WM_PAINT
          UpdateWindow_(hNotepad)
          UpdateWindow_(hCalc)
      EndSelect

      ProcedureReturn Result
    EndProcedure


  ; Function CaptureWindowAsChild---------------------------------------
    Procedure CaptureWindowAsChild(Program.i)

      ; Variables-------------------------------------------------------
        Protected hWindow.i
        Protected Quit.b = #False
        Protected PID.i
        Protected PIDProgram.i
        Protected hProgramWin.i

      PIDProgram = ProgramID(Program)

      Repeat
        hWindow = FindWindow_(0, 0)
        While hWindow <> 0 And Quit = #False
          GetWindowThreadProcessId_(hWindow, @PID)
          If PID = PIDProgram And GetParent_(hWindow) = 0 And
            IsWindowVisible_(hWindow)
            hProgramWin = hWindow
            Quit = #True
          EndIf 
          hWindow = GetWindow_(hWindow, #GW_HWNDNEXT)
        Wend 
      Until hProgramWin

      SetWindowLong_(hProgramWin, #GWL_STYLE, 
        GetWindowLong_(hProgramWin, #GWL_STYLE) | #WS_CHILD)
      SetParent_(hProgramWin, GadgetID(#MDI))

      UpdateWindow_(hProgramWin)

      ProcedureReturn hProgramWin
    EndProcedure


  ; Function Init-------------------------------------------------------
    Procedure.b Init()

      If OpenWindow(#MainWin, 10, 10, 1024, 768, "SetParent", 
        #PB_Window_SizeGadget | #PB_Window_SystemMenu)

        SetWindowCallback(@WinCallback())
 
        MDIGadget(#MDI, 0, 0, 0, 0, 0, 0, #PB_MDI_AutoSize)
  
        Notepad = RunProgram("notepad.exe", "", "", #PB_Program_Open)
        hNotePad = CaptureWindowAsChild(Notepad)

        Calc = RunProgram("calc.exe", "", "", #PB_Program_Open)
        hCalc = CaptureWindowAsChild(Calc)

        ProcedureReturn #True
      EndIf

    EndProcedure


  ; Sub Main------------------------------------------------------------
    Procedure Main()

      ; Variables-------------------------------------------------------
        Protected Quit.b = #False
        Protected Event.i

      Repeat

        Event = WaitWindowEvent()
        Select Event
          Case #PB_Event_CloseWindow
            Quit = #True
        EndSelect

      Until Quit

    EndProcedure


  ; Sub Done------------------------------------------------------------
    Procedure Done()

      If IsProgram(Notepad)
        KillProgram(Notepad)
        CloseProgram(Notepad)
      EndIf

      If IsProgram(Calc)
        KillProgram(Calc)
        CloseProgram(Calc)
      EndIf

      If IsWindow(#MainWin)
        CloseWindow(#MainWin)
      EndIf

    EndProcedure


  ; Main----------------------------------------------------------------
    If Init()
      Main()
      Done()
    EndIf

; End-------------------------------------------------------------------
Image

Where is my mistake?

Thanks for hints and tips.

Cheers
Stefan
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Occupy Window from External Process, but Menu Bar is Mis

Post by netmaestro »

Your mistake comes very early, it is in your approach to the task. You make the window a child and no child window can host a menu. Only toplevel windows can have menus. You will have to come up with a new approach if you want the menu included.
BERESHEIT
User avatar
Stefan Schnell
User
User
Posts: 85
Joined: Wed May 07, 2003 2:53 pm
Location: Germany - Oberirsen
Contact:

Re: Occupy Window from External Process, but Menu Bar is Mis

Post by Stefan Schnell »

Hello netmaestro,

thanks for this information, it solved my problem.
:D

Cheers
Stefan
Post Reply