Checking the Resize Window Event

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Joseph.

Hello all
i have tried to add the 'size' flag to a window

Code: Select all

If OpenWindow(0, 200, 200, 195, 260, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "PureBasic Window")

...
and then I have tried (but no success) to check its supposed EventiID inside the main loop

Code: Select all

If EventID = #PB_Event_SizeWindow  

      ;do_something

    EndIf
(infact, even if not mentioned in the documentation it seems to me that the #PB_Event_SizeWindow constant is recognized by PureBasic)

I have found a resizing window sample in the excellent PureBasic resource site but I have noticed that it basically uses a comparison between the stored previous dimensions of the window and the actual ones outside the main loop, I'd like instead to check, if possibile, the resing EventID, something similar to when you close the window or move it...

any tip/help will be greatly appreciated

thanks in advance

best regards
Joseph
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

Hi Joseph,

You can use WM_SIZE message

Code: Select all

Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
    Select message
    Case #WM_SIZE 
      beep_(50,50)
    EndSelect
  ProcedureReturn Result 
EndProcedure 

hwnd= OpenWindow(1, 200, 200, 673, 155 ,#PB_Window_SystemMenu | #PB_Window_SizeGadget |#PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget,"test")

If hwnd
  SetWindowCallback(@MyWindowCallback()) 
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow 
EndIf

End   
Regards,

Benny


http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I have tried [...] to check its supposed EventiID inside the main loop

Check for #WM_SIZE as shown below... (no need to use a callback):

Code: Select all

If OpenWindow(0,200,200,450,200,#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget,"Test")
  CreateGadgetList(WindowID())
  TextGadget(0,50,50,100,20,"Resize this window...")
  Repeat
    ev=WaitWindowEvent()
    If ev=#WM_SIZE ; Window's size has just changed.
      MessageBeep_(#MB_ICONASTERISK) ; Play chime.
    EndIf
  Until ev=#PB_EventCloseWindow
EndIf

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.
Check for #WM_SIZE as shown below... (no need to use a callback):
Ofcourse, a callback is not necesary, but the sample was on my HD.

I always use Callback, just a personal way of coding. :)

Benny

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
I always use Callback, just a personal way of coding. :)
Well it does not need to be a personal way of coding. I found out on this forum a long time ago (6 months?) that my GUI code looked like crap when you ran it as I checked for a resize in the normal event handler loop. This meant your application only gets told about the resize once it is over.

If you check for a resize in a callback, you can resize gadgets as the window is being resized (which makes your apps look more like normal Windows apps and less like crap :wink:.

Unless the messaging behaviour in PB has changed since then?


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

Thats it Tinman,
(which makes your apps look more like normal Windows apps and less like crap :wink:.

Unless the messaging behaviour in PB has changed since then?
It is not changed , V3.3 makes a mess of your app if you resize the window. Especialy when 'Show window contents while dragging' is on.

This is a problem a ran in the first week a used PB, using callback always now.
Also GFA Basic 32 had some message problems, only in a callback, you could monitor all messages.
Seems a common problem in basic languages

Regards,

Benny


http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Benny Sels

It is not changed , V3.3 makes a mess of your app if you resize the window. Especialy when 'Show window contents while dragging' is on.
Yep, that's the one :)
This is a problem a ran in the first week a used PB, using callback always now.
Also GFA Basic 32 had some message problems, only in a callback, you could monitor all messages.
Seems a common problem in basic languages
Obviously when you are doing a "normal" message handler loop with these basics, you rely on the code that the author has put into the libraries.

I'm no expert on the Windows API, but it seems like PB works the way Windows lets you - normally you would only get told about the final message (after processing, i.e. for gadgets or OS stuff etc), or you can install a callback that Windows will call automatically for all events that are generated.

Having two separate sizing messages (1 during sizing, 1 at the end) can be a good thing sometimes so I should not complain too much :)

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Hello,

I am not sure that callbacks are Basic issues related ... in fact I am sure it is not, but because you are using windows and you want to design something using events, it is necessary to have messages so that the operating system can handle all the stuff you have.

This is so with MS Windows, but looking X-Window you will also find low level events and messages handling that have the same look as far as I know.

When coding an application with c or cpp language it is the same. And if you use a high level language that does not make necessary to know about callbacks, this is because it generates it behind the scenes. But in this case, you should notice that you have not so much possibilities to design your own objects.

In this way, coding with PureBasic shows how this language has a large ability to let you decide if you want to code at low or high level.

So far, you can check a window user sizing using WindowWidth() WindowHeight() in the main event loop to check if it has changed since last event, or you can check #WM_SIZE event as well. Depending on the use of WindowEvent() or WaitWindowEvent() will make different possible behaviours.

But you can also put it in a callback for deeper process if you prefer to put high level coding in the main part of your application and let a callback process low level. This is a good way to do because a callback is a 'near real-time' processing that you do not have to handle in the main part as far as you decide to do so.

I just want try to make it clear to you that callbacks are not dark coding ...

Rgrds

Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I always use Callback, just a personal way of coding.

Hi Benny, I didn't mean any offense when I said "no need for a callback".
Like you, it was just my own personal way of doing a resize example.

> V3.3 makes a mess of your app if you resize the window.
> Especialy when 'Show window contents while dragging' is on.

I don't understand... I have "Show window contents when dragging" on
and v3.30 doesn't mess up my windows when I resize them without using
a callback... maybe it's just your system. :)


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
> V3.3 makes a mess of your app if you resize the window.
> Especialy when 'Show window contents while dragging' is on.

I don't understand... I have "Show window contents when dragging" on
and v3.30 doesn't mess up my windows when I resize them without using
a callback... maybe it's just your system. :)
Any chance you could share your code? What I find happens is that as the user resizes the window, the window changes size. However, the gadgets inside the window do not, so you either get a large space between gadgets and window border (if making the window bigger) or the edges of gadgets get cut off (if making the window smaller).



--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Any chance you could share your code? What I find happens is that
> as the user resizes the window, the window changes size. However,
> the gadgets inside the window do not, so you either get a large
> space between gadgets and window border (if making the window
> bigger) or the edges of gadgets get cut off (if making the window
> smaller).

This is all I'm doing to resize windows perfectly with no problems.
As mentioned, I don't use callbacks, but I may do so in the future.

Code: Select all

Form1_W=312 ; Form's client width.
Form1_H=213 ; Form's client height.
Form1_X=(GetSystemMetrics_(#SM_CXSCREEN)-Form1_W)/2 ; Centered horizontally.
Form1_Y=(GetSystemMetrics_(#SM_CYSCREEN)-Form1_H)/2 ; Centered vertically.
;
#Form1_Flags=#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_SystemMenu
Form1_hWnd=OpenWindow(0,Form1_X,Form1_Y,Form1_W,Form1_H,#Form1_Flags,"Form1")
If Form1_hWnd=0 Or CreateGadgetList(Form1_hWnd)=0 : End : EndIf
;
Form1_OrigW=WindowWidth()  ; Original non-client width.
Form1_OrigH=WindowHeight() ; Original non-client height.
;
#Form1_Command1=1 : Form1_Command1_hWnd=ButtonGadget(#Form1_Command1,8,8,81,33,"Command1")
#Form1_Command2=2 : Form1_Command2_hWnd=ButtonGadget(#Form1_Command2,224,8,81,33,"Command2")
#Form1_Command3=3 : Form1_Command3_hWnd=ButtonGadget(#Form1_Command3,120,88,81,33,"Command3")
#Form1_Command4=4 : Form1_Command4_hWnd=ButtonGadget(#Form1_Command4,8,176,81,33,"Command4")
#Form1_Command5=5 : Form1_Command5_hWnd=ButtonGadget(#Form1_Command5,224,176,81,33,"Command5")
;
Repeat
  Form1_EventID=WaitWindowEvent()
  Select Form1_EventID
    Case #WM_SIZE ; Form's size has changed.
      Form1_RatioW.f=WindowWidth()/Form1_OrigW ; Get horizontal difference.
      Form1_RatioH.f=WindowHeight()/Form1_OrigH ; Get vertical difference.
      ShowWindow_(Form1_hWnd,#SW_HIDE) ; To prevent graphics corruption.
      ResizeGadget(#Form1_Command1,8*Form1_RatioW,8*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command2,224*Form1_RatioW,8*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command3,120*Form1_RatioW,88*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command4,8*Form1_RatioW,176*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command5,224*Form1_RatioW,176*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ShowWindow_(Form1_hWnd,#SW_SHOW)
  EndSelect
Until Form1_EventID=#PB_EventCloseWindow
;
End

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

@PB: Your Window doesnt resize correctly.

You resize it after the mousebutton has been released,
but with a callback you get "resize on the fly".
Every good Windows application can resize on-the-fly,
so better use a callback.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.
Originally posted by PB
This is all I'm doing to resize windows perfectly with no problems.
As mentioned, I don't use callbacks, but I may do so in the future.
Danilo hit 'Post Reply" before me
With a CallBack, gadgets are resized while changing the size of the window.
Just changed PB's code to use CallBack.

Code: Select all

Form1_W=312 ; Form's client width.
Form1_H=213 ; Form's client height.
Form1_X=(GetSystemMetrics_(#SM_CXSCREEN)-Form1_W)/2 ; Centered horizontally.
Form1_Y=(GetSystemMetrics_(#SM_CYSCREEN)-Form1_H)/2 ; Centered vertically.
;
Global Form1_hWnd,Form1_OrigW,Form1_OrigH

#Form1_Flags=#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_SystemMenu
Form1_hWnd=OpenWindow(0,Form1_X,Form1_Y,Form1_W,Form1_H,#Form1_Flags,"Form1")
If Form1_hWnd=0 Or CreateGadgetList(Form1_hWnd)=0 : End : EndIf
;

Form1_OrigW=WindowWidth()  ; Original non-client width.
Form1_OrigH=WindowHeight() ; Original non-client height.
;
#Form1_Command1=1 : Form1_Command1_hWnd=ButtonGadget(#Form1_Command1,8,8,81,33,"Command1")
#Form1_Command2=2 : Form1_Command2_hWnd=ButtonGadget(#Form1_Command2,224,8,81,33,"Command2")
#Form1_Command3=3 : Form1_Command3_hWnd=ButtonGadget(#Form1_Command3,120,88,81,33,"Command3")
#Form1_Command4=4 : Form1_Command4_hWnd=ButtonGadget(#Form1_Command4,8,176,81,33,"Command4")
#Form1_Command5=5 : Form1_Command5_hWnd=ButtonGadget(#Form1_Command5,224,176,81,33,"Command5")
;

Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
    Select message
    Case #WM_SIZE ; Form's size has changed.
      
      Form1_RatioW.f=WindowWidth()/Form1_OrigW ; Get horizontal difference.
      Form1_RatioH.f=WindowHeight()/Form1_OrigH ; Get vertical difference.
      
      ResizeGadget(#Form1_Command1,8*Form1_RatioW,8*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command2,224*Form1_RatioW,8*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command3,120*Form1_RatioW,88*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command4,8*Form1_RatioW,176*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      ResizeGadget(#Form1_Command5,224*Form1_RatioW,176*Form1_RatioH,81*Form1_RatioW,33*Form1_RatioH)
      
    EndSelect
  ProcedureReturn Result 
EndProcedure 


SetWindowCallback(@MyWindowCallback())
Repeat
  WaitWindowEvent()
Until WaitWindowEvent()=#PB_EventCloseWindow
;
End
Regards,

Benny


http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.
Originally posted by fweil
I just want try to make it clear to you that callbacks are not dark coding ...
That's wright fweil, It is just a little bit more advanced way of coding.
Just a little bit more control over the code, especialy when usig API.
Easy to convert from C++ etc...

Regards,

Benny

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Just changed PB's code to use CallBack.

Thanks, Benny. I've reposted your changes to my original tip in the
"Tips 'N' Tricks" section, along with a credit to you. :)


PB - Registered PureBasic Coder
Post Reply