window api question - DestroyWindow()

Just starting out? Need help? Post your questions and find answers here.
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

window api question - DestroyWindow()

Post 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
Axolotl
Enthusiast
Enthusiast
Posts: 798
Joined: Wed Dec 31, 2008 3:36 pm

Re: window api question - DestroyWindow()

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Re: window api question - DestroyWindow()

Post 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
Axolotl
Enthusiast
Enthusiast
Posts: 798
Joined: Wed Dec 31, 2008 3:36 pm

Re: window api question - DestroyWindow()

Post by Axolotl »

Hi Allen
unfortunately, I can't help you with that, it's not my area of expertise (does it even exist? :oops: )
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Re: window api question - DestroyWindow()

Post 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
Post Reply