PDA

View Full Version : How to close dialog box from within callback



EmbeddedMan
29-09-2015, 20:47
I searched the example files, the offline help file, and the forums, and I couldn't find an answer to something that seems like it should be really easy:

How do you close a dialog window from within your dialog callback?

For example, if you want to detect that the user has pressed the escape key, and shut the program down when they do that, how do you get the window to close?

I set up a timer in my dialog window callback like so:

Case %WM_TIMER
If GetAsyncKeyState(%VK_ESCAPE) Then
Dialog Send CBHNDL, %WM_CLOSE, 0, 0
End If

and I try to send myself a %WM_CLOSE event, but this causes the app to crash when I hit escape.

Clicking on the close box, and having my normal %WM_CLOSE code run works perfectly.

Any ideas? This has got to be simple, but I just couldn't find any examples of it.

*Brian

Petr Schreiber
29-09-2015, 21:10
Hi Brian,

it should be as easy as calling:


Dialog End CBHNDL


Let us know!


Petr

EmbeddedMan
29-09-2015, 21:20
Yes! That works perfectly. Previously I tried Dialog End hDlg, where hDlg was the dialog window's handle, and that crashed my application.

*Brian

ErosOlmi
30-09-2015, 07:26
Consider using GetWindowKeyState: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?getwindowkeystate.htm

GetAsyncKeyState(%VK_ESCAPE) will return %TRUE even when your window is not the current one, so if you put your window in the background under other windows and press ESC, you window will always receive the state of the ESC key.
Instead GetWindowKeyState(CBHNDL, %VK_ESCAPE) will return %TRUE when ESC is pressed but only if your window is the actual foreground window.



...
Case %WM_TIMER
If GetWindowKeyState(CBHNDL, %VK_ESCAPE) Then
Dialog End CBHNDL
End If
...

John Spikowski
30-09-2015, 23:57
Hi Eros,

Is there a performance hit trying to deal with the Windows message que using an on the fly interpreter?

With Script BASIC I let IUP take on the job.

ErosOlmi
01-10-2015, 06:10
Ciao John,

so far I would say no problem but of course it depends on what you need to do with a script.

Dealing with window messages is always a challenge. Even compiled applications sometimes lose some message :)
Important is not to insert long operations in respond to a message otherwise your application seems blocked.
Messages are queued by windows and unless your operation is very long ... you always get messages from the queue without missing any of them.

One way to do long jobs in response to a message is to let different threads do the work but thinBasic is still not multi threads, maybe future versions.

In any case, usually there are many ways to achieve the same operations, sometimes a little optimization, some tricks or a native compiled functions inside thinBasic can solve the problem. That's why thinBasic has some many native commands: because they are compiled in the language and when used they are fast.

That said, I would never use thinBasic to loop for all pixels of a multi megapixel image in order to make some image transformation on the fly. That's not a work for an interpreted language.
In that case I would develop a thinBasic compiled module with some native functions that do the job.

Ciao
Eros

John Spikowski
01-10-2015, 19:36
Thanks Eros for the insight into the Windows message loop.

I filter out mouse movement messages at the C extension module level which helps. Having IUP doing much of the GUI low level work allows me to focus more on the original task.