Page 1 of 2

How to set the WindowClass name?

Posted: Wed Jul 11, 2007 8:20 pm
by Mistrel
I would like to specify the class name of the window made by Visual Designer. I would like to use something other than WindowClass_#. Is this possible?

Posted: Wed Jul 11, 2007 8:43 pm
by netmaestro
The classname is set when the window is created, and while there is a GetClassName_( API, there is no corresponding SetClassName_(, I don't believe this is something that can be changed after the window is created. You would have to create the window via API, registering your own classname, which takes you down a path that's a lot more work than a standard PB event loop, so you'd have to weigh whether or not the benefits would be worth the effort.

Posted: Wed Jul 11, 2007 8:58 pm
by ts-soft
here: http://www.purebasic.fr/german/viewtopi ... 876#143876

is a small tool with source that patch the classname in the exe. I haven't test it

Posted: Wed Jul 11, 2007 9:23 pm
by srod
Now that is one dirty low down trick!

:)

Posted: Wed Jul 11, 2007 9:35 pm
by ts-soft
srod wrote:Now that is one dirty low down trick!

:)
The way makes no difference, as long as he leads to the success :twisted:

Posted: Wed Jul 11, 2007 11:01 pm
by hallodri
Simple but also dirty :lol:

Code: Select all

Import "window.lib"

  CompilerIf #PB_Compiler_Unicode
    _PB_Window_ProcessEvent(a,b,c,d) As "_PB_Window_ProcessEvent_UNICODE@16"
  CompilerElse
    _PB_Window_ProcessEvent(a,b,c,d) As "_PB_Window_ProcessEvent@16"
  CompilerEndIf
  
  PB_Window_Icon
  PB_Window_Cursor
  PB_Window_Objects  
  
  PB_Object_GetOrAllocateID(*Object,id)  
EndImport

Procedure NewOpenWindow(id,x,y,cx,cy,title.s,flags=#WS_VISIBLE|#WS_OVERLAPPEDWINDOW,parent=0)
  Protected wnd.wndclass
  Protected classname.s = "MY_OWN_CLASS"
  Protected *WinObj.integer
  Protected hWnd.i
  Protected rc.rect
  
  
  With wnd
    \style          = #CS_HREDRAW|#CS_VREDRAW
    \lpfnWndProc    = @_PB_Window_ProcessEvent() 
    \hInstance      = GetModuleHandle_(0)
    \hIcon          = PB_Window_Icon
    \hCursor        = PB_Window_Cursor
    \lpszClassName  = @classname
    \hbrBackground  = #COLOR_WINDOW
    \cbWndExtra     = 0
    \cbClsExtra     = 0
  EndWith
  
  If RegisterClass_(wnd)
    
    SetRect_(rc,0,0,cx,cy)
    
    AdjustWindowRectEx_(rc,flags,0,#WS_EX_WINDOWEDGE)
    
    If x = #PB_Ignore Or y = #PB_Ignore
      x = #CW_USEDEFAULT
      y = #CW_USEDEFAULT
    EndIf
    
    hWnd = CreateWindowEx_(#WS_EX_WINDOWEDGE,classname,title,flags,x,y,rc\right-rc\left,rc\bottom-rc\top,parent,0,GetModuleHandle_(0),0)
    
    If hWnd 
      *WinObj   = PB_Object_GetOrAllocateID(PB_Window_Objects,id)
      *WinObj\i = hWnd  

      If id = #PB_Any
        SetProp_(hWnd,"Pb_WindowID",*WinObj+1) 
      Else
        SetProp_(hWnd,"Pb_WindowID",id+1) 
      EndIf 

    Else 
      UnregisterClass_(GetModuleHandle_(0),classname)
    EndIf 
    
  EndIf
 
  If id = #PB_Any
    id = *WinObj
  Else
    id = hWnd 
  EndIf
  
  ProcedureReturn id
EndProcedure

Procedure Main()
  
  NewOpenWindow(0,#PB_Ignore,#PB_Ignore,200,200,"333")
  
  Repeat
  	event = WaitWindowEvent()
  
  Until event = #PB_Event_CloseWindow
  
EndProcedure:Main()

Posted: Wed Jul 11, 2007 11:10 pm
by srod
Now that is bloody neat! 8)

Where did you get the info on the imports from window.lib ? If you don't mind me asking! :)

Posted: Wed Jul 11, 2007 11:41 pm
by hallodri
The information comes from the lib self, but i write
a little tools which help me.

Posted: Wed Jul 11, 2007 11:56 pm
by srod
Did you use a COFF library viewer or something similar?

Posted: Thu Jul 12, 2007 3:29 am
by netmaestro
@hallodri: Classname is set all right, but window events are not handled correctly (won't close) and EventWindow() is returning -1 when the window gets an event. So something isn't getting set up quite right. Otherwise interesting.

Posted: Thu Jul 12, 2007 6:46 am
by hallodri
I have forgotten SetProp. See code above

Posted: Thu Jul 12, 2007 7:43 am
by eesau
Neat. Very useful code, thanks for posting!

Re:

Posted: Wed Nov 25, 2020 3:10 am
by BarryG
netmaestro wrote:while there is a GetClassName_( API, there is no corresponding SetClassName_(, I don't believe this is something that can be changed after the window is created. You would have to create the window via API, registering your own classname
Is this comment from 13 years ago still valid with the latest PureBasic version (5.73)? I need to change the class name of my window that I created with OpenWindow.

Re: How to set the WindowClass name?

Posted: Wed Nov 25, 2020 8:18 am
by RASHAD
Hi BarryG
Change hallodri code
Then please check and report

Code: Select all

Import "window.lib"   
  CompilerIf #PB_Compiler_Version  < 550 And #PB_Compiler_Unicode
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      _PB_Window_ProcessEvent(a,b,c,d) As "_PB_Window_ProcessEvent_UNICODE@16"
    CompilerElse
      _PB_Window_ProcessEvent(a,b,c,d) As "PB_Window_ProcessEvent_UNICODE"
    CompilerEndIf
  CompilerElse
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      _PB_Window_ProcessEvent(a,b,c,d) As "_PB_Window_ProcessEvent@16"
    CompilerElse
      _PB_Window_ProcessEvent(a,b,c,d) As "PB_Window_ProcessEvent"
    CompilerEndIf
  CompilerEndIf

  PB_Window_Icon
  PB_Window_Cursor
  PB_Window_Objects 
  PB_Object_GetOrAllocateID(*Object,id) 
EndImport
Edit : It works

Re: How to set the WindowClass name?

Posted: Wed Nov 25, 2020 10:47 am
by BarryG
Thanks Rashad, yes, it's working great.