Announcement

Collapse
No announcement yet.

Quick_Tut 101: What is an Event Handler - DDT style

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Quick_Tut 101: What is an Event Handler - DDT style

    First, start with a Modeless Dialog Box



    Code:
    'EXAMPLE 5: A BASIC MODELESS DIALOG - DDT style
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CALLBACK FUNCTION My_DlgProc() AS LONG
        SELECT CASE CBMSG    '----------- start message handler ----------
        END SELECT     '-----------  end message handler  ----------
    END FUNCTION ' (My_DlgProc)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FUNCTION PBMAIN
        DIM hDlg AS LONG, Count AS LONG
        DIALOG NEW %HWND_DESKTOP, "A BASIC MODELESS DIALOG - DDT style",100,100,250,100, _
                                  %WS_OVERLAPPEDWINDOW, TO hDlg&
        DIALOG SHOW MODELESS hDlg&, CALL My_DlgProc&()
        DO		'----------- start message loop ----------
            DIALOG DOEVENTS TO Count&
        LOOP WHILE Count&	  '-----------  end message loop  ----------
    END FUNCTION ' (PBMAIN)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Now, a lot of programmers new to windows want to place thier code
    inside of the PBMAIN() function block, let's try it:




    Code:
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FUNCTION PBMAIN  
        DIM hDlg as LONG
        DIALOG NEW %HWND_DESKTOP, "Demo-ing improper placement of code",100,100,250,100, _
                                  %WS_OVERLAPPEDWINDOW, TO hDlg
        CONTROL ADD LABEL,  hDlg, 101,"This is a label",10,10,170,11, %SS_SUNKEN or %SS_CENTER                          
        CONTROL ADD LABEL,  hDlg, 102,"This is a label",10,30,170,11, %SS_SUNKEN or %SS_CENTER
        DIALOG SHOW MODELESS hDlg&, 
    
       'a demonstration of improperly attempting to use code
       'outside of an Event Handler
    		CONTROL SET TEXT hDlg,102,
    		"This is a label"    
    		SLEEP 1500
    		CONTROL SET TEXT hDlg,102,
    		"Changing the text "
    		SLEEP 1500
    		CONTROL SET TEXT hDlg,102,
    		"This is the last change"
       '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
        DO     '----------- start message loop ----------
            DIALOG DOEVENTS TO Count&
        LOOP WHILE Count&   '----------- end message loop ----------
            
    END FUNCTION ' (PBMAIN)
    Now, as you run it, you will see the labels not completly drawn until the end
    this is because in order for windows to support multi-tasking it needs to process
    many messages (or commands) coming from different programs at the same time.
    Windows does this by putting messages on a queue (a circular list) and processes
    them in any order they are recieved, how-ever some messages must have priority
    over others, so windows uses a procedure that manages the messages.

    This procedure, (or callback function) is called an event handler.
    let's try a proper version that uses the callback function:




    Code:
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CALLBACK FUNCTION My_DlgProc
    STATIC My_TextFlg as LONG
      SELECT CASE CBMSG  '----------- start message handling ----------
    	CASE %WM_INITDIALOG
    	   SetTimer CBHNDL, 1, 1000, BYVAL 0
    	   FUNCTION=%TRUE
    		
    	CASE %WM_TIMER
                SELECT CASE My_TextFlg
                   CASE 0
    		  CONTROL SET TEXT CBHNDL,102,
    		  "This is a label"
    		  INCR My_TextFlg
                   CASE 1
    		  CONTROL SET TEXT CBHNDL,102,
    		  "Changing the text "
    		  BEEP
                       INCR My_TextFlg
                   CASE 2
    		  CONTROL SET TEXT CBHNDL,102,
    		  "This is the last change"
                        My_TextFlg = 0
                 END SELECT
    
     	CASE %WM_DESTROY
                KillTimer CBHNDL, 1    
      END SELECT   '----------- end message handling ----------
    END FUNCTION '(My_DlgProc)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FUNCTION PBMAIN  
        DIM hDlg as LONG
        DIALOG NEW %HWND_DESKTOP, "Demo: using the Event Handler",100,100,250,100, _
                                  %WS_OVERLAPPEDWINDOW, TO hDlg
        CONTROL ADD LABEL,  hDlg, 101,"This is a label",10,10,170,11, %SS_SUNKEN or %SS_CENTER                          
        CONTROL ADD LABEL,  hDlg, 102,"This is a label",10,30,170,11, %SS_SUNKEN or %SS_CENTER
              
        DIALOG SHOW MODAL hDlg, CALL My_DlgProc&()
    END FUNCTION ' (PBMAIN)
    SIMPLE !!!

    Also note that we didn't need to use a Modeless Dialog to process the messages
    The Modal Dialog worked fine.


  • #2
    Also note that we didn't need to use a Modeless Dialog to process the messages
    The Modal Dialog worked fine
    It might be a little easier to understand if you said...

    All dialogs require a message loop.

    DIALOG SHOW MODAL statement creates its own message loop, and does not return until DIALOG END is executed

    DIALOG SHOW MODELESS statement returns more or less immediately and requires the programmer supply a message loop.

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      What happens to %WM_COMMAND messages if you don't give a callback function to DIALOG SHOW?

      Comment


      • #4
        Originally posted by Chris Holbrook View Post
        What happens to %WM_COMMAND messages if you don't give a callback function to DIALOG SHOW?
        it's been a while since I've programmed windows, but as I recall windows has it's own callback function in the background that processes all the messages that we don't provide code for in our callbacks.

        Michael, I appreciate all the critique!

        let's fix this demo up, I remember having a very difficult time understanding how it all worked when I first attempted windows, maybe very beginner tut's like this will help a lot!

        Comment

        Working...
        X