Page 1 of 1

PB creates the class name for the window [Win Only]

Posted: Fri Jul 25, 2025 3:50 pm
by Axolotl
Maybe this is already common knowledge or just a coincidence....
I found out today that the main window of pb always uses the number to create the class name.
No matter whether you use a constant or #PB_Any (I know, #PB_Any is a constant as well)
Result (for me): I can use a better version of FindWindowEx() to find my already running main window instance...

Code: Select all

#ProgramName$         = "TestApp" 
#ProgramMainCaption$  = "Test App " + #PB_Editor_BuildCount+"."+#PB_Editor_CompileCount 

#WND_Main         = 123   ; Enumerations are recommended :) 
#GDT_txtClassname =   1 
#GDT_txtWindow    =   2 


Procedure CreateMainWindow(Window) 
  Protected hwnd, hwndFound, cn${#MAX_PATH} 

  hwnd = OpenWindow(Window, 0, 0, 320, 80, #ProgramMainCaption$, #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  If hwnd 
    If Window = #PB_Any 
      Window = hwnd         ; Number 
      hwnd = WindowID(hwnd) ; Handle 
    EndIf 
    StringGadget(#GDT_txtClassname, 8,  8, 304, 20, "") 
    StringGadget(#GDT_txtWindow,    8, 32, 304, 20, "") 

    If GetClassName_(hwnd, @cn$, #MAX_PATH) 
      SetGadgetText(#GDT_txtClassname, "Classname" + #TAB$ + cn$) 
    EndIf 

    hwndFound = FindWindowEx_(#Null, #Null, "WindowClass_"+Window, #ProgramMainCaption$) 
    If hwndFound = hwnd 
      SetGadgetText(#GDT_txtWindow, "Window #" + Window + " " + #TAB$ + "found") 
    EndIf 

    ProcedureReturn #True 
  EndIf 
  ProcedureReturn #False 
EndProcedure 

Procedure Main() 
CompilerIf 0  ; <- change this to 1 or 10 and see the difference 
  If CreateMainWindow(#WND_Main) 
CompilerElse 
  If CreateMainWindow(#PB_Any) 
CompilerEndIf 
    Repeat 
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow 
          Break 
      EndSelect 
    ForEver 
  EndIf 
  ProcedureReturn 0
EndProcedure 

End Main() 

Re: PB creates the class name for the window [Win Only]

Posted: Sat Jul 26, 2025 4:25 pm
by Caronte3D
Useful, thanks for sharing! :wink:

Re: PB creates the class name for the window [Win Only]

Posted: Sun Jul 27, 2025 1:50 pm
by Quin
Thanks for this, you learn something new every day ;)

Re: PB creates the class name for the window [Win Only]

Posted: Mon Jul 28, 2025 10:38 am
by Axolotl
Thanks guys,

please keep in mind, that if you want to use it to search for the first application instance, you have to use the constant (e.g. #WND_Main). Dynamically, a new number is generated each time and was only intended as an illustration.