How to set the WindowClass name?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

How to set the WindowClass name?

Post 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?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Now that is one dirty low down trick!

:)
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post 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()
Last edited by hallodri on Sun Aug 22, 2010 7:10 am, edited 3 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post by hallodri »

The information comes from the lib self, but i write
a little tools which help me.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Did you use a COFF library viewer or something similar?
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post by hallodri »

I have forgotten SetProp. See code above
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

Neat. Very useful code, thanks for posting!
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re:

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: How to set the WindowClass name?

Post 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
Egypt my love
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: How to set the WindowClass name?

Post by BarryG »

Thanks Rashad, yes, it's working great.
Post Reply