Page 1 of 1
MDI Child Window
Posted: Mon Nov 24, 2003 2:59 am
by Codemonger
I was looking for examples of how to get a MDI child window working and didn't find anything that was convincing ... anyway I went searching on the
http://PureArea.net and found a fantastic userlib thats called 'WindowEx' written by Roger Beausoleil.
Anyway I first could not get anything working properly then finally .. Heres the code to simple to read and simple to use ... no crazy code here:
Code: Select all
; ------------------------------------
; WindowEx Library Written by Roger Beausoleil
; Example of MDI by Codemonger
; ------------------------------------
Enumeration
#MainWindow
#ChildWindow
EndEnumeration
;//Create the Parent Window
OpenWindow( #MainWindow,0,0,640,480,#PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Main Window")
;//Create the Child
OpenWindow( #ChildWindow,10,10,200,275,#PB_Window_SystemMenu,"Child Window")
;// Get the styles of the child window
ChildStyle.l = WindowStyles( #ChildWindow )
ChildEXStyle =WindowExStyles( #ChildWindow )
;// Set the style of the child window and set the parent window
SetWindowStyles( #ChildWindow, ChildStyle.l, ChildEXStyle.l | #WS_EX_MDICHILD)
SetWindowParent( #ChildWindow, #MainWindow)
Repeat: Until WaitWindowEvent() = #PB_EventCloseWindow : End
Posted: Mon Nov 24, 2003 3:18 am
by Karbon
That's pretty nice!
Posted: Mon Nov 24, 2003 11:28 am
by einander
Thanks Codemonger!
Long time waiting for this.
Now, with a little addition, is possible to resize the child window related to the parent.
Code: Select all
; ------------------------------------
; WindowEx Library Written by Roger Beausoleil
; Example of MDI by Codemonger
; Resized Child by Einander
; ------------------------------------
Enumeration
#MainW
#Child1
EndEnumeration
Procedure WindowCallback(0, Msg, lParam, wParam)
If Msg=#WM_SIZE
UseWindow(#mainw)
WI=WindowWidth():HE=WindowHeight()
UseWindow(#child1)
ResizeWindow(Wi/1.5, He/1.5)
Box(2,2,Wi-4,He-4,#RED)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WI=640:HE=480
OpenWindow( #MainW,0,0,WI,HE,#PB_Window_SystemMenu | #PB_Window_ScreenCentered|#WS_OVERLAPPEDWINDOW, "Main Window")
OpenWindow( #Child1,50,50,WI/1.5,HE/1.5,#PB_WINDOW_BORDERLESS| #WS_HSCROLL, "")
ChildStyle.l = WindowStyles( #Child1 )
ChildEXStyle =WindowExStyles( #Child1)
SetWindowStyles( #Child1, ChildStyle.l, ChildEXStyle.l | #WS_EX_MDICHILD)
SetWindowParent( #Child1, #MainW)
StartDrawing(WindowOutput())
SetWindowCallback(@WindowCallback())
Repeat
EV=WaitWindowEvent()
If EV=#PB_Event_Repaint
UseWindow(#child1)
Box(2,2,Wi-4,He-4,#RED)
EndIf
Until EV = #PB_EventCloseWindow
End
Posted: Mon Nov 24, 2003 6:53 pm
by fsw
Sorry, but what you have is NOT real MDI
Add a menu to the ParentWindow and change the Child Styles to be able to maximize the ChildWindow.
You can see that the ChildWindow Titlebar is not moved into the MenuArea of the Parent Window.
If you need MDI search the forum for EL_CHONI'S work on this (looong time ago).
He did a great job back than. (Maybe his code needs to be modified for todays PureBasic)
Or maybe you can mix El_Chonis work with Rogers Library...
Posted: Mon Nov 24, 2003 8:32 pm
by einander
[quote="fsw"]Sorry, but what you have is NOT real MDI
Thanx fsw!
My point was not MDI, and I don't want the Child window maximized; only want to move it with the Parent.
After searching the forums with no luck for a way to have a Child window ralated to the position of the Parent, the example was a simple way to do that.
Better examples are welcomed!
Posted: Tue Nov 25, 2003 1:07 am
by Kale
Have you seen this:
viewtopic.php?t=3706 
Posted: Tue Nov 25, 2003 1:41 am
by Codemonger
That is the worst peice of code I have seen for an example, it looks hacked to death. I went looking for MDI examples and that is exactly what I stumbled on too and immediately ran away from ...
The above code that I posted creates the Framework for MDI, how you work with it beyond that is up to you ... so go crazy and create menus and all sorts of junk. Certainly it can be rifined to do other things that are needed.
Posted: Tue Nov 25, 2003 2:31 pm
by freak
Sorry Codemonger, but actually your code is a bad hack, El_Choni's is
a quite solid, well done piece of code.
MDI is a bit more than just sticking the #WS_EX_MDICHILD to the window
flags. As you can see in El_Choni's code, you also have to do a different
Message processing than the standart PB one.
Timo
Posted: Tue Nov 25, 2003 3:09 pm
by Psychophanta
Nice aclaration Timo.
Here is what einander wanted to do with Codemonger post code:
Code: Select all
Enumeration
#MainWindow
#ChildWindow
#MainWindowWidth=640
#MainWindowHeight=480
#ChildWindowX=10
#ChildWindowY=10
#ChildWindowWidth=200
#ChildWindowHeight=275
#WM_ENTERSIZEMOVE=561
#WM_EXITSIZEMOVE
EndEnumeration
Global x,y,w,h,ww,hh
x=#ChildWindowX
y=#ChildWindowY
ww=#MainWindowWidth
hh=#MainWindowHeight
w=#ChildWindowWidth+8
h=#ChildWindowHeight+29
Procedure.l WindowCallBack(WindowID.l,Message.l,wParam.l,lParam.l)
Result.l=#PB_ProcessPureBasicEvents
If WindowID=WindowID(#MainWindow)
If Message=#WM_SIZE:ww=lParam&$FFFF:hh=lParam>>16
MoveWindow_(WindowID(#ChildWindow),ww*x/#MainWindowWidth,hh*y/#MainWindowHeight,ww*w/#MainWindowWidth,hh*h/#MainWindowHeight,#TRUE)
Result=0
EndIf
ElseIf WindowID=WindowID(#ChildWindow)
If Message=#WM_EXITSIZEMOVE
UseWindow(#ChildWindow):x=WindowX()-4:y=WindowY()-23
w=WindowWidth()+8:h=WindowHeight()+29
w*#MainWindowWidth/ww:h*#MainWindowHeight/hh
UseWindow(#MainWindow):x-WindowX():y-WindowY()
x*#MainWindowWidth/ww:y*#MainWindowHeight/hh
Result=0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
;//Create the Parent Window
OpenWindow(#MainWindow,0,0,#MainWindowWidth,#MainWindowHeight,#PB_Window_SizeGadget|#PB_Window_SystemMenu|#PB_Window_ScreenCentered, "Main Window")
;//Create the Child
OpenWindow(#ChildWindow,#ChildWindowX,#ChildWindowY,#ChildWindowWidth,#ChildWindowHeight,#PB_Window_SizeGadget|#PB_Window_SystemMenu,"Child Window")
;// Get the styles of the child window
ChildStyle.l=WindowStyles(#ChildWindow)
ChildEXStyle=WindowExStyles(#ChildWindow)
;// Set the style of the child window and set the parent window
SetWindowStyles( #ChildWindow, ChildStyle.l, ChildEXStyle.l | #WS_EX_MDICHILD)
SetWindowParent( #ChildWindow, #MainWindow)
SetWindowCallback(@WindowCallback())
Repeat: Until WaitWindowEvent() = #PB_EventCloseWindow
If Debugger is disabled, possible flickering goes away

Posted: Tue Nov 25, 2003 4:54 pm
by einander
Thanks you Psychophanta
Ive replaced the style of the child window for #PB_WINDOW_BORDERLESS | #WS_HSCROLL
and it's exactly what I need.
Posted: Tue Nov 25, 2003 5:02 pm
by Codemonger
Point well taken guys.
@Psychophanta - Nice Addition
@Freak
Heres an example of a for .. next loop from the documentation
Code: Select all
Example 1 :
For k = 0 To 10
...
Next
In this example, the programme will loop 11, time (0 to 10), then quit.
Most people learn by 'example', as you notice the For ... Next loop has nothing in it but defines a for ... next loop very well. If someone were to define a for next loop with this example they could easily do it. If I start adding variables in the middle of the code and even start opening some windows like this example...
Code: Select all
Example 1 :
If OpenWindow(0, 200, 200, 200, 100, #PB_Window_SystemMenu, "Menu Example")
If CreateMenu(0, WindowID()) ; here the menu creating starts....
MenuTitle("Project")
MenuItem(1, "Open" +Chr(9)+"Ctrl+O")
MenuItem(2, "Save" +Chr(9)+"Ctrl+S")
MenuItem(3, "Save as"+Chr(9)+"Ctrl+A")
MenuItem(4, "Close" +Chr(9)+"Ctrl+C")
EndIf
Title$ [, ParentWindowID])
For k = 0 To 10
...
Next
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
In this example, the programme will loop 11, time (0 to 10), then quit.
That's what I call hacked code, it's someones idea of what they are trying to do, and usually 90% of it is irrelevant for what they are explaining.
If you had no idea what a computer was and I explained that a "Pentium is a computer" (not a lie), and asked you in return what a computer is ? you would reply "a Pentium". It is not a very accurate definition, but nontheless untrue.
So I think my intention is more than accurate and the title of El_Choni's post is "PB Windows MDI template (API bloated)", so I guess his intention was right on the mark. The definition of MDI, is to have child windows within a parent window (without going into detail etc..). One thing you leave out is maybe someone would like to move a MDI window from outside the main window as if it was sliding in ... putting pre-defined limitations on code is not MDI, it is only an addition.
Going through the code in the link to El_Choni's code I found the first 200 Lines of code to have nothing to do with MDI window, so how is his code rock solid ? Anyway no offense to El_Choni on his code, as it is his code and it works for what he wants. Obviously I'm coming from the perspective of someone that simply wan'ts to create an MDI window or even have access to extended window styles. Anyway thanks guys for the feedback as a debate is always welcome.

Posted: Tue Nov 25, 2003 5:42 pm
by Fred
I think you misunderstand Freak, as MDI is a whole, and not only a child window. MDI requiers automatic Menu handling, unlimited area to place the child windows in, the window arrange facilities, the icon when maximizing, etc... El Choni's template is a basic MDI application nothing more and you get started immediately, and franky, his code is very good. I'm of course not telling you than your example is bad or something, just want give my point about this (complex) MDI problem.