• Welcome to PlanetSquires Forums.
 

Using CLayout manager in a Designer project

Started by philbar, May 27, 2022, 03:39:11 AM

Previous topic - Next topic

philbar

As promised in the other thread: Many thanks to José for this gem that can save a lot of trouble.

The attached picture shows the window as designed with two text boxes, a frame and a list. In the main.bas program, don't forget:

#include "afx\clayout.inc"

And this is the form.inc code:

' frmMain form code file
''
''
''These constants are enumerated in afx/clayout.inc
'   AFX_ANCHOR_NONE =  0
'   AFX_ANCHOR_WIDTH
'   AFX_ANCHOR_RIGHT
'   AFX_ANCHOR_CENTER_HORZ
'   AFX_ANCHOR_HEIGHT
'   AFX_ANCHOR_HEIGHT_WIDTH
'   AFX_ANCHOR_HEIGHT_RIGHT
'   AFX_ANCHOR_BOTTOM
'   AFX_ANCHOR_BOTTOM_WIDTH
'   AFX_ANCHOR_BOTTOM_RIGHT
'   AFX_ANCHOR_CENTER_HORZ_BOTTOM
'   AFX_ANCHOR_CENTER_VERT
'   AFX_ANCHOR_CENTER_VERT_RIGHT
'   AFX_ANCHOR_CENTER

dim shared as clayout ptr playout         'A shared pointer so it can be seen in all functions
dim shared as boolean form_ready=false    'Prevents a hard-to-debug crash

Function frmMain_Load( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   dim as long ngrip=frmmain.getnextctrlid()
   
   playout = new clayout(frmmain.hwindow)    'assigns a layout manager to this window
   
   'This is optional. Adds the little gripper spot to the bottom right corner.
   frmmain.pwindow->addcontrol("SizeGrip", frmmain.hWindow, ngrip)
   
   'Decide how each control is to be sized
   playout->anchorcontrol(frmmain.text1.hwindow, afx_anchor_width)
   playout->anchorcontrol(frmmain.text2.hwindow, afx_anchor_height_width)
   playout->anchorcontrol(frmmain.frame1.hwindow, afx_anchor_height_right)
   playout->anchorcontrol(frmmain.list1.hwindow, afx_anchor_height_right)
   playout->anchorcontrol(ngrip, afx_anchor_bottom_right)
   Function = 0
End Function

''
''
Function frmMain_Resize( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   if form_ready then            'This prevents the crash.
      playout->adjustcontrols    'This does the resizing     
   end if
   Function = 0
End Function

''
''
Function frmMain_FormClosed( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   delete playout       'Wouldn't want a memory leak
   Function = 0
End Function

''
''
Function frmMain_AllEvents( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   'This part taken from José's example layout manager programs.
   'Who knew there was such a thing?
   'I could have just put a test in the RESIZE event.
   if e.Message = wm_getminmaxinfo then
      dim ptmmi as MINMAXINFO ptr = cast(minmaxinfo ptr, e.lParam )
      ptmmi->ptmintracksize.x = 300
      ptmmi->ptmintracksize.y = 180
   end if
   Function = 0 
End Function

''
''
Function frmMain_FormReady( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   'Sometimes a Resize event happens before Form_Load is done.
   'That causes a nasty crash.
   form_ready=true
   Function = 0
End Function


I think the comments explain what's going on. The minimum size of 300x180 is a nice touch taken from the examples. Check out the layout manager examples in the templates for more complex behavior. It's possible to put layouts inside layouts.

Petrus Vorster

Thank you for the example mate, it is much appreciated.

I do get errors right of the bat.
The include of the clayout.inc is fine.

The assignment of the pointer throws an error:
dim shared as clayout ptr playout
Throws the error : ERROR 14: Expected identifier, found 'clayout'

I do see where this is going though.

But I think i am going to wait until Paul adds it to his designer.
That automation made it a breeze.

Thanks for the example.

Peter
-Regards
Peter

philbar

Hmm. Works for me. I do get that error, among many others, if I leave out the #include.

SeaVipe

Thanks, Philbar! Worked first time, no errors.
Clive Richey

Petrus Vorster

#4
Yes it works just fine if the moron that pasted the info removes a rem comma....
I can be a real idiot.

Dear Heavens.  :-[
-Regards
Peter

Petrus Vorster

Great stuff. It works perfectly when I change the form size, use the maximize button and so forth.

But when i start the form using formwindowstate.maximized then it completely messes up the controls.
In Powerbasic the Maximized form worked under the form_resize function.
It does not seem to be the case in Freebasic.

-Regards, Peter
-Regards
Peter

philbar

I've never tried it on a form that starts out maximized. Maybe if you start out regular and maximize it in Form_Load after the layout manager has initialized?

Petrus Vorster

Hi

That works fine if i do that, I was just wondering why the Maximize startup of the form does not fire the form-resize event.
Unless the Maximize startup runs before some of the other events.

Regards, Peter
-Regards
Peter

SeaVipe

QuoteBut when i start the form using formwindowstate.maximized then it completely messes up the controls.
FormWindowState.Normal is the only state that appears to work without messing up the control sizes and positions.
Clive Richey

philbar

After some experimenting, I got the result I expected when I started out normal and put "frmmain.windowstate = maximized" at the end of the frmmain_FormReady event.

Petrus Vorster

#10
Yes, that works perfectly.

Thanks a million.
-Regards
Peter