Is it possible to open and resize a form dynamically?

Let me explain. The form opens using this function (macro) and with the specified dimensions, not dynamic:
.
Resize(1338,580,300,250)
.
What I would like is for this function to become dynamic. For example, if the form was moved or resized on the screen and closed, that it can open (if requested) in the last location when closing it and with the new dimension if it has been resized. Here is the function (found on the internet):
.

Sub Resize(iWidth,iHeight,Optional iXPos,Optional iYPos) As Integer			' Fonction procédant au dimensionnement des divers formulaires
	Dim vRect as Object 													' RĂ©initialisation de la variable
	If IsNull(thisComponent.currentController.Frame) then exit sub			' Si la variable ne contient pas de données alors la fonction se termine
	vRect = thisComponent.currentController.Frame.ContainerWindow.PosSize
    Wait 200
    If IsMissing(iXPos) Then iXPos=vRect.X									' Teste si la fonction est appelée avec un paramÚtre facultatif
    If IsMissing(iYPos) Then iYPos=vRect.Y
	thisComponent.currentController.Frame.ContainerWindow.setPosSize(iXPos, iYPos, iWidth, iHeight, 15)
    Wait 200
	thisComponent.currentController.Frame.ContainerWindow.setPosSize(iXPos, iYPos, iWidth, iHeight, 15)
End Sub

Hello,

A bit of background.

In the past (few years ago?), the size and position of a form was remembered when the form was edited. Then a Bug arose. Then code (similar to yours) was used to resize/reposition the form. The another bug arose where setPosSize was not working and the fix was to repeat that line (as you have now). At some point that requirement was no longer needed (something done to correct). The code works for me without the second occurrence.

Now back to your question. Simply attach the macro (the one calling Resize) to the Open Document event of the form. Then when the document is opened, the macro will be executed.

2 Likes

Ok Thanks!

counter_2022.odb (46.0 KB)

I stored the position, size and zoom in the form’s user-defined properties. The form’s open event applies these settings calling a Base document macro. Same macro should work in global scope as well.

“Form” refers to the form document here.

Maybe the question wasn’t clear, because neither Ratslinger nor you Villeroy really answered my question.
.
The Resize function I exposed in this post causes the rectangle of the form window to be forced open according to the coordinates passed to the function, which are the coordinates of a rectangle by default. This function is executed when the form is opened, Open Document event.
.
If for example I drag the window of the form in a 2nd screen, the coordinates of the window have just changed, it is no longer the same rectangle coordinates. Now if I close the form window with these new coordinates, they are not saved anywhere, so if I open this form again it will open where the default coordinates are and not the existing coordinates when it was closed .
.
The time of a work session, apart from the first opening of a form where I want the form to open at a specific location, I would like the opening and closing of a form not to be those of the coordinates by default, but rather those that take into account the new location on the screen.
.
I noticed that Base did this with no problem. But depending on the situation, it will not take into account the rectangle of the window and its predefined dimensions; any form opening will be done according to the last screen coordinates displayed. If these no longer correspond to the predefined rectangle, I will have to manually adjust the size of the window.
.
I have forms with a predefined dimension when first opened. However, I want to be able to move them, close them, and reopen them at the last viewed screen position while keeping the rectangle assigned to the form in the new location. I don’t know if this is clear, but how do you do this? Do I need global variables that will accommodate the new layout of the rectangle by changing the default coordinates? The initial rectangle must be the same no matter what is the new location.

@Renel
Look closely at code by @Villeroy.
.
With minor modification it will do what you want. You would save current location/size when closing the form (an event). Then the open event will read that saved size/position.
.
As for me I did not understand just what was wanted.

You can’t write and save the document properties unless you unlock the form document for editing. This was just a very quick solution (10 minutes) reading “hard coded” parameters in a form document. For a dynamic solution I got as far as this:

Sub onFormLoad(ev)
	frm = ev.Source
	doc = getParentObject(frm, "com.sun.star.document.OfficeDocument")
	view = doc.CurrentController
	win = view.Frame.ContainerWindow
	X = frm.getInt(1)
	Y = frm.getInt(2)
	W = frm.getInt(3)
	H = frm.getInt(4)
	Z = frm.getInt(5)
	view.ViewSettings.ZoomValue = Z
	win.setPosSize(X, Y, W, H, 15)
End Sub

Sub onFormUnload(ev)
msgbox "OK"
exit sub
	frm = ev.Source
	doc = getParentObject(frm, "com.sun.star.document.OfficeDocument")
	view = doc.CurrentController
	ps = view.Frame.ContainerWindow.getPosSize()
print ps.X, ps.Y, ps.Width, ps.Height
	frm.updateLong(1, ps.X) 
	frm.updateLong(2, ps.Y) 
	frm.updateLong(3, ps.Width) 
	frm.updateLong(4, ps.Height) 
	frm.updateLong(5, view.ViewSettings.ZoomValue) 
	frm.updateRow()
End Sub

Function getParentObject(byval m, srvName$)
	do until m.supportsService(srvName)
		m = m.getParent()
	loop
	getParentObject = m
End Function

It tries to read/write from/to a table through an invisible form (form with no controls). The invisible form is linked to SELECT * FROM WINDOWS WHERE N = ‘Left Half Zoom 80’ and the form’s load/unload events are bound to the above routines.
The name column “N” is a primary key, the other columns are integer X,Y,W,H,Z.
This time the term “form” refers to a logical form on a form document.

Problem: the second routine onForumUnLoad is never called.

Edit:
counter_WinSettings.odb (49.0 KB)

1 Like

Well, it’s a lot more complex than I thought it would be. You answer my question, but I’m wondering if I should continue with this. Everything is already working fine, so do I really need to complicate things? For now, I’ll leave that aside and focus on simpler things to do.
.
Thank you for your valuable time!

No, don’t continue working on this issue. Most Base macros are a waste of time.

Saw the same issue. I thought of using hidden controls, but this will cause an issue if saving and the user did not want something else saved.
.
Did get you code working - mostly.
.
Before/When Reloading can’t get working. However, if assigned to form event Document is going to be closed the routine is called. One further issue then occurs, ev returned is not positioned correctly and fixing that the code works.
.
counter_WinSettings.odb (49.1 KB)
.
The issue remaining is not recognizing top or bottom of screen correctly - should be 0 (top) but getting 48 (possibly due to title bar?). Left edge sets at 1 vs 0 - maybe due to frame width?

1 Like

Ratslinger,
.
Your version of counter_WinSettings.odb seems to react exactly the way I wanted. It doesn’t matter which form is open or closed, they retain their new positions and dimensions.
.
So, if I understand correctly, each form must have its hidden “WindowsSettings” form and each corresponds to a record in the “Windows” table.
.
I copied one of the two forms and identified it as “Form3”. I added a record in the Windows table and changed the text of column N. This 3rd form reacts correctly and the new positions are saved. It’s great, so I can save as many positions as I have forms. If I understand correctly, this column N, the text that is written there acts as a simple identifier and can therefore be completely modified, is that correct?
.
Just to complicate things a bit more, would it be possible for a form to open with say this position as default (1315,650,300,150) upon login and then react as expected in “counter_WinSettings.odb” until the session is closed. Obviously this for each new session.

That’s correct. You can use the form names as identifiers or you can use “role names” as identifiers for multiple forms sharing the same view settings. Just edit the WHERE clause in the form’s SQL statement to match a record in the windows table. The names of forms and the strings in the name column are both case-sensitive.

1 Like

Thank you Villeroy!
.
Currently I am trying to replicate how the windows table and invisible form work in my DB. I copied the macro into a module and assigned it to the invisible form, but so far it doesn’t seem to work.
.
The latest version of counter_WinSettings that Ratslinger gave me works flawlessly though. Regardless of the position and size of the window, the data is saved and reused.
.
I must have made a mistake somewhere.

You can upload your (erroneous) document.

Actually, if you again read my post you will see this is not correct. Cannot align to top/bottom correctly and to a much lesser degree the sides. Further testing shows the vertical positioning worsens with each closing.
.

.
This is not very complex code. If this worries you as you state:
.

.
Then why use it? I only continued the sample out of curiosity.
Further you ask:
.

.
Yes. On opening the .odb (Base file) use an event to run a macro to update the table records for those forms you wish opened first time with specific location, size. SQL execution in a macro. Many posts already with samples of this. Try searching this site for SQL macro.

Ratslinger,
.
There’s one thing I can’t quite grasp. In the version you sent me, only the Basic procedure “onFormLoad(ev)” is assigned to the invisible form “WindowSettings” at the “On Loading” event.
.
Nowhere is there any “onFormUnload(ev)” procedure assignment and yet when I click the close X on the window or select ‘Close’ from the menu bar, your ‘Print’ command of that procedure is displayed and the rest of the procedure is executed by clicking OK. This procedure is bound to be called somewhere. How could a precedure execute without being called?
.
Please forgive my total ignorance here


@Renel
.
You are not reading my post information completely. Stated:

I read, but I don’t understand. What is the logic behind this?

No logic. It is the event that the macro is attached to. The original events used do not work (mentioned by Villeroy and myself). Found the form event to close document works with slight variation.

.
what event and where?