MDI Child Window

Share your advanced PureBasic knowledge/code with the community.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

MDI Child Window

Post 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
    
Last edited by Codemonger on Mon Nov 24, 2003 3:36 am, edited 1 time in total.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

That's pretty nice!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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 
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post 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...
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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!
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Have you seen this: viewtopic.php?t=3706 :D
--Kale

Image
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Kale wrote:Have you seen this: viewtopic.php?t=3706 :D
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.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
freak
PureBasic Team
PureBasic Team
Posts: 5950
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :wink:
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post 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. :o
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Fred
Administrator
Administrator
Posts: 18390
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Post Reply