Open new window with same height and Y position as another

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Open new window with same height and Y position as another

Post by Dude »

Check this out... all I want to do is open a second window at the same screen position and vertical size as the first. The only difference is the #PB_Window_SizeGadget flag, which the second window purposely doesn't have. How can I make both windows be the exact same screen position and vertical size, then?

If I remove #PB_Window_SizeGadget from the first window, then yes, it works; but I need that size flag there. :shock:

Image

Code: Select all

y=200 ; Both windows should open at this vertical position!

OpenWindow(0,200,y,200,100,"Lower",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
OpenWindow(1,300,y,WindowWidth(0),WindowHeight(0),"Higher - why?",#PB_Window_SystemMenu)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
[Edited] To clarify that I don't want the second window to be centered inside the first. Just X/Y position and height must be the same, not the width.
Last edited by Dude on Sun Aug 28, 2016 9:51 am, edited 2 times in total.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Open new window with same size and position as another

Post by bbanelli »

I don't know technical answer to your question, but maybe you can use this as a workaround?

Code: Select all

y=200 ; Both windows should open at this vertical position!

OpenWindow(0,200,y,200,100,"Lower",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
OpenWindow(1,300,y,WindowWidth(0),WindowHeight(0),"Higher - why?",#PB_Window_SystemMenu|#PB_Window_WindowCentered,WindowID(0))

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Open new window with same size and position as another

Post by Dude »

Hi, yes #PB_Window_WindowCentered works for this example, but sometimes the first window will be wider than the second, and I really need the second to be the same X/Y position, and height only, as the first (like in my code).

In the example image I posted, I really want the second window to be the same Y position as the first, even if X is different. I didn't clearly explain that in my first post, sorry (I've now edited it).

I think this is a bug with the resize flag or something, because it should work as coded in my first post.
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: Open new window with same height and Y position as anoth

Post by Bisonte »

Interesting. On Windows 10 the window is not higher... The window have another height .... (PB5.43LTS)

Image
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Open new window with same height and Y position as anoth

Post by RASHAD »

With window(0) you are using #PB_Window_SizeGadget it means different theme
Try

Code: Select all

y=200 ; Both windows should open at this vertical position!

OpenWindow(0,200,y,200,100,"Lower",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
OpenWindow(1,300,WindowY(0,#PB_Window_FrameCoordinate),WindowWidth(0),WindowHeight(0),"Higher - why?",#PB_Window_SystemMenu)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Egypt my love
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Open new window with same height and Y position as anoth

Post by DontTalkToMe »

@Dude

Using /subsystem:windows,6.0 work as expected with and without #PB_Window_SizeGadget
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Open new window with same height and Y position as anoth

Post by Dude »

Rashad: Your #PB_Window_FrameCoordinate example didn't make any difference; the second window was still slighter higher than the first.

DontTalkToMe: I really don't want to change the default PureBasic settings for this. There must be an easier way to make this work. Anyway, I entered "windows,6.0" in Compiler Options as a test, and it didn't work (it said: "The following subsystem cannot be found: windows").

At the end of the day, shouldn't something as simple as setting WindowHeight(win) match the height of "win"...?
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Open new window with same height and Y position as anoth

Post by DontTalkToMe »

Dude wrote: Anyway, I entered "windows,6.0" in Compiler Options as a test, and it didn't work (it said: "The following subsystem cannot be found: windows").
it's not THAT subsystem :wink:

http://www.purebasic.fr/english/viewtop ... =3&t=61679

instructions on how to use it

http://purebasic.fr/english/viewtopic.p ... 88#p461188

all the problems mentioned in these different threads, and yours mentioned here, will probably never go away without that linker option. fred added a workaround (instead of the linker option) but later on removed it because was causing other problems
http://purebasic.fr/english/viewtopic.p ... 74#p468874

what surprises me if it has taken so long for someone to report it and for someone to report the fix, the problem must be here from years ago
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Open new window with same height and Y position as anoth

Post by Dude »

Tried the linker options fix, and it works for Y position and height (woohoo!) but now the X position of the second window is off by 5 pixels to the right. :(

Why is this so freaking hard? Why can't WindowX(new) match WindowX(old), and WindowHeight(new) match WindowHeight(old)? Seems so basic to get right... what's going on? Why do we need linker options and all this other crap?
User avatar
TI-994A
Addict
Addict
Posts: 2752
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Open new window with same height and Y position as anoth

Post by TI-994A »

Dude wrote:...only difference is the #PB_Window_SizeGadget flag...
This is a Windows issue, by design. The #WS_SIZEBOX property is synonymous with #WS_THICKFRAME; and thus the offset.

Pure-API windows yield exactly the same results:

Code: Select all

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_CLOSE
      DestroyWindow_(hWnd)
    Case #WM_DESTROY
      PostQuitMessage_(0)
    Default
      result = DefWindowProc_(hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

wndClass$ = "API Window"
wndClass.WNDCLASSEX
With wndClass
  \lpfnWndProc = @WndProc()
  \lpszClassName = @wndClass$
  \cbSize = SizeOf(WNDCLASSEX)
EndWith
RegisterClassEx_(@wndClass)

client.RECT
With client
  \top = 0
  \left = 0
  \right = 200
  \bottom = 100
  AdjustWindowRect_(@client, #WS_CAPTION | #WS_SYSMENU | #WS_SIZEBOX, 0);
  hWnd  = CreateWindowEx_(0, wndClass$, "Resizable Win",  
                          #WS_VISIBLE | #WS_SYSMENU | #WS_SIZEBOX,
                          200, 400, \right - \left, \bottom - \top, 0, 0, 0, 0)
  \top = 0
  \left = 0
  \right = 200
  \bottom = 100
  AdjustWindowRect_(@client, #WS_CAPTION | #WS_SYSMENU, 0);
  hWnd2 = CreateWindowEx_(0, wndClass$, "Regular Win",  
                          #WS_VISIBLE | #WS_SYSMENU,
                          300, 400, \right - \left, \bottom - \top, 0, 0, 0, 0)
EndWith

While GetMessage_(uMsg.MSG, 0, 0, 0)
  TranslateMessage_(uMsg)
  DispatchMessage_(uMsg)
Wend
Bug or not, it's not a PureBasic issue. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Open new window with same height and Y position as anoth

Post by Dude »

So... is the value of #WS_THICKFRAME somewhere to be evaluated at runtime, so I can just add that to the window's Y position? GetSystemMetrics_() or something?
User avatar
TI-994A
Addict
Addict
Posts: 2752
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Open new window with same height and Y position as anoth

Post by TI-994A »

Dude wrote:...is the value of #WS_THICKFRAME somewhere to be evaluated at runtime...
Not that I'm aware of. However, although the difference could simply be calculated, it's not bulletproof, as the results differ on different versions of Windows. The results of your original post are consistent with Windows 8.1, but not with Windows 10; on 10, the y-position is correct, while the height differs.

Nevertheless, the difference could be calculated like so:

Code: Select all

Procedure metricsDifference(width, height)
  client.RECT
  With client
    \top = 0
    \left = 0
    \right = width
    \bottom = height
    AdjustWindowRect_(@client, #WS_CAPTION | #WS_SYSMENU | #WS_SIZEBOX, 0);
    bottom = \bottom
    \top = 0
    \left = 0
    \right = width
    \bottom = height
    AdjustWindowRect_(@client, #WS_CAPTION | #WS_SYSMENU, 0);
    yDifference = bottom - \bottom
  EndWith
  ProcedureReturn yDifference
EndProcedure

y = 200
yOffset = metricsDifference(200, 100)
OpenWindow(0, 200, y, 200, 100, "Same!", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
OpenWindow(1, 300, y + yOffset, WindowWidth(0), WindowHeight(0), "Same!", #PB_Window_SystemMenu)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
On Windows 10, the difference could be offset from the window height.

But, like I said, not bulletproof. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Open new window with same height and Y position as anoth

Post by Dude »

Hi TI-994A, your solution is perfect for a small fonts (DPI) Windows system, and I can even shorten it to the following; but both yours and my shortened version fail on a large fonts Windows system. Oh well. Most users have small fonts by default, so I can live with that. :lol:

Code: Select all

y=200 ; Both windows should open at this vertical position!

noresize=GetSystemMetrics_(#SM_CYSIZEFRAME)-GetSystemMetrics_(#SM_CYFIXEDFRAME)

OpenWindow(0,200,y,200,100,"Original",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
OpenWindow(1,300,y+noresize,WindowWidth(0),WindowHeight(0),"Same Y pos!",#PB_Window_SystemMenu)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Open new window with same height and Y position as anoth

Post by DontTalkToMe »

Dude wrote:Tried the linker options fix, and it works for Y position and height (woohoo!) but now the X position of the second window is off by 5 pixels to the right. :(
It's the other way around. Now it is at the right place. Previously without the linker option was n pixels off to the left.

Anyway PB does something wrong when the #PB_Window_SizeGadget is applied, even the linker option seems not enough to make all work correctly.

The second window is now positioned at x=300 but the first one is not at x=200 using PB openwindow with the linker option.

If you use a full API only program, with the linker option which microsoft compilers specify when targeting newer OSes, the windows are positioned correctly. So using API and the flags as prescribed by MS (as idiotic as it may be) all seems to work, at least on windows 7 where I tested this, don't know if MS has furtherly botched this in newer OSes.
Last edited by DontTalkToMe on Sun Aug 28, 2016 2:09 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Open new window with same height and Y position as anoth

Post by RASHAD »

I hate to switch between windows versions
Simple solution
Tested with windows 7 x86

Code: Select all

y=200 ; Both windows should open at this vertical position!

OpenWindow(0,200,y,200,100,"Lower",#PB_Window_SystemMenu)
OpenWindow(1,300,y,WindowWidth(0),WindowHeight(0),"Higher - why?",#PB_Window_SystemMenu)
SetWindowLongPtr_(WindowID(0),#GWL_STYLE,GetWindowLongPtr_(WindowID(0),#GWL_STYLE)|#WS_THICKFRAME)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Egypt my love
Post Reply