News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

modeless dialogbox displays only with msgbox

Started by Rainstorm, February 23, 2008, 10:32:22 AM

Previous topic - Next topic

Rainstorm

hi,
i have a msgbox in my code to display the return value. - if i comment out the msgbox the dialogbox doesn't show too. it only displays if the msgbox displays. -also when i close the msgbox the dialog box closes too.

heres's the code..    DlgBox PROTO :DWORD, :DWORD, :DWORD, :DWORD

    .data?

        hInstance   dd      ?

    .data
   
        dlgtemplatebuff   dd     200 dup(0)
        ret_val           dd     0
        error_code        dd     0       

        dlgboxtitle      db    "Dialog Foundary",0
        font_name        db    "fixedsys",0
        button_title     db    "OK",0

; -----------------------------------------------------------------------

    .code

start:

        mov hInstance, FUNC(GetModuleHandle,NULL)

; ----------------------------------------------------
;          Fill in the dialog box template
; ----------------------------------------------------

    mov edi, offset dlgtemplatebuff

;    memalign edi, 4
    mov dword ptr [edi+0],   WS_POPUP or WS_SYSMENU or WS_CAPTION or WS_VISIBLE or WS_BORDER or DS_SETFONT
    mov dword ptr [edi+4],      0              ; extended styles
    mov  word ptr [edi+8],      1              ; control item-count
    mov  word ptr [edi+10],     100            ; x co-ordinate
    mov  word ptr [edi+12],     100            ; y co-ordinate
    mov  word ptr [edi+14],     90             ; width
    mov  word ptr [edi+16],     30             ; height
   
    mov  word ptr [edi+18],      0             ; Menu array   (0 = no menu)
    mov  word ptr [edi+20],      0             ; Class array  (0 = predifined dialogbox class)

    add edi,  22                               ; Title array start address

    invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr dlgboxtitle, -1,
                                edi, LENGTHOF dlgboxtitle
    add edi, LENGTHOF dlgboxtitle*2


; ========= * Font * ============

    mov word ptr [edi],          9             ; point size value of the font
    add edi,  2
   

; -- [ Font name, converted into a unicode string ] ---------

    invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr font_name, -1,
                                edi, LENGTHOF font_name

    add edi, LENGTHOF font_name*2

   
; --------------------------------------------------------------
;           Fill in the dialog box items template
; --------------------------------------------------------------

       memalign edi, 4
       mov dword ptr [edi+0] ,   BS_DEFPUSHBUTTON or WS_CHILD or WS_VISIBLE     ; style
       mov dword ptr [edi+4] ,   0           ;  extended style
       mov  word ptr [edi+8] ,   28          ;  x-coordinate
       mov  word ptr [edi+10],   14          ;  y-coordinate
       mov  word ptr [edi+12],   30          ;  width , in dialog box units, of the control
       mov  word ptr [edi+14],   10          ;  height, in dialog box units, of the control
       mov  word ptr [edi+16],   IDCANCEL    ;  control identifier

   Comment *
   ===------- Class array --------------------------------------------
       65535 value indicates that array has 1 additional element       
       that specifies the ordinal value of a predefined system class 
   ------------------------------------------------------------------- *

       mov word ptr [edi+18], 65535         
       mov word ptr [edi+20], 128          ; signifies a 'buttonclass' for the control


; ------ Title array --------

      add edi, 22

       invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr button_title, -1,
                                   edi, LENGTHOF button_title

       add edi, LENGTHOF button_title*2


       mov word ptr [edi],  0                ;  creation data
       add edi           ,  2

; ===========================================================
       
       
    invoke CreateDialogIndirectParam, hInstance, addr dlgtemplatebuff, 0,  DlgBox, 0
    mov ret_val, eax

  ; ------***MessageBox here***----------

    fn MessageBox, NULL,sstr$(ret_val),"Return Value",MB_OK

;    invoke GetLastError
;    mov error_code, eax
;    fn MessageBox, NULL,ustr$(error_code),"Error Code",MB_OK


    DlgBox proc hWndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

           .if uMsg == WM_COMMAND
               movzx ebx, word ptr [wParam]          ; low word of wParam - control identifier
               .if ebx == IDCANCEL
                   jmp end_dialog
               .endif


           .elseif uMsg == WM_INITDIALOG
               jmp true_

           .else
               jmp false_                            ; default processing
                               
           .endif   

        end_dialog:
           invoke DestroyWindow, hWndDlg
         
   
        true_:
          mov eax, 1                              ;  return true
          jmp ret_
   
        false_:
          xor eax, eax                            ; return false
   
        ret_:   
          ret
     
    DlgBox endp

end start

MichaelW

For a modal dialog the function that creates the dialog does not return until the dialog is destroyed. For a modeless dialog the function that creates the dialog returns a handle, just as CreateWindow or CreateWindowEx would for a normal application window. And for a modeless dialog your application must provide a message loop, just as it would for a normal application window, and the dialog window procedure must handle the WM_CLOSE and WM_DESTROY messages just as it would for a normal application window.

I see that you took care of this, but to make the point anyway: modal dialogs are made visible regardless, but for a modeless dialog to be visible you must specify WS_VISIBLE style in the template.
eschew obfuscation

Rainstorm

michael wrote...
QuoteAnd for a modeless dialog your application must provide a message loop, just as it would for a normal application window, and the dialog window procedure must handle the WM_CLOSE and WM_DESTROY messages just as it would for a normal application window.
frick yah i forgot that the modless dialog box needs to fetch its own msgs. - the modal dialog seems to handle the WM_CLOSE through default processing. I added it here anyway but i tried with commenting out the WM_CLOSE lines, & the default processing seems to handle it right like if i rt-clicked the dialog window & choose close

It seems to work okay now. - Ive used PostQuitMsg to process the WM_DESTROY & then exit.. that's right isn't it ?
any comments welcome.
thank-you
here's the code for the dialog-box....
    DlgBox PROTO :DWORD, :DWORD, :DWORD, :DWORD

     .data?

        hInstance   dd      ?

    .data
        hdlg              dd     0
        dlgtemplatebuff   dd     200 dup(0)
        ret_val           dd     0
        error_code        dd     0       

        dlgboxtitle      db    "Dialog Foundary - Stardrift",0
        font_name        db    "fixedsys",0
        button_title     db    "OK",0

        msg             MSG     <0>
       
; -----------------------------------------------------------------------


    .code

start:

        mov hInstance, FUNC(GetModuleHandle,NULL)


; ----------------------------------------------------
;          Fill in the dialog box template
; ----------------------------------------------------

    mov edi, offset dlgtemplatebuff

    mov dword ptr [edi+0],   WS_POPUP or WS_SYSMENU or WS_CAPTION or WS_VISIBLE or WS_BORDER or DS_SETFONT
    mov dword ptr [edi+4],      0              ; extended styles
    mov  word ptr [edi+8],      1              ; control item-count
    mov  word ptr [edi+10],     100            ; x co-ordinate
    mov  word ptr [edi+12],     100            ; y co-ordinate
    mov  word ptr [edi+14],     90             ; width
    mov  word ptr [edi+16],     30             ; height
   
    mov  word ptr [edi+18],      0             ; Menu array   (0 = no menu)
    mov  word ptr [edi+20],      0             ; Class array  (0 = predifined dialogbox class)

    add edi,  22                               ; Title array start address

    invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr dlgboxtitle, -1,
                                edi, LENGTHOF dlgboxtitle
    add edi, LENGTHOF dlgboxtitle*2

    mov word ptr [edi],          9             ; point size value of the Font
    add edi,  2

; -- [ Font name, conerted into a unicode string ] ---------

    invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr font_name, -1,
                                edi, LENGTHOF font_name

    add edi, LENGTHOF font_name*2

   
; --------------------------------------------------------------
;           Fill in the dialog box items template
; --------------------------------------------------------------

       memalign edi, 4
       mov dword ptr [edi+0] ,   BS_DEFPUSHBUTTON or WS_CHILD or WS_VISIBLE     ; style
       mov dword ptr [edi+4] ,   0           ;  extended style
       mov  word ptr [edi+8] ,   28          ;  x-coordinate
       mov  word ptr [edi+10],   14          ;  y-coordinate
       mov  word ptr [edi+12],   30          ;  width , in dialog box units, of the control
       mov  word ptr [edi+14],   10          ;  height, in dialog box units, of the control
       mov  word ptr [edi+16],   IDCANCEL    ;  control identifier

   Comment *
   ===------- Class array --------------------------------------------
       65535 value indicates that array has 1 additional element       
       that specifies the ordinal value of a predefined system class 
   ------------------------------------------------------------------- *

       mov word ptr [edi+18], 65535         
       mov word ptr [edi+20], 128          ; signifies a 'buttonclass' for the control


; ===-- Title array -----=============

       add edi, 22

       invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, addr button_title, -1,
                                   edi, LENGTHOF button_title

       add edi, LENGTHOF button_title*2


       mov word ptr [edi],  0                ;  creation data
       add edi           ,  2

; =====================x====== - End of Template - ======x================================
       
       
    invoke CreateDialogIndirectParam, hInstance, addr dlgtemplatebuff, 0,  DlgBox, 0
    mov hdlg, eax                         ; handle to dialog


    Messageloop:
;      .if FUNC(GetMessage, addr msg, hdlg, 0, 0) != SDWORD PTR -1

      invoke GetMessage, addr msg, hdlg, 0, 0
      mov ret_val, eax

      .if ret_val == SDWORD PTR -1 
          fn MessageBox,0,"error","Title",MB_OK
          jmp ExitMsgLoop

      .elseif ret_val == 0

          jmp ExitMsgLoop

      .else
          invoke TranslateMessage,  addr msg
          invoke DispatchMessage ,  addr msg
      .endif

          jmp Messageloop


    ExitMsgLoop:
          mov eax, msg.wParam
          invoke ExitProcess, eax 


    DlgBox proc hWndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
 

           .if uMsg == WM_COMMAND
               movzx ebx, word ptr [wParam]          ; low word of wParam - control identifier
               .if ebx == IDCANCEL
                   jmp end_dialog
               .endif

           .elseif uMsg == WM_INITDIALOG
               jmp true_

           .elseif uMsg == WM_DESTROY   
               invoke PostQuitMessage, NULL
               jmp true_

            .elseif uMsg == WM_CLOSE
               jmp end_dialog   

           .else
               jmp false_                            ; default processing
                               
           .endif   


        end_dialog:
           invoke DestroyWindow, hWndDlg
   
        true_:
          mov eax, 1                              ;  return true
          jmp ret_
   
        false_:
          xor eax, eax                            ; return false
           
   
        ret_:   

          ret
     
    DlgBox endp



end start