PDA

View Full Version : The importance of IsWindow()



marcuslee
02-09-2008, 15:57
Hey, just found ThinBasic the other day, have been looking over things. I like it so far. I've used Liberty Basic in the past, but I still consider myself a beginner (perhaps advanced beginner would be appropriate). Anyway!

In my searching I've seen the IsWindow() function used a lot. I was wondering what the importance of it was. I understand the code (methinks) in that continues to perform the actions while a certain handle is a window. But, I am confused as to why you need it. Why would the handle not be a window if you assigned it as a window?

Mark ???

ErosOlmi
02-09-2008, 16:21
Hi marcuslee,

first of all welcome to thinBasic community forum.

Despite its name, IsWindow is not checking if the handle is a window but if the handle is still a valid window existing in the system (the operating system).
IsWindow is used to check if the message queue is still valid to be processed for that window.

In any case, all this is changing. If you have followed latest discussions on next thinBasic 1.7.0.0 (coming out in few days) next thinBasic will have callbacks functions to handle window and control messages. So the WHILE IsWindow ... WEND loop will have no more sense (even if still supported). Mainly you will tell a window or a control which is the function that will handle its events. The operating system will call that function for all events fired for a particular window or control.

To give you a simple example of the new syntax showing some of the events you can trap, see the following:


'------------------------------------------------------------------------------
' Program to show dialogs and buttons callbacks
'------------------------------------------------------------------------------

uses "UI"
uses "console" '---Console is used to easily output events info

'---Constant declarations
begin const
%ID_Button_01 = %IDOK
%ID_Button_02
end const

TYPE RECT
nLeft AS LONG
nTop AS LONG
nRight AS LONG
nBottom AS LONG
END TYPE

'------------------------------------------------------------------------------
' Program start point
'------------------------------------------------------------------------------
FUNCTION TBMain() as long
local hDlg AS DWORD '---Used to store window handle of main dialog

'---Create a new dialog
DIALOG NEW pixels, 0, "thinBasic test button callback", -1, -1, 300, 200, _
%WS_DLGFRAME | _
%DS_CENTER | _
%WS_CAPTION | _
%WS_SYSMENU | _
%WS_OVERLAPPEDWINDOW, 0 TO hDlg

'---Set window minimum size
DIALOG SET MINSIZE hDlg, 300, 200

'---Show dialog in modal mode
'---cbDialog function is the callback function handling dialog events
'---Application control will pass to dialog callback till dialog will exists
DIALOG SHOW modal hDlg, call cbDialog

'---If execution comes here it means main dialog as been destroyed
printl "---Application finished. Press a key to end---"
waitkey

END FUNCTION

'------------------------------------------------------------------------------
' Callback procedure for main window
'------------------------------------------------------------------------------
callback FUNCTION cbDialog() as long

'printl CBHNDL, CBCTL, CBCTLMSG, CBLPARAM, CBWPARAM

select case cbmsg

case %WM_CREATE
printl time$, "Fired %WM_CREATE dialog message"

case %WM_INITDIALOG
printl time$, "Fired %WM_INITDIALOG dialog message"
'---Add controls
control add button, cbhndl, %ID_Button_01, "I'm the button 1" , 100, 50, 100, 35, %BS_NOTIFY | %WS_TABSTOP | %BS_DEFAULT, call cbButton
control add button, cbhndl, %ID_Button_02, "I'm the button 2" , 100, 100, 100, 35, %BS_NOTIFY | %WS_TABSTOP, call cbButton

Case %WM_COMMAND
printl time$, "Fired %WM_COMMAND dialog message", CbCtl

case %WM_SIZE '---The WM_SIZE message is sent to a window after its size has changed.
printl time$, "Fired %WM_SIZE dialog message", cbwparam, lowrd(cblparam), hiwrd(cblparam)

case %WM_SIZING '---The WM_SIZING message is sent to a window that the user is resizing.
printl time$, "Fired %WM_SIZING dialog message", cbwparam, lowrd(cblparam), hiwrd(cblparam)

'case %WM_MOVING
' local RC as RECT ptr
' RC = cblParam
' printl time$, "Fired %WM_MOVING dialog message", RC.nLeft, RC.nTop, RC.nRight, RC.nBottom

case %WM_MOVE
printl time$, "Fired %WM_MOVE dialog message", lo(integer, cblparam), hi(integer, cblparam)

case %WM_DESTROY
'---Do whatever needed just before dialog is destroyed.
printl time$, "Fired %WM_DESTROY dialog message"

end select

END FUNCTION

'------------------------------------------------------------------------------
' Callback procedure for button control
'------------------------------------------------------------------------------
callback function cbButton() as long

if cbmsg = %WM_COMMAND then

select case CBCTLMSG
case %BN_CLICKED
select case CBCTL
case %ID_Button_01
printl time$, "Button 1 clicked"
case %ID_Button_02
printl time$, "Button 2 clicked"
end select
case %BN_DISABLE
select case CBCTL
case %ID_Button_01
printl time$, "Button 1 disable"
case %ID_Button_02
printl time$, "Button 2 disable"
end select
case %BN_KILLFOCUS
select case CBCTL
case %ID_Button_01
printl time$, "Button 1 kill focus"
case %ID_Button_02
printl time$, "Button 2 kill focus"
end select
case %BN_SETFOCUS
select case CBCTL
case %ID_Button_01
printl time$, "Button 1 set focus"
case %ID_Button_02
printl time$, "Button 2 set focus"
end select
end select

'---If button callback does not return %TRUE, message is passed
'---to parent window
function = %TRUE

end if
end function


We are beta-testing new thinBasic 1.7.0.0 before final version will be out.
If you want to get the download url where to get thinBasic preview version 1.7.0.0 just send me a personal massage here in forum and will send you the link.

Discussion on beta testing is going in the following forum: http://community.thinbasic.com/index.php?board=166.0

Ciao
Eros

marcuslee
02-09-2008, 16:58
Despite its name, IsWindow is not checking if the handle is a window but if the handle is still a valid window existing in the system (the operating system).
IsWindow is used to check if the message queue is still valid to be processed for that window.


So, under the current release, if you leave out the IsWindow() function, what will happen? Errors? Program stop running? Unsure of code execution?


And, under the new release, instead of program flow continuing after the DIALOG SHOW command, you pass it to a function, which in your example is where you create the button. Do you have to create the button there or can you do it before you show the dialog as I understand it to be right now?

I haven't browsed through the new stuff yet. There's too much here! :o I'm getting there, though. ;)


Mark

ErosOlmi
02-09-2008, 17:08
So, under the current release, if you leave out the IsWindow() function, what will happen? Errors? Program stop running? Unsure of code execution?


Nothing special. Without a while/wend you will just process the first message.



And, under the new release, instead of program flow continuing after the DIALOG SHOW command, you pass it to a function, which in your example is where you create the button. Do you have to create the button there or can you do it before you show the dialog as I understand it to be right now?

The operating system will automatically call the designated callback function for the relevant window or control. You just indicate which is the function (if any) in place for handling events of that particular window or control.

Controls must be created after a DIALOG NEW ... has been fired. Every control need a dialog where to be placed.
You can do it just after DIALOG NEW ... statement or you can do it later (usually done when %WM_INITDIALOG message is fired
You can also create/kill/hide/show controls during your program execution (even if not much elegant from a user interface point of view).



I haven't browsed through the new stuff yet. There's too much here! :o I'm getting there, though. ;)

The only thing I can tell here is "start simple" (as usual for every new programming language):
1 window with its messages (there are a lot you can experiment
1 window with one button
1 window with 1 button and a textbox
... and so on.
I'm sure in few experimenting steps you will get all.

Ciao
Eros

Petr Schreiber
02-09-2008, 18:39
So, under the current release, if you leave out the IsWindow() function, what will happen? Errors? Program stop running? Unsure of code execution?


Hi Marcuslee,

the IsWindow will not be removed from ThinBasic! Eros just developed new, better way to handle dialog interaction ( callbacks ). But IsWindow will be here, as it can be used for other tasks as well.

If you like IsWindow approach, you can continue using it. But I recommend to jump to callbacks once newThinBasic is out - it is much easier and has lot of benefits.


Petr