News:

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

Dialog won't close

Started by dicky96, August 15, 2006, 09:09:42 AM

Previous topic - Next topic

dicky96

Hi guys

I have a program which has a child dialog opened from the main one for the user to input some data

Thge dialog opens fine with

invoke DialogBoxParam,hInstance,IDD_ADDSERV_DLG,hWnd,OFFSET DlgProc,NULL
         mov hDlg,eax

but when I try to close it in the child dialog message loop with the following code, it will not close.   I added the messagebox line just to prove program execution reaches this point, which it does.

.elseif eax==WM_CLOSE
      invoke MessageBox,NULL, addr MsgBoxText,addr MsgBoxCaption,MB_OK
      invoke EndDialog,hDlg,NULL







Tedd

That looks good from here.
I think you're going to have to post your full program (preferably a stripped down version that still shows the problem; and sometimes doing this even hleps you find the problem :wink) so we can find where the problem is actually coming from.
No snowflake in an avalanche feels responsible.

sluggy

Are you returning TRUE or FALSE after you process this message?

dicky96

TRUE

But I just tried FALSE and that makes no difference.  While playing around I accidentally put invoke EndDialog,hWnd,NULL and that causes the parent window to close, leaving the child dialog open on screen!   :red

Here's the code...

.data?

hInstance         dd ?
CommandLine         dd ?
hWnd            dd ?
hDlg            dd ?

.code

start:

   invoke GetModuleHandle,NULL
   mov    hInstance,eax
   invoke GetCommandLine
   invoke InitCommonControls
   mov      CommandLine,eax
   invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
   invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
   LOCAL   wc:WNDCLASSEX
   LOCAL   msg:MSG

   mov      wc.cbSize,sizeof WNDCLASSEX
   mov      wc.style,CS_HREDRAW or CS_VREDRAW
   mov      wc.lpfnWndProc,offset WndProc
   mov      wc.cbClsExtra,NULL
   mov      wc.cbWndExtra,DLGWINDOWEXTRA
   push   hInst
   pop      wc.hInstance
   mov      wc.hbrBackground,COLOR_BTNFACE+1
   mov      wc.lpszMenuName,IDM_MENU
   mov      wc.lpszClassName,offset ClassName
   invoke LoadIcon,NULL,IDI_APPLICATION
   mov      wc.hIcon,eax
   mov      wc.hIconSm,eax
   invoke LoadCursor,NULL,IDC_ARROW
   mov      wc.hCursor,eax
   invoke RegisterClassEx,addr wc
   invoke CreateDialogParam,hInstance,IDD_DIALOG,NULL,addr WndProc,NULL
   invoke ShowWindow,hWnd,SW_SHOWNORMAL
   invoke UpdateWindow,hWnd
   .while TRUE
      invoke GetMessage,addr msg,NULL,0,0
     .BREAK .if !eax
      invoke TranslateMessage,addr msg
      invoke DispatchMessage,addr msg
   .endw
   mov      eax,msg.wParam
   ret

WinMain endp

WndProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

   mov      eax,uMsg
   .if eax==WM_INITDIALOG
      push   hWin
      pop      hWnd
      ;initialise path to the .ini file
      invoke GetAppPath,offset AppPath
      invoke lstrcat,offset AppPath,offset IniFile
      
      ;initialise handles to controls and set up initial contents
      invoke GetDlgItem,hWnd,IDC_BTN_ADD
      mov hBtnAdd,eax
      invoke GetDlgItem,hWnd,IDC_BTN_EDIT
      mov hBtnEdit,eax
      
      invoke GetDlgItem,hWnd,IDC_BTN_DEL
      mov hBtnDel,eax
      
      invoke GetDlgItem,hWnd,IDC_BTN_OPEN
      mov hBtnOpen,eax
      
      invoke GetDlgItem,hWnd,IDC_BTN_CLOSE
      mov hBtnClose,eax
      
      invoke GetDlgItem,hWnd,IDC_COM_PORT
      mov hComPort,eax
      
      invoke GetDlgItem,hWnd,IDC_TXT_LOG
      mov hTxtLog,eax
      invoke SendMessage,hTxtLog,WM_SETTEXT,0,addr msg_Welcome1
      invoke SendMessage,hTxtLog,EM_SETSEL,NEXTLINE,NEXTLINE
      invoke SendMessage,hTxtLog,EM_REPLACESEL,0,addr msg_Welcome2
      invoke SendMessage,hTxtLog,EM_SETSEL,NEXTLINE,NEXTLINE
      invoke SendMessage,hTxtLog,EM_REPLACESEL,0,addr msg_Welcome3
      invoke SendMessage,hTxtLog,EM_SETSEL,NEXTLINE,NEXTLINE
      
      invoke GetDlgItem,hWnd,IDC_LST_SERVERS
      mov hLstServers,eax
      
   .elseif eax==WM_COMMAND
      mov      eax,wParam
      and      eax,0FFFFh
      .if eax==IDM_FILE_EXIT
         invoke SendMessage,hWin,WM_CLOSE,0,0
      .elseif eax==IDM_HELP_ABOUT
         invoke ShellAbout,hWin,addr AppName,addr AboutMsg,NULL
      .elseif eax==IDC_BTN_ADD
         invoke DialogBoxParam,hInstance,IDD_ADDSERV_DLG,hWnd,OFFSET DlgProc,NULL
         mov hDlg,eax
      .endif
;   .elseif eax==WM_SIZE
   .elseif eax==WM_CLOSE
      invoke DestroyWindow,hWin
   .elseif uMsg==WM_DESTROY
      invoke PostQuitMessage,NULL
   .else
      invoke DefWindowProc,hWin,uMsg,wParam,lParam
      ret
   .endif
   xor    eax,eax
   ret

WndProc endp

DlgProc PROC hWin:HWND,iMsg:UINT,wParam:WPARAM, lParam:LPARAM
   mov eax,iMsg
   .if eax==WM_INITDIALOG
      invoke GetDlgItem,hWin,IDC_EDIT_SERVER
      mov hTxtServer, eax
      invoke SetFocus,eax
      
      invoke GetDlgItem,hWin,IDC_EDIT_URL
      mov hTxtURL, eax
      
      invoke GetDlgItem,hWin,IDC_EDIT_PORT
      mov hTxtPort, eax
      

      invoke GetDlgItem,hWin,IDC_EDIT_USER
      mov hTxtUser, eax

      invoke GetDlgItem,hWin,IDC_EDIT_PASS
      mov hTxtPass, eax
      
      invoke GetDlgItem,hWin,IDC_BTN_SAVE
      mov hBtnSave, eax

      invoke GetDlgItem,hWin,IDC_BTN_CANCEL
      mov hBtnCancel, eax

   .elseif eax==WM_CLOSE
      invoke MessageBox,NULL, addr MsgBoxText,addr MsgBoxCaption,MB_OK
      invoke EndDialog,hDlg,NULL

; tried the next two lines to test if return FALSE fixed the problem
;      mov eax,FALSE
;      ret
   .elseif eax==WM_COMMAND
      mov eax,wParam
      and eax,0FFFFh
      .if eax==IDC_BTN_SAVE
         invoke SendMessage,hTxtServer,WM_SETTEXT,0,addr msg_Test
      .endif
   .else
      mov eax,FALSE
      ret
   .endif
   mov  eax,TRUE
   ret
DlgProc endp

end start




BUT actually in trying to describe this problem I also have fixed it!   I noticed I could not send text to any of my edit controls in this child dialog either - so I changed all the GetDlgItem lines from

invoke GetDlgItem,hDlg,IDC_ blah blah 

to

invoke GetDlgItem,hWin,IDC_blah blah

and that fixed that problem so I decided I was onto a wiiner here ... so then I changed

invoke EndDialog,hDlg,NULL

in to

invoke EndDialog,hWin,NULL

and Hey Presto!! it works


Alll I can gather from this is that hDlg is never initialised because after I invoke DialogBoxParam the program flow passes straight to the Dialog Procedure for the child dialog and I never execute

mov hDlg,eax

But that doesn't sound correct to me, so what's the real reason???


TIA
dicky

ToutEnMasm

Hello,
Missing an important thing in the source code,the resource.
The register class name must be the class of the dialog box.

You create a dialog box in the modeless dialog box by:
invoke CreateDialogParam,hInstance,IDD_DIALOG,NULL,addr WndProc,NULL
The handle of the mother is null,try to change to the modeless handle one
                               ToutEnMasm

Mincho Georgiev

If you want your Dlg to be properly closed by EndDialog API, you have to use a child messagebox instead of that one,i.e.:

invoke MessageBox,hWin, addr MsgBoxText,addr MsgBoxCaption,MB_OK

p.s. DlgProc is always better to return 0.

dicky96

@shaka_zulu
The messagebox was just there while I diagnosed the problem - it was to see if program flow got to that point

@tout_en_masm
not sure I  understand your reply - I need to study a bit further, but if you could elaborate further that woiuld be great.  I was happy I got the child dialog to close.


dicky

Boucly

#7
Quote@tout_en_masm
not sure I  understand your reply - I need to study a bit further, but if you could elaborate further that woiuld be great.  I was happy I got the child dialog to close.

I think tout_en_masm meant to change the hWndParent (the third) parameter of the CreateDialogParam function to the handle main window, so that the parent window cannot be closed before the dialogue returns a value.

Oh, and hello to everyone, this is my first real post in this forum.

Boculy

tenkey

Quote from: dicky96 on August 15, 2006, 04:03:44 PM
... so then I changed

invoke EndDialog,hDlg,NULL

in to

invoke EndDialog,hWin,NULL

and Hey Presto!! it works


Alll I can gather from this is that hDlg is never initialised because after I invoke DialogBoxParam the program flow passes straight to the Dialog Procedure for the child dialog and I never execute

mov hDlg,eax

But that doesn't sound correct to me, so what's the real reason???

The real reason is the DialogBoxParam doesn't return at all until you execute EndDialog and exit the DlgProc.

DialogBoxParam has its own message handling loop which calls the DlgProc. EndDialog sets up things so that the hidden message handling loop is terminated on return from DlgProc.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

dicky96

Thanks guys  :bg

Good here in'it   :8)

dicky