Seite 1 von 2

Wie abfragen, ob (OS-)Fenster maximiert ist?

Verfasst: 27.10.2014 14:13
von es_91
Achtung! Der Threadtitel wurde mehrfach geändert. Ursprünglicher Titel: Fenster verschieben auf APIisch

Hi.

Mit SetWindowPos_ () oder MoveWindow_ () kann man die Göße eines gesamten Fensters setzen. Dabei wird der Rahmen mitberechnet, heißt: die angegebene Größe wird zu (Fenstergröße + Rahmen).

Nun weiß ich, dass unter Window 10 die Rahmen nur noch 1 oder 2 Pixel breit sind (bis auf die Titelzeile). Ich möchte aber genau sein und frage Euch deshalb nach einer Lösung, wie man nur die ClientArea in Ihrer Größe verändern kann.

... So, wie es PureBasic auch macht, bei seinen eigenen Fenstern.

Re: Fensterkoordinatensetzen auf APIisch

Verfasst: 27.10.2014 14:36
von _JON_
Wir wärs mit GetWindowRect_() und GetClientRect_() ?

Code: Alles auswählen


lpRect.rect

If OpenWindow(0, 300, 400, 200, 100, "test", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 
    
   GetWindowRect_(WindowID(0), lpRect)
   
   Debug "Pos x: " + Str(lpRect\left)
   Debug "Pos y: " + Str(lpRect\top)
      
   Debug "Width: " + Str(lpRect\right - lpRect\left)
   Debug "Hight: " + Str(lpRect\bottom - lpRect\top)
   
   GetClientRect_(WindowID(0), lpRect)
   
   Debug "Pos x: " + Str(lpRect\left)
   Debug "Pos y: " + Str(lpRect\top)
      
   Debug "Width: " + Str(lpRect\right - lpRect\left)
   Debug "Hight: " + Str(lpRect\bottom - lpRect\top)
   
    Repeat 
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow 
          End 
      EndSelect 
    ForEver 
    
  EndIf 


Re: Fensterkoordinatensetzen auf APIisch

Verfasst: 27.10.2014 14:46
von es_91
Gibt es denn eine SetClientRect_ ()-Funktion?

Bedenke: ich will keine Fenster von PureBasic in deren Größe verändern, sondern "fremde"!

Re: Fensterkoordinatensetzen auf APIisch

Verfasst: 27.10.2014 15:00
von ts-soft
Meinste:

Code: Alles auswählen

Define rect.RECT

rect\right = 600
rect\bottom = 200
AdjustWindowRect_(@rect, #WS_BORDER | #WS_CAPTION, 0)
Debug rect\right
Debug rect\bottom
// edit
So macht der Code mehr Sinn :D

Re: Fensterkoordinatensetzen auf APIisch

Verfasst: 27.10.2014 15:18
von es_91
Alles klar, ich ermittle also die "echten" RECT-Daten über AdjustWindowRect_ () und setze diese dann mit SetWindowPos_ (). Dankeschön! :)

Verfasst: 27.10.2014 15:38
von CodeCommander
~ DELETE ~

Re: Fensterkoordinatensetzen auf APIisch

Verfasst: 27.10.2014 15:42
von ts-soft
Ich hatte vorhin recht wenig Zeit, aber du bist ja von allein drauf gekommen :wink:

Re: Koordinaten fremder Fenster setzen

Verfasst: 28.10.2014 11:54
von es_91
/* Titel wurde geändert */

Hallo. Ich habe ein Program geschrieben, mit dem ein Fenster automatisch in der Mitte des Bildschirms angezeigt wird und eine bestimmte Größe erhält. Ich hatte gehofft, damit alle meine Fensterchen auf eine Standartgröße setzen zu können, jedoch (bei den meisten Anwendungen) ohne Erfolg.

Folgende Fenster kann ich mit unten stehendem Code in ihrer Größe verändern: Notepad (Editor) und PureBasic IDE.

Code: Alles auswählen

; 
; SetProperWindowCoordinates.pb
; 
; Author: Enrico Seidel
; Date: 10/28/2014
; Demo: no

#WindowWidth = 1008
#WindowHeight = 544

;{ Calculating desktop measures savely

DevMode. DEVMODE

DevMode\ dmSize = SizeOf (DEVMODE)

EnumDisplaySettings_ (#Null, #ENUM_CURRENT_SETTINGS, DevMode)

DesktopWidth = DevMode\ dmPelsWidth
DesktopHeight = DevMode\ dmPelsHeight

;}

WindowRect. RECT
SolidWindowRect. RECT

WindowRect\ Bottom = (DesktopHeight + #WindowHeight) / 2
WindowRect\ Left = (DesktopWidth - #WindowWidth) / 2
WindowRect\ Top = (DesktopHeight - #WindowHeight) / 2
WindowRect\ Right = (DesktopWidth + #WindowWidth) / 2

SolidWindowRect = WindowRect

WindowName. s = InputRequester("Fenster", "Titel des Fensters:", "")

If WindowName
  
  WindowHandle = FindWindow_ (0, WindowName)
  
  AdjustWindowRect_ (WindowRect, #WS_SYSMENU| #WS_CAPTION, #Null)
  
  WindowRect\ Bottom = (DesktopHeight + SolidWindowRect\ Bottom - SolidWindowRect\ Top) / 2
  WindowRect\ Left = (DesktopWidth - SolidWindowRect\ Right + SolidWindowRect\ Left) / 2
  WindowRect\ Top = (DesktopHeight - SolidWindowRect\ Bottom + SolidWindowRect\ Top) / 2
  WindowRect\ Right = (DesktopWidth + SolidWindowRect\ Right - SolidWindowRect\ Left) / 2
  
  Debug SetWindowPos_ (WindowHandle, #Null, WindowRect\ Left, WindowRect\ Top, WindowRect\ Right - WindowRect\ Left, WindowRect\ Bottom - WindowRect\ Top, #Null)
  
EndIf
(Der Teil, in dem die Desktopkoordinaten abgefragt werden musste sein, da DesktopWidth () und DesktopHeight () bei meinem Arbeitsrechner nicht mehr funktionieren (engl.).)

Jetzt funktioniert dies allerdings nur bei den genannten Programmen. Warum geht das nicht auch bei anderen Anwendungen, wie zum Beispiel Word, Excel oder Notepad++?

Re: Koordinaten von Fenstern verändern

Verfasst: 30.10.2014 03:00
von chi
Zuverlässiger wäre es so...

Code: Alles auswählen

;
; SetProperWindowCoordinates.pb
;
; Author: Enrico Seidel
; Date: 10/28/2014
; Demo: no

Procedure ListVisibleWindows()
  Protected hwnd, txt.s
  hwnd = GetWindow_(GetDesktopWindow_(), #GW_CHILD)
  While hwnd ! 0
    If IsWindowVisible_(hwnd)
      txt = Space(#MAX_PATH)
      GetWindowText_(hwnd, txt, #MAX_PATH)
      If LCase(txt) <> "start" And LCase(txt) <> "program manager" And txt <> ""
        Debug "handle: " + hwnd + Chr(9) + txt
      EndIf
    EndIf
    hwnd = GetWindow_(hwnd, #GW_HWNDNEXT)
  Wend
EndProcedure

#WindowWidth = 1008
#WindowHeight = 544

;{ Calculating desktop measures savely

  DevMode. DEVMODE
  
  DevMode\ dmSize = SizeOf (DEVMODE)
  
  EnumDisplaySettings_ (#Null, #ENUM_CURRENT_SETTINGS, DevMode)
  
  DesktopWidth = DevMode\ dmPelsWidth
  DesktopHeight = DevMode\ dmPelsHeight

;}

WindowRect. RECT
SolidWindowRect. RECT

WindowRect\ Bottom = (DesktopHeight + #WindowHeight) / 2
WindowRect\ Left = (DesktopWidth - #WindowWidth) / 2
WindowRect\ Top = (DesktopHeight - #WindowHeight) / 2
WindowRect\ Right = (DesktopWidth + #WindowWidth) / 2

SolidWindowRect = WindowRect

ListVisibleWindows()

WindowHandle = Val(InputRequester("Fenster", "Handle des Fensters:", ""))

If WindowHandle
  
  If IsIconic_(WindowHandle)
    ShowWindow_(WindowHandle, #SW_RESTORE)
  EndIf
  
  AdjustWindowRect_ (WindowRect, #WS_SYSMENU| #WS_CAPTION, #Null)
  
  WindowRect\ Bottom = (DesktopHeight + SolidWindowRect\ Bottom - SolidWindowRect\ Top) / 2
  WindowRect\ Left = (DesktopWidth - SolidWindowRect\ Right + SolidWindowRect\ Left) / 2
  WindowRect\ Top = (DesktopHeight - SolidWindowRect\ Bottom + SolidWindowRect\ Top) / 2
  WindowRect\ Right = (DesktopWidth + SolidWindowRect\ Right - SolidWindowRect\ Left) / 2
  
  Debug SetWindowPos_ (WindowHandle, #Null, WindowRect\ Left, WindowRect\ Top, WindowRect\ Right - WindowRect\ Left, WindowRect\ Bottom - WindowRect\ Top, #Null)
  
EndIf

[Programm] Fenster schön mittig anzeigen lassen

Verfasst: 30.10.2014 08:29
von es_91
Ja! Vielen Dank, chi. :allright:

Hier ein verbessertes Programm, was die Änderungen von chi verwendet:

//updated: 17:14 Uhr

Code: Alles auswählen

;
; SetProperWindowCoordinates3.pb
;
; Author: Enrico Seidel
; Date: 10/30/2014
; Demo: no

Window = OpenWindow (#PB_Any, 0, 0, 315, 56, "Auswahl", #PB_Window_SystemMenu| #PB_Window_ScreenCentered)

Button1 = ButtonGadget (#PB_Any, 13, 17, 50, 22, "16 : 9")

Button2 = ButtonGadget (#PB_Any, 73, 17, 50, 22, "16 : 10")

Button3 = ButtonGadget (#PB_Any, 133, 17, 50, 22, "4 : 3")

Button4 = ButtonGadget (#PB_Any, 193, 17, 50, 22, "5 : 4")

Button5 = ButtonGadget (#PB_Any, 253, 17, 50, 22, "k/Ä")
DisableGadget (Button5, #True)

Repeat
	
	Select WaitWindowEvent ()
			
		Case #PB_Event_Gadget
			
			Select EventGadget ()
					
				Case Button1
					H = 360
					Break
					
				Case Button2
					H = 400
					Break
					
				Case Button3
					H = 480
					Break
					
				Case Button4
					H = 512
					Break
					
				Case Button5
					OnlyCenter = #True
					Break
					
			EndSelect
			
		Case #PB_Event_CloseWindow
			
			End
			
	EndSelect
	
ForEver

CloseWindow (Window)

Procedure ListVisibleWindows ()
	
	Protected HWnd, Txt.s
	Shared ListIcon
	
	HWnd = GetWindow_ (GetDesktopWindow_ (), #GW_CHILD)
	
	While Not HWnd = #Null
		
		If IsWindowVisible_ (HWnd)
			
			txt = Space (#MAX_PATH)
			GetWindowText_ (HWnd, Txt, #MAX_PATH)
			
			If LCase (Txt) <> "start" And LCase (Txt) <> "program manager" And Txt <> ""
				
				AddGadgetItem (ListIcon, -1, Str (HWnd) + #LF$ + Txt)
				
			EndIf
			
		EndIf
		
		hwnd = GetWindow_ (HWnd, #GW_HWNDNEXT)
		
	Wend
	
EndProcedure

Window = OpenWindow (#PB_Any, #PB_Ignore, #PB_Ignore, 630, 340, "Visible Windows on Desktop", #PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)

ListIcon = ListIconGadget (#PB_Any, 15, 10, 600, 290, "Handle", 125, #PB_ListIcon_FullRowSelect)
AddGadgetColumn(ListIcon, 1, "Title", 300)

Button = ButtonGadget (#PB_Any, 535, 310, 80, 20, "Aktualisieren")

;{ Calculating desktop measures savely

DevMode. DEVMODE

DevMode\ dmSize = SizeOf (DEVMODE)

EnumDisplaySettings_ (#Null, #ENUM_CURRENT_SETTINGS, DevMode)

DesktopWidth = DevMode\ dmPelsWidth
DesktopHeight = DevMode\ dmPelsHeight

;}

ListVisibleWindows()

Repeat
	
	Select WaitWindowEvent ()
			
		Case #PB_Event_CloseWindow
			
			Break
			
		Case #PB_Event_SizeWindow
		  
		  ResizeGadget (ListIcon, 15, 10, WindowWidth (Window) - 30, WindowHeight (Window) - 50)
		  ResizeGadget (Button, WindowWidth (Window) - 95, WindowHeight (Window) - 30, #PB_Ignore, #PB_Ignore)
		  
		Case #PB_Event_Gadget
			
			Select EventGadget ()
					
				Case ListIcon
					
					If EventType () = #PB_EventType_LeftDoubleClick
						
						WindowHandle = Val (GetGadgetItemText (ListIcon, GetGadgetState (ListIcon), #Null))
						
						If WindowHandle
							
							If IsIconic_ (WindowHandle)
								ShowWindow_ (WindowHandle, #SW_RESTORE)
							EndIf
							
							If OnlyCenter
								Rect. RECT
								GetClientRect_ (WindowHandle, Rect)
								WindowWidth = Rect\ Right
								WindowHeight = Rect\ Bottom
							Else
								WindowWidth = 630 * (1.0 * DesktopWidth / 640)
								WindowHeight = 1.0 * WindowWidth / 630 * (336.0 / 360 * H)
							EndIf
							
							WindowRect. RECT
							
							WindowRect\ Bottom = (DesktopHeight + WindowHeight) / 2
							WindowRect\ Left = (DesktopWidth - WindowWidth) / 2
							WindowRect\ Top = (DesktopHeight - WindowHeight) / 2
							WindowRect\ Right = (DesktopWidth + WindowWidth) / 2
							
							SolidWindowRect. RECT
							SolidWindowRect = WindowRect

							AdjustWindowRect_ (WindowRect, #WS_SYSMENU| #WS_CAPTION, #Null)
							
							WindowRect\ Bottom = (DesktopHeight + SolidWindowRect\ Bottom - SolidWindowRect\ Top) / 2
							WindowRect\ Left = (DesktopWidth - SolidWindowRect\ Right + SolidWindowRect\ Left) / 2
							WindowRect\ Top = (DesktopHeight - SolidWindowRect\ Bottom + SolidWindowRect\ Top) / 2
							WindowRect\ Right = (DesktopWidth + SolidWindowRect\ Right - SolidWindowRect\ Left) / 2
							
							SetWindowPos_ (WindowHandle, #Null, WindowRect\ Left, WindowRect\ Top, WindowRect\ Right - WindowRect\ Left, WindowRect\ Bottom - WindowRect\ Top, #Null)
							
						EndIf
						
					EndIf
					
				Case Button
					
					ClearGadgetItems (ListIcon)
					ListVisibleWindows ()
					
			EndSelect
			
	EndSelect		
	
ForEver
Try it! ;)

ToDo:

- Unterstützung für den k/Ä-Button hinzufügen
- Programme wie WinAmp zur Kooperation bringen
- diverse Verbesserungen...