hi,all
in one example of tutorial 11,he create a modeless dialog box use below code


invoke CreateDialogParam,hInstance, addr DlgName,hWnd,OFFSET DlgProc,NULL
mov hwndDlg,eax


but made the produce as below which to process model dialog box's message.


DlgProc PROC hWnd:HWND,iMsg:DWORD,wParam:WPARAM, lParam:LPARAM
.if iMsg==WM_INITDIALOG
invoke GetDlgItem,hWnd,IDC_EDIT
invoke SetFocus,eax
.elseif iMsg==WM_CLOSE
invoke EndDialog,hWnd,NULL
mov hwndDlg,0
.elseif iMsg==WM_COMMAND
mov eax,wParam
mov edx,eax
shr edx,16
.if dx==BN_CLICKED
.if eax==IDC_EXIT
invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
.elseif eax==IDC_BUTTON
invoke SetDlgItemText,hWnd,IDC_EDIT,ADDR TestString
.endif
.endif
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp

it seems wrong,but the code run well?that puzzled me.
Posted on 2004-11-12 00:58:06 by geegle
What is wrong with the code? I don't see anything wrong with it.
Posted on 2004-11-12 05:59:15 by roticv
The code has some problems.


invoke CreateDialogParam,hInstance, addr DlgName,hWnd,OFFSET DlgProc,NULL

ADDR should be used when using INVOKE.


.if iMsg==WM_INITDIALOG
invoke GetDlgItem,hWnd,IDC_EDIT
invoke SetFocus,eax

Should then return FALSE, not TRUE.


.elseif iMsg==WM_CLOSE
invoke EndDialog,hWnd,NULL

mov hwndDlg,0

Should close the dialog using DestroyWindow(), not EndDialog().


.elseif iMsg==WM_COMMAND
mov eax,wParam
mov edx,eax
shr edx,16
.if dx==BN_CLICKED
.if eax==IDC_EXIT
invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL

Should compare AX, not EAX. Should probably use PostMessage() instead of SendMessage(). Also missing two .elses to return FALSE.
Posted on 2004-11-12 10:28:33 by death
i have seen form msdn that "The dialog box procedure must not call the EndDialog function to destroy a modeless dialog box."
but in this eaxmple code it use EndDialog and run well.
Posted on 2004-11-14 21:21:12 by geegle
Some of these comments are not correct. You can use OFFSET in an invoke call with no problems as long as the data IS an OFFSET. An OFFSET in code is nothing more than a 32 bit address of the OFFSET of the data in the EXE file, usually but not always in the .DATA section.

The value returned from the WM_INITDIALOG processing depends on if you want to set the focus to the first control in the dialog box or not. Either true OR false are valid depending on the code design.

Even though CreateDialogParam is a modeless dialog function call, if it has no parent, it behaves no differently to a modal dialog box with no parent and you normally close a modal dialog procedure with EndDialog. As it works on versions from win95 up, it appears to be OK and version safe.

Windows code has always had various methods of handling windows. Iczelion's code has proven reliable over many years and he was well aware of the range of variation available.
Posted on 2004-11-15 04:58:55 by hutch--
Some of these comments are not correct. You can use OFFSET in an invoke call with no problems as long as the data IS an OFFSET. An OFFSET in code is nothing more than a 32 bit address of the OFFSET of the data in the EXE file, usually but not always in the .DATA section.

Notice I said should. AFAIK, MASM's Programmers Guide allows OFFSET to be via its BNF grammar appendix, but has no mention of it otherwise (in context of INVOKE). In the section for passing an address using INVOKE, it mentions ADDR only.

The value returned from the WM_INITDIALOG processing depends on if you want to set the focus to the first control in the dialog box or not. Either true OR false are valid depending on the code design.

Yes, and you can see that SetFocus() was called. Obviously, it should return FALSE.

Even though CreateDialogParam is a modeless dialog function call, if it has no parent, it behaves no differently to a modal dialog box with no parent and you normally close a modal dialog procedure with EndDialog. As it works on versions from win95 up, it appears to be OK and version safe.

But is it documented?

Windows code has always had various methods of handling windows. Iczelion's code has proven reliable over many years and he was well aware of the range of variation available.

The code is incorrect. Iczelion is a great coder, but he is not infallible.
Posted on 2004-11-15 06:07:33 by death
i find some words in msdn as below:

"
The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus.
"


if we return true,what control will the system set focus to?

"default keyboard focus" defined with us in resource file or
microsoft windows?
Posted on 2004-11-15 08:50:19 by geegle
Read the remarks section for WM_INITDIALOG.
Posted on 2004-11-15 12:22:02 by death
geegle,

Just pick which return value you want depending on what you want to do, its simple enough. Either set the 1st control focus as a default or manually set the forus with the API.

"invoke" syntax can easily handle a single DWORD value and this is what OFFSET delivers in 32 bit mode. You don't need to determine between ADDR and OFFSET if you know the value is an offset in the file.

All dialogs are originally designed to work as child windows as per documentation, use them without a parent handle and you change the behaviour. When a modal dialog has no parent so it cannot behave as a modal dialog. This is where the distinction breaks down.

The notion of "correct" here is in itself incorrect as the documentation does not handle the case of either type of dialog as a main window but as a child window of another window. Iczelion had no problems distinguishing between the two types, he just happened to know how they behaved when used outside of their documented behaviour.
Posted on 2004-11-15 18:36:05 by hutch--
thanks death and hutch.
i researched the dialog box for days,iczelion's code no wrong.
we can use EndDialog to close a modeless dialogbox.
Posted on 2004-11-17 00:10:13 by geegle
We also can use DestroyWindow to end a model dialogbox which have
no parent.
Posted on 2004-11-17 00:13:51 by geegle