Get window properties

Just starting out? Need help? Post your questions and find answers here.
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Get window properties

Post by ZX80 »

Good time to all.

How can I know, has window shadow? I'm looking for API-solution. Target window is not mine.
I tried to find out so:

Code: Select all

If Bool(GetWindowLong_(hWnd, #GCL_STYLE) & $00020000) = #True
, but it doesn't work.


Thanks in advance.
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Get window properties

Post by chi »

GCL = GetClassLong... but use GetClassLongPtr so it works on x86 and x64

Code: Select all

If Bool(GetClassLongPtr_(hWnd, #GCL_STYLE) & #CS_DROPSHADOW) = #True
Et cetera is my worst enemy
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: Get window properties

Post by ZX80 »

Hi, chi.
Glad to see you again :D

Thank you for your reply, but it doesn't work too:

Code: Select all

hWnd = FindWindow_("TTOTAL_CMD", "Total Commander 9.22a - xxx") ;Change to your title or other window
If hWnd
  Debug hWnd
  SetForegroundWindow_(hWnd)
  Sleep_(1000)
  If Bool(GetClassLongPtr_(hWnd, #GCL_STYLE) & #CS_DROPSHADOW) = #True
    Debug "Total is active now"
  Else
    Debug "Total is INactive now"
  EndIf
EndIf
I need exactly this definition method.
Another way. I thought to look for the 'x' button in the title bar and then use GetPixel API-function to make sure the 'close' button is red color. But this is more difficult and less accurate. Especially if aero is used. This is just some kind of madness. Yes, you may have thought so. But believe me, this is really necessary. Okay. First I need to find out the location of the system buttons. For this we use the TITLEBARINFOEX-structure. RASHAD's code(from here) does a great job of it. But what about the gradient? It's not a pure red color. Or compare a specific pixel of the 'x' button with all windows? And the pixel that differs from all the others will be the active window.
I have also tried using the #WS_OVERLAPPEDWINDOW constant to get foreground window. Nothing works.

P.S. Yes, this is a non-standard solution for this issue. I like it :mrgreen:
Because GetForegroundWindow_ doesn't work in my case (jokes aside).
Last edited by ZX80 on Mon Dec 07, 2020 4:46 pm, edited 2 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Get window properties

Post by RASHAD »

You may need :
- DwmIsCompositionEnabled_() and
- DwmGetWindowAttribute_()

For modern styles Window >= 7
Egypt my love
ZX80
Enthusiast
Enthusiast
Posts: 330
Joined: Mon Dec 12, 2016 1:37 pm

Re: Get window properties

Post by ZX80 »

RASHAD, thank you very much :!:

Your help is very important for me. I went to read.
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Get window properties

Post by SeregaZ »

how to get backgroung color with this GetClassLong? GetWindowColor(#Window) shows -1
but i want to get current default color.

Code: Select all

Enumeration
  #Window
EndEnumeration

If OpenWindow(#Window, 100, 100, 200, 160, "TEST", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  hWnd = FindWindow_("WindowClass_0", "TEST") 
  
  If hWnd
    Debug "hWnd = " + Str(hWnd)
    TEST.LOGBRUSH = GetClassLong_(hWnd, #GCL_HBRBACKGROUND)
    Debug TEST\lbColor
  EndIf
  
  Repeat
     Select WaitWindowEvent()

       Case #PB_Event_CloseWindow
         qiut = 1
   
     EndSelect
   Until qiut = 1

EndIf

End
but it dont want to use structure as return value. as i can see C++ have some GetObject(hBrush, sizeof(LOGBRUSH), &lb); - but how to do same with PB?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Get window properties

Post by RASHAD »

Hi

Code: Select all

Enumeration
  #Window
EndEnumeration

If OpenWindow(#Window, 100, 100, 200, 160, "TEST", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  hWnd = FindWindow_("WindowClass_0", "TEST") 
  
  If hWnd
    Debug "hWnd = " + Str(hWnd)
    hBrush = GetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND)
    buff = GetObject_(hBrush,SizeOf(hBrush),0)
    GetObject_(hBrush,buff,test.LOGBRUSH)
    Debug test\lbColor
  EndIf
  
  Repeat
     Select WaitWindowEvent()

       Case #PB_Event_CloseWindow
         qiut = 1
   
     EndSelect
   Until qiut = 1

EndIf

End
Egypt my love
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Get window properties

Post by SeregaZ »

o! i thought it was something C++ internal :) not a winapi. thanks!
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Get window properties

Post by SeregaZ »

*crying...

it is wrong color. for my case it is return white - 16777215, same as RGB(255, 255, 255)
but i need to get some kind of 13158600 - RGB(200, 200, 200) - some light gray windows background color. not some listview, but just window's color - body color.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Get window properties

Post by RASHAD »

With Windows API you get the proper Color
Maybe PB is using different scheme because of the Cross platforms
Use Startdrawing (Windowoutput()) and Point to get the proper color

Code: Select all

Declare WndProc(Wnd, Message, wParam, lParam) 
Declare XHiWord(a.l) 
Declare XLoWord(a.l) 

Global Appname.s,hMenu.l 
Appname = "Meinfenster" 

wc.WNDCLASS 
wc\style          =  #CS_VREDRAW | #CS_HREDRAW 
wc\lpfnWndProc    =  @WndProc() 
wc\cbClsExtra     =  0 
wc\cbWndExtra     =  0 
wc\hInstance      =  hInstance 
wc\hIcon          =  LoadIcon_(hInstance, "#1") 
wc\hCursor        =  LoadCursor_(0, #IDC_ARROW) 
wc\hbrBackground  =  CreateSolidBrush_(GetSysColor_(15)) 
wc\lpszMenuName   =  0 
wc\lpszClassName  =  @Appname 
RegisterClass_(wc) 
hWnd = CreateWindowEx_(0,Appname,Caption$,#WS_OVERLAPPEDWINDOW,#CW_USEDEFAULT,0,#CW_USEDEFAULT,0,0,0,hInstance,0) 

UpdateWindow_(hWnd) 
ShowWindow_(hWnd,#SW_SHOWNORMAL) 
SetForegroundWindow_(hWnd)

hBrush = GetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND)
buff = GetObject_(hBrush,SizeOf(hBrush),0)
GetObject_(hBrush,buff,test.LOGBRUSH)
Debug test\lbColor 

;der Einfachheit halber Menü mit PB 
hMenu = CreateMenu(0,hWnd) 
MenuTitle("File") 
MenuItem( 1, "Load") 
MenuItem( 2, "Save") 
MenuItem( 3, "Save As") 
MenuBar() 
MenuItem( 4, "Ende") 


While GetMessage_(m.MSG, 0, 0, 0) 
  TranslateMessage_(m) 
  DispatchMessage_(m) 
Wend 

Procedure WndProc(Wnd,Message,wParam,lParam) 
  Returnval.l = DefWindowProc_(Wnd, Message, wParam, lParam) 
  Select Message 
  ; *************************************************************************** 
    Case #WM_COMMAND 
          Select XLoWord(wParam) 
          Case 1 
          MessageRequester("Meldung","Load gewنhlt",0) 
          Case 2 
          MessageRequester("Meldung","Save gewنhlt",0) 
          Case 3 
          MessageRequester("Meldung","Save as...gewنhlt",0) 
          Case 4 
          PostQuitMessage_(0) 
          Case 100 
          MessageRequester("Meldung","Button gedrückt",0) 
          EndSelect 
  ; *************************************************************************** 
  ; *************************************************************************** 
    Case #WM_CLOSE 
  ; *************************************************************************** 
    UnregisterClass_(Appname,hInstance) 
    PostQuitMessage_(0) 
  EndSelect 
  ProcedureReturn Returnval 
EndProcedure 

Procedure XHiWord(a.l) 
  ProcedureReturn Int(a / $10000) 
EndProcedure 

Procedure XLoWord(a.l) 
  ProcedureReturn Int(a - (Int(a/$10000)*$10000)) 
EndProcedure

Egypt my love
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Get window properties

Post by SeregaZ »

thanks for Startdrawing (Windowoutput()) - i will use it.
Post Reply