PDA

View Full Version : Modeless Message Handling



JosephE
25-08-2010, 21:01
I'm rather new to window message handling, and I'm working on making a simple modeless dialog.

I'm curious as to why it keeps processing messages after I close the window. See the example code. On my system, I get about 38 WM_CLOSE messages every time. And thinBASIC doesn't throw an error every time I end the dialog that doesn't exist, either. Am I missing something?

Note that I have a simple While Loop running while the window is open, this represents what my program is doing in the background while the user is staring at my window.



Uses "UI"
Uses "Console"

Dim hWin As DWord
Dim hWinStyle As DWord = %WS_DLGFRAME Or %DS_CENTER Or %WS_CAPTION Or %WS_SYSMENU Or %WS_OVERLAPPEDWINDOW
Dim Title As String = "Test Window"
Dim Alive As Boolean
Dim Closed As Integer = 0

Function TBMain()
Dim test As Integer = 0
Dialog New Pixels, 0, Title, -1, -1, 800, 600, hWinStyle To hWin
Dialog Show Modeless hWin, Call Messages()
Alive = TRUE
While Alive
test+=1
PrintL "Up and running... "&test
Dialog DoEvents 0
Wend
PrintL "Press any key to quit..."
WaitKey
End Function

CallBack Function Messages()
' Message handler for window.
Select Case CBMSG
Case %WM_INITDIALOG
Case %WM_COMMAND
Select Case CBCTL
Case %bClose
End Select
Case %WM_CLOSE
Dialog End hWin
Alive = FALSE
Closed+=1
PrintL "Closing: " & Closed
End Select
End Function

Petr Schreiber
25-08-2010, 21:17
Hi JosephE,

I think the thing is you should not call DIALOG END hWin in your WM_Close.
The fact you entered handling WM_Close means that dialog is ... yes ... :unguee: ... on its way to the other side.

Other thing - you don't need Alive, IsWindow function will do the same job for you.

Have a look at this code:


Uses "UI"
Uses "Console"

Dim hWin As DWord
Dim hWinStyle As DWord = %WS_DLGFRAME Or %DS_CENTER Or %WS_CAPTION Or %WS_SYSMENU Or %WS_OVERLAPPEDWINDOW
Dim Title As String = "Test Window"
Dim Alive As Boolean
Dim Closed As Integer = 0

Function TBMain()
Dim test As Integer = 0
Dialog New Pixels, 0, Title, -1, -1, 800, 600, hWinStyle To hWin
Dialog Show Modeless hWin, Call Messages()

While IsWindow(hWin)
test+=1
PrintL "Up and running... "&test
Dialog DoEvents 0
Wend

PrintL "Press any key to quit..."
WaitKey
End Function

CallBack Function Messages()
' Message handler for window.
Select Case CBMSG
Case %WM_INITDIALOG
Case %WM_COMMAND
Select Case CBCTL
Case %bClose
End Select
Case %WM_CLOSE
Closed+=1
PrintL "Closing: " & Closed
End Select
End Function



Petr

JosephE
25-08-2010, 21:23
Thanks Petr, that's great. I thought for sure it was my responsibility to close the dialog on the close message, but apparently that's one less thing I have to worry about. And I also didn't know about IsWindow(), that's going to be useful.

You guys are really helpful (and fast!). Now I get to go back and work on the fun part. :)