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

Share your advanced PureBasic knowledge/code with the community.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

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

Post 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() 
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).
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

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

Post by Caronte3D »

Useful, thanks for sharing! :wink:
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post by Quin »

Thanks for this, you learn something new every day ;)
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

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

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