Page 1 of 1

window api question - DestroyWindow()

Posted: Thu Nov 28, 2024 2:25 pm
by Allen
Hi,

I would like to use runprogram() to open a chrome window and then later close it (not minimize )

Below is my attempt using examples from other experienced member (thanks for their contribution).

The API Closewindow (line 44) work as expected but when I use DestroyWindow (line 43) it failed. Please advise.

Code: Select all

Structure ChromeWin_Structure
  FullTitle$
  WinTitlePart$
  TitleFrontPart$
  hWnd.i
EndStructure
Global ChromeWin.ChromeWin_Structure

Procedure.i ChromeWinsCallback(hWnd.i, *lParam.ChromeWin_Structure)
  Protected *Buffer, Result
  Result = #True
  If IsWindowVisible_(hWnd)
    *Buffer = AllocateMemory(1024)
    If *Buffer
      If GetWindowText_(hWnd, *Buffer, MemorySize(*Buffer))
        *lParam\FullTitle$ = PeekS(*Buffer)
        If *lParam\FullTitle$ <> ""
          If *lParam <> #Null
            If *lParam\WinTitlePart$ <> ""
              If FindString(*lParam\FullTitle$, *lparam\WinTitlePart$)
                *lParam\hWnd = hWnd
                Debug hWnd
                Result = #False
              EndIf
            EndIf
          EndIf
        EndIf
      EndIf
      FreeMemory(*Buffer)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure

Procedure.i FindVisibleWindowByTitlePart(WinTitlePart$)
  ChromeWin\hWnd = 0
  ChromeWin\WinTitlePart$ = WinTitlePart$
  EnumWindows_(@ChromeWinsCallback(), @ChromeWin)
EndProcedure

Procedure CloseChrome()
  If IsWindow_(ChromeWin\hWnd)
    ;If DestroyWindow_(ChromeWin\hWnd)
    If CloseWindow_(ChromeWin\hWnd)
      Debug "OK"
    Else
      Debug "Fail"
    EndIf
  EndIf
EndProcedure

RunProgram("Chrome.exe")
Delay(7000)
FindVisibleWindowByTitlePart("Google Chrome") 
Delay(1000)
CloseChrome()
Thanks

Allen

Re: window api question - DestroyWindow()

Posted: Thu Nov 28, 2024 3:28 pm
by Axolotl
You should try instead of CloseWindow_()
not tested, but I think this is the better choice.

Code: Select all

SendMessage_(ChromeWin\hWnd, #WM_CLOSE, 0, 0) 

BTW: Maybe you do not know that WebViewGadget() and WebGadget() with parameter #PB_Web_Edge are also using the chrome stuff under the hood.

Re: window api question - DestroyWindow()

Posted: Thu Nov 28, 2024 5:11 pm
by Allen
Axolotl,

It works, thanks the help.

I did test use the webgadget (PB built-in) to replace the use of chrome, however it seems the HTML code return from - GetGadgetItemText() with #PB_Web_HtmlCode flag did not encode chinese char properly . Web page display correctly, but content of HTML does not. I did not know how to manually set the char encoding. A brief searching the forum did not find the information I need. I appreciate if you can give some advise/direction on this.

Thanks.

Allen

Re: window api question - DestroyWindow()

Posted: Fri Nov 29, 2024 10:47 am
by Axolotl
Hi Allen
unfortunately, I can't help you with that, it's not my area of expertise (does it even exist? :oops: )

Re: window api question - DestroyWindow()

Posted: Sat Nov 30, 2024 1:54 pm
by Allen
Axolotl,

No worries. I did learned a lot from your advices (and from other members). Thank you again for your help all along.

Allen