Is it possible to open and resize a form dynamically?

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?

Have stated multiple times. Broken down:

Form Event

.
Document is going to be closed
.
Not the internal form but the form event:

1 Like

Phew! Are there other mysterious places like this in this software. So I can deduce that anything related to the form in the document and not the forms in the form, that Customize menu is where things happen. Also, part of my misunderstanding is that I didn’t understand the subtlety between Document event of a form and Form Event of a form.
.
OK, you’re probably going to tell me Read the manual, but please be lenient.
.
Alright, everything works. All I have to do is disable the MsgBox.
.
As for opening with defined default window dimensions, I will do a search on this site as you suggested.
.
This time again, I have to thank you and Villeroy. Both of you have contributed greatly. But it’s still you who brought the elements making everything work as I wanted.

Sorry, but do not know where you got that. The image is for Events.
.

Yes. One that comes to mind is text box.
.

.
Without at least skimming through documentation, your lack of knowledge of the terminology used is going to be a hindrance.

Hello Villeroy,
.
I am rereading the comments and indications that you and Ratslinger gave me following the question that I had asked.
.
In your file, “counter_2022.odB”, you mentioned that you stored the position, size and zoom in the form’s user-defined properties.
.
I was able to read these coordinates in the udp variable used by the macro you wrote. These are : H=800, W=800, X=0, Y=50, Z=80.
.
I’m trying to access these user-defined properties, but can’t see how. I looked in the manual and when we talk about property, it is always associated with existing elements in a form or the properties of the forms inside the form.
.
Please, can you tell me where these coordinates are so that I can visualize things?

@Renel
.
This is the heart of the code in my sample (originally by Villeroy) in Sub onFormUnload(ev)

    frm = ev.Source.DrawPage.Forms.getByName("WindowSettings")

	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) 

.
You get the document, the CurrentController, the Frame, the ContainerWindow and there you obtain:

getPosSize()

in the code that is now ps. So in ps get:

X = ps.X
Y = ps.Y
W = ps.Width
H = ps.Height

Also once you have the CurrentController (view in this case), you can get ViewSettings and then get ZoomValue.
.
Edit:
.
Examining the internals can be done with a tool such as MRI. For more information on this see the links within this post → How to Get and Put data from/to form fields with a macro
.
There is a bunch of info out there (and posted within this site). Got to search and read - a lot.

Ok, I understand that with macros it is essential to have a very good understanding of the Application Programming Interface.
.
I also understand why Villeroy seems to encourage me to invest in understanding the SQL language rather than investing my time in macros. API seems a complex and obscure world for a neophyte like me…
.
I will follow the link you suggested anyway. Thanks!