Window position bottom right ?

New to FreeBASIC? Post your questions here.
Post Reply
softfoot
Posts: 34
Joined: Aug 31, 2020 3:45

Window position bottom right ?

Post by softfoot »

Having great fun with FreeBasic :-)

I have managed to create a program that displays simple controls and statii and it works very well.

I have figured how to move it from the default center of screen to be centered on the cursor but I would like to create it so that it sits at the bottom right of the screen and be wholly on the screen (if that makes sense).

The obvious way is to obtain the screen dimensions (how?) and subtract the hight and width of the program's window and then move the window.
But I wonder if there is a better way ?

Dave
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Window position bottom right ?

Post by deltarho[1859] »

In Windows use:

Code: Select all

Dim Shared As Long xWindow, yWindow
After window creation use something like:

Code: Select all

IF AfxFileExists(AfxGetExePath & "\Location.dat") Then
  f = Freefile
  Open AfxGetExePath & "\Location.dat" For Input As #f
  Get #f, , xWindow
  Get #f, , yWindow
  Close #f
  SetWindowPos( hDlg, HWND_TOPMOST, xWindow, yWindow, 0, 0, SWP_NOSIZE )
Else
  pWindow.Center
  SetWindowPos hDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If

I use José Roca's WinFBX and I am a topmost fanatic. :)

On your application's initial outing Location.dat won't exist, so it will get centred on the desktop.

Now position the window wherever you want it.

In the call back function trap WM_CLOSE and SC_CLOSE and use something like this:

Code: Select all

Dim As Long x, y, f
Dim rc As RECT
GetWindowRect( hDlg, @rc )
' If Window has moved then save current location
If xWindow <> rc.left Orelse yWindow <> rc.top Then
  f = Freefile
  Open AfxGetExePath & "\Location.dat" For Output As #f
    Put #f, , rc.left
    Put #f, , rc.top
  Close #f
End If

For second and subsequent application sessions, your window will be located at the last saved location.

Notice that xWindow and yWindow are Longs. I have a secondary monitor and a lot of my applications open on that.
softfoot
Posts: 34
Joined: Aug 31, 2020 3:45

Re: Window position bottom right ?

Post by softfoot »

I have been experimenting and have run into an odd problem the following code starts a dialog and tries to position the window,
but if I perform math on the retrieved position it is not obeyed. If I pass a literal value it works !

This is almost certainly something this newbie has missed !

The dialog is positioned correctly in the "Y" direction but the "X" position is always hard against the right hand side of the screen no matter what value I subtract from "dimensions.right"

What am I doing wrong ??

Regards,
Dave

Code: Select all

dim as short id, c, x, y
dim as RECT dimensions

'' Get the screen size
SystemParametersInfo(SPI_GETWORKAREA, 0, @dimensions, 0)
MessageBox(0, STR$(dimensions.top)+" "+STR$(dimensions.bottom)+" "+STR$(dimensions.left)+" "+STR$(dimensions.right), "DEBUG Message", 0)
x=(dimensions.right) - 1000
y=(dimensions.bottom) - (NUMSYSTEMS+NUMCHECKBOXES+NUMCOMBOBOXES)*20+15-500
MessageBox(0, STR$(x)+" "+STR$(y), "DEBUG Message", 0)

'' NB The first parameter is the number of controls expected
Dialog(NUMSYSTEMS+NUMCHECKBOXES+NUMCOMBOBOXES,_
        x, 100,_
        80, (NUMSYSTEMS+NUMCHECKBOXES+NUMCOMBOBOXES)*20+15, "Remote", lpdt, _
        WS_OVERLAPPED or WS_SYSMENU)
Post Reply