BBC BASIC for Windows
« Close icon problem »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:21pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Close icon problem  (Read 371 times)
mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Close icon problem
« Thread started on: Mar 6th, 2010, 3:34pm »

When developing a program I discovered a problem with the Close icon in a window title bar sometimes not working.

Eventually I narrowed down the problem, and the following small program demonstrates what I found.

When run, it should display a simple dialog box, and when any message is received by the program it will print the msg, wparam and lparam values.

Code:
   10 REM TestClose          Demonstrate oddity woth click on Close icon
   20 
   30 INSTALL @lib$+"WINLIB2"
   40 
   50 PROCCreateDialogs                 :REM Create all windows
   60 PROCOpenDialogs                   :REM Display all windows
   70 
   80 DIM Click%(2),click%(2)
   90 ON SYS Click%()=@msg%,@wparam%,@lparam% : RETURN     :REM Message received
  100 
  110 REPEAT
  120   WAIT 1
  130   click%(0) = 0                   :REM surely SWAP will ovewrite?
  140   SWAP Click%(),click%()
  150   IF click%(0) THEN PRINT click%(0),click%(1);" (&";~click%(1);")",click%(2)
  160 UNTIL FALSE
  170 
  180 END
  190 
  200 REM =======================================================================
  210 
  220 DEF PROCCreateDialogs             :REM Create all windows
  230 d01% = FN_newdialog("Test Close Icon", 100, 10, 100, 100, 10, 300)
  240 PROC_editbox( d01%,"Edit box"              ,101, 10, 40, 80, 12,0)
  250 PROC_checkbox(d01%,"Check box "            ,100, 10, 20, 80, 12,0)
  260 ENDPROC
  270 
  280 REM =======================================================================
  290 
  300 DEF PROCOpenDialogs               :REM Display all windows
  310 PROC_showdialog(d01%)
  320 SYS "SendDlgItemMessage",!d01%,228,&465,0,20      :REM what does this do?
  330 ENDPROC
  340 
  350 REM ======================================================================= 


When the Close icon is clicked, it should (I thought) display 273, 2 (&2), and 0.
But it actually displays 273, 33554533 (&2000065), and a varying number.
(Note that &2000065 is &2000000 plus id of Edit box)

This caused my program to miss the 'Close' and not end correctly, which was puzzling, as it had worked previously!

However, it DOES return the correct values if any of the following is true:

1. Click the 'Check box' before the Close.
2. Comment out line 240 to remove the edit box.
3. Move line 240 to line 255 to define Edit box after Check box.
... and there are probably others!

The cause of this could be:

1. My program (and hence my understanding) is wrong.
2. There is a problem with BB4W.
3. There is a 'feature' of Windows.

Any suggestions please?
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #1 on: Mar 6th, 2010, 5:18pm »

on Mar 6th, 2010, 3:34pm, mavison wrote:
When the Close icon is clicked, it should (I thought) display 273, 2 (&2), and 0.
But it actually displays 273, 33554533 (&2000065), and a varying number.

As you have discovered, the 'problem' arises when you click on the 'close' button whilst the edit box has input focus. The reason is that in this case Windows sends an EN_KILLFOCUS notification for the edit control before the dialogue box closes. It's this notification you are seeing.

The sample code in the BB4W documentation (http://www.bbcbasic.co.uk/bbcwin/manual/bbcwing.html#clickbutton) tests not only for click% being 1 (OK) or 2 (Cancel) but also directly for the dialogue box closing (!dlg% = 0):

Code:
click% = 0
ON SYS click% = @wparam% AND &FFFF : RETURN
REPEAT pause% = INKEY(1) : UNTIL click% = 1 OR click% = 2 OR !dlg% = 0 

If you add a similar test (!d01% = 0) it should solve the problem.

Incidentally please note that strictly you should be ANDing your @wparam% value (click%(1) in your program) with &FFFF before testing it, because the LS 16 bits contain the control's ID (which is often what you are interested in) and the MS 16-bits contain a notification code (e.g. &200 for the EN_KILLFOCUS notification). You are getting away with it because the BN_CLICKED notification code is zero!

The sample code above does that in the ON SYS, but of course it need not be done there.

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #2 on: Mar 6th, 2010, 5:45pm »

Thanks Richard for the rapid solution: I will give that a go. I suspected it would be my program, but could not see why! Using Windows is a learning 'experience' !!

Martin
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #3 on: Mar 7th, 2010, 9:55pm »

Richard, your suggestions seem to resolve my problems, but can I ask for a couple of clarififications please:

Is this the best way to process buttons and icons at line 155?

__ If @msg% returns command (273)
____ then
______ If !dlg%=zero
________ then Close icon has been clicked
________ else do any tests on the Control ID

Should !dlg% be included in the click%() and Click% arrays?

Thanks
Martin
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #4 on: Mar 8th, 2010, 08:17am »

on Mar 7th, 2010, 9:55pm, mavison wrote:
Is this the best way to process buttons and icons at line 155?

Er, line 155? There is no line 155 in the program you previously listed!

Quote:
__ If @msg% returns command (273)
____ then
______ If !dlg%=zero
________ then Close icon has been clicked
________ else do any tests on the Control ID

That doesn't look right. You shouldn't be relying on receiving an ON SYS at all if the user closes the dialogue box. Note that unless you're issuing a *SYS 1 (not recommended) there's no need to store or test @msg% because it can only be WM_COMMAND!

The code ought to be something like this:

Code:
      DIM Click%(2),click%(2)
      ON SYS Click%()=@msg%,@wparam%,@lparam%:RETURN
      REPEAT
        WAIT 1
        click%()=0
        SWAP Click%(),click%()
        CASE click%(1) OF
            REM process any 'in dialogue' events here
        ENDCASE
      UNTIL click%(1)=1 OR click%(1)=2 OR !dlg%=0 

So the 'dialogue loop' will be exited if the box is closed, irrespective of whether any ON SYS events occur or not.

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #5 on: Mar 8th, 2010, 12:02pm »

Sorry: my example program was a simplification of a much larger program, which may have confused things. I could post a bit more complete program, if it would help.

Line 155 was just where I would put code to actually do some useful progessing of the messages. The example program just prints the values at line 150.

I am using SYS 1 in the real pogram, because I need to receive the dropfiles message 563.

You say 'You shouldn't be relying on receiving an ON SYS at all if the user closes the dialogue box.' but that is where I started! When the user clicks the Close (x) icon at the top right of the window (not the Close button, which returns and ID of 2), I do seem to get a message from ON SYS. If there is no message, how do I get to see that !dlg% (which I think is the dialog handle, and dlg% is just the address of the dialog block?) has been set to zero? Where/When does !dlg% get set to zero?

Incidentally, what returns an ID of 1 (tested for in your example?)

Sorry if I am misunderstanding you! I am trying to establish a basic structure which I fully understand, before using it as a basis for other programs.

Martin







User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #6 on: Mar 8th, 2010, 2:06pm »

on Mar 8th, 2010, 12:02pm, mavison wrote:
If there is no message, how do I get to see that !dlg%

I listed the code! Just copy the listing in my last message, which is basically identical to the code in the manual and in DLGDEMO.BBC, but with a click%() array rather than a scalar click% variable.

Quote:
Incidentally, what returns an ID of 1 (tested for in your example?)

The OK button returns an ID of 1 (IDOK). Every dialogue box should have an OK button (with a few rare exceptions). It's what the user clicks to say 'I've made my selections/entries, please proceed accordingly'.

Quote:
Sorry if I am misunderstanding you! I am trying to establish a basic structure which I fully understand, before using it as a basis for other programs.

I'm fully in agreement with that, which is why I'm trying to persuade you to use the tried-and-tested code I listed! That provides a "basic structure" which is suitable for virtually any dialogue box.

Richard.
« Last Edit: Mar 8th, 2010, 2:07pm by admin » User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #7 on: Mar 11th, 2010, 11:20am »

Thanks for the reply, Richard. Sorry for the delay in replying to you, but I have been reading and experimenting.

I did actually start from your code, but expanded it from the simple case to cater for my actual reqirements, which were not so simple! (for example, I wanted to cater for dropped files, and my OK button is just 'process', not 'process and close'). The code I posted was just to illustrate the problem, and was perhaps therefore misleading.

I have posted below a rather longer version, which reflects my current real code, which seems to work fine.

From my experiments, it seems that ...

!dlg% is set to zero by Windows when the Close Button orthe Close Icon is clicked.

Then when the next Command message is processed...

If it was the Close Button it will have an Item Number of 2.

However, if it was the Close Icon, then it MAY have an Item Number of 2, but it may reflect an item which had the input focus removed because of the close.

I hope I am now understanding correctly! Thanks for your help. Martin

10 REM TestClose2 Demonstrate oddity with click on Close icon
20
30 INSTALL @lib$+"WINLIB2"
40
50 PROCCreateDialogs :REM Create all windows
60 PROCOpenDialogs :REM Display all windows
70
80 DIM Click%(2),click%(2)
90 *SYS 1 :REM Enables many messages, imcluding DropFiles
100 SYS "DragAcceptFiles", @hwnd%, 1 :REM Registers window as accepting files
110 ON SYS Click%()=@msg%,@wparam%,@lparam% : RETURN :REM Message received
120
130 Quit% = FALSE
140 REPEAT
150 WAIT 1
160 IF !dlg%=0 THEN PRINT "Closed?"
170 click%(0) = 0
180 SWAP Click%(),click%()
190 CASE click%(0) OF
200 WHEN 273: PROCcommand(click%(1),click%(2))
210 WHEN 563: PROCdropfiles(click%(1),click%(2))
220 ENDCASE
230 UNTIL Quit% OR !dlg%=0
240
250 REM Code here to handle orderly closedown of program
260 PRINT "Program closing"
270 END
280
290 REM =================================================
300
310 DEF PROCcommand(wp%,lp%) :REM process clicks on buttons etc
320 LOCAL nc%,in%
330 nc% = wp%>>16 :REM get Notiication code
340 in% = wp% AND &FFFF :REM get item number
350 PRINT "command" TAB(10)"nc=";nc%,TAB(20)"in=";in%,TAB(30)"lp=";~lp%,TAB(42)"dh=";~!dlg%
360 CASE in% OF
370 WHEN 1 : REM Code to process OK action
380 WHEN 2 : Quit% = TRUE :REM act on Close Button
390 REM Code here with WHENs to process other buttons
400 ENDCASE
410 ENDPROC
420
430 REM =================================================
440
450 DEF PROCdropfiles(wp%,lp%) :REM Process DropFiles
460 PRINT "dropfiles" wp%,lp%
470 REM Code here to handle files that have been dropped
480 ENDPROC
490
500 REM =================================================
510
520 DEF PROCCreateDialogs :REM Create all windows
530 dlg% = FN_newdialog("Test Close Icon", 100, 10, 100, 100, 10, 300)
540 PROC_editbox( dlg% ,"Edit box" ,101, 10, 40, 80, 12, 0)
550 PROC_checkbox(dlg% ,"Check box" ,100, 10, 20, 80, 12, 0)
560 PROC_pushbutton(dlg%,"OK" , 1, 10, 60, 60, 12, 0)
570 PROC_pushbutton(dlg%,"Close" , 2, 10, 80, 60, 12, 0)
580 ENDPROC
590
600 REM =================================================
610
620 DEF PROCOpenDialogs :REM Display all windows - testing only
630 PROC_showdialog(dlg%)
640 SYS "SendDlgItemMessage",!dlg%,228,&465,0,20 :REM what does this do?
650 ENDPROC
660
670 REM =======================================================================
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #8 on: Mar 11th, 2010, 2:44pm »

on Mar 11th, 2010, 11:20am, mavison wrote:
!dlg% is set to zero by Windows

Not strictly by Windows, but by the code in WINLIB2.

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #9 on: Mar 16th, 2010, 4:39pm »

on Mar 11th, 2010, 2:44pm, Richard Russell wrote:
Not strictly by Windows, but by the code in WINLIB2.

Sorry Richard, not sure I understand this, because I can see no code:
either in WINLIB2 which sets !dlg%=0
or in your example between ON SYS and the !dlg% test which calls WINLIB2

so I must be missing something!
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #10 on: Mar 16th, 2010, 9:31pm »

on Mar 16th, 2010, 4:39pm, mavison wrote:
Sorry Richard, not sure I understand this, because I can see no code: either in WINLIB2 which sets !dlg%=0

Here is the code in WINLIB2 which sets !dlg% to zero:

Code:
      .Q%
      xchg eax,[dlg%] 

On entry to this code eax is zero, as you can see from the code which jumps to Q%:

Code:
      or eax,eax
      jz Q% 

Quote:
so I must be missing something!

Maybe you were only looking in the BASIC code rather than the assembler code?

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #11 on: Mar 17th, 2010, 3:25pm »

I did *look* at the assembler code, but made very little sense of it. I have now done a bit of research, and it seems that the dialog is defined with the address of some assembler code which is called (D%? or T%?). I am not sure of the precise mechanism, but when there is a message the code you refer to gets called if the Close icon has been clicked, presumably before the message is passed to the Basic program.

I think I have taken enough of your time, Richard! Many thanks for your patience and explanations. My current vague understanding will do for the moment.

Martin
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #12 on: Mar 17th, 2010, 6:49pm »

on Mar 17th, 2010, 3:25pm, mavison wrote:
I did *look* at the assembler code, but made very little sense of it. I have now done a bit of research...

Why do you feel the need to understand it? Is it just curiosity? The whole point of libraries is that you don't need to look at the code, and most certainly you don't need to understand it! The supplied libraries in particular are designed to be fast and compact, not readable.

With other languages libraries are often supplied as machine code object modules, without any source code being provided; that doesn't stop people using them. Because of the way BBC BASIC works you can look at the code of the libraries, but you are not expected to.

I would urge you to overcome your curiosity, and simply treat the libraries as 'black boxes'. If you could bring yourself simply to accept what I say at face value, rather than needing to verify it for yourself, your BBC BASIC programming experience ought to be more pleasurable.

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 14
xx Re: Close icon problem
« Reply #13 on: Mar 17th, 2010, 10:23pm »

I just wanted to understand what was changing !dlg%, why and when, as it was a variable used in my code. My 40 years in computing have taught me never to make assumptions!

And the Windows API is totally new to me, so I admit some desire to understand it better.

Martin
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Close icon problem
« Reply #14 on: Mar 18th, 2010, 12:29am »

on Mar 17th, 2010, 10:23pm, mavison wrote:
My 40 years in computing have taught me never to make assumptions!

I can claim almost 40 years in computing too (probably wrote my first program, either in Fortran or FOCAL, in late 1970), but what I've learnt is that you should make assumptions - you should assume that what the documentation says will happen actually will happen! When you program in BBC BASIC you assume the interpreter will behave how it's documented to behave; you don't ask to see its source code. When you call a Windows API you assume it will behave how MSDN says it will behave, you don't ask to see Microsoft's code.

Quote:
And the Windows API is totally new to me, so I admit some desire to understand it better.

Trying to learn about the Windows API by looking at the supplied libraries really is jumping in at the deep end. The complications of assembler code and multi-threading severely get in the way of understanding.

Far better, at least initially, to learn how to access API routines directly from a BASIC program. There are many published examples, Wiki articles etc. that you can study.

Richard.
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls