News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Need help processing enter key on dialog via dialogBoxParam.

Started by Konai, April 26, 2014, 11:17:29 AM

Previous topic - Next topic

Konai

I have been trying to process the enter key differently (really I want it to do nothing) when the following program runs.
The program is modified from the iczelion tutorial to input data from a user and display what they entered in a  message box.
If the user presses enter the message box comes up... I would rather the message box to be displayed only when the user clicks ok or (if the ok button has focus and enter was then pressed) and under no other circumstances should it display.

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

DlgProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

.stack 4096

.data
DlgName db "IDD_MAIN",0
AppName db "Our Second Dialog Box",0
TestString db "Wow! I'm in an edit box now",0
inputMessage db "Please enter a number.",0
outputMessage db "You entered...",0
inputLength dd 10
outputBuffer db 11 dup (0)
jPromptADDR dd ?
jDestADDR dd ?
jLen dd ?

inputJosh MACRO promptX,destinationX,lengthX
            lea eax,promptX
            mov jPromptADDR,eax
            lea eax,destinationX
            mov jDestADDR,eax
            mov eax,lengthX
            mov jLen,eax
            call   _getInput            ; getInput(inPrompt, inStr, maxLength)
          ENDM

.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
buffer db 512 dup(?)

.const
IDC_TEXT  equ 3000
IDC_OK    equ 3001
IDC_LABEL equ 3002

.code
start:
  inputJosh inputMessage,outputBuffer,inputLength
  invoke MessageBox,NULL,ADDR outputBuffer,ADDR outputMessage,MB_OK 

  jmp _theEnd
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
  .IF uMsg==WM_INITDIALOG
    invoke GetDlgItem, hWnd,IDC_TEXT
    invoke SetFocus,eax
    mov esi,jPromptADDR
    invoke SetDlgItemText,hWnd,IDC_LABEL,esi
  .ELSEIF uMsg==WM_CLOSE
    ;invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0
    invoke EndDialog, hWnd,NULL ;replaced above because this seemed confusing
  .ELSEIF uMsg==WM_COMMAND
                                             
    mov eax,wParam
    .IF lParam==0
    ;edited this out
    .ELSE
      mov edx,wParam
      shr edx,16
      .IF dx==BN_CLICKED
        .IF ax==IDC_OK
;
          invoke GetDlgItemText,hWnd,IDC_TEXT,jDestADDR,jLen
          invoke EndDialog, hWnd,NULL
        .ENDIF
      .ENDIF
    .ENDIF 
  .ELSE
    mov eax,FALSE   
    ret
  .ENDIF
  mov eax,TRUE
  ret
DlgProc endp

_getInput proc
  invoke GetModuleHandle, NULL
  mov    hInstance,eax
  invoke DialogBoxParam, hInstance, ADDR DlgName,NULL,ADDR DlgProc,NULL
  ret
_getInput endp


_theEnd:
  mov eax,0
_alternateExit:
  call ExitProcess
end start



and the resource file


;This Resource Script was generated by WinAsm Studio.


#define IDC_TEXT    3000
#define IDC_OK      3001
#define IDC_LABEL   3002

IDD_MAIN DIALOGEX 0,0,207,63
FONT 10,"MS Sans Serif"
STYLE 0x80c80ac0
EXSTYLE 0x00000008
BEGIN
  CONTROL "",IDC_TEXT,"Edit",0x10000080,7,21,193,14,0x00000200
  CONTROL "OK",IDC_OK,"Button",0x10000001,156,40,44,14,0x00000000
  CONTROL "",IDC_LABEL,"Edit",NOT 0x00810000 | 0x10000880,7,6,165,14,0x00000000
END



and the compile batch file using masm32


@echo off

\masm32\bin\rc /v framework.rc
\masm32\bin\cvtres /machine:ix86 framework.res

if exist "dialog.obj" del "dialog.obj"
if exist "dialog.exe" del "dialog.exe"

\masm32\bin\ml /c /coff "dialog.asm"
\masm32\bin\Link /SUBSYSTEM:WINDOWS "dialog.obj" framework.res

dir dialog.*

pause


This is my first attempt at creating a windows application so any suggestions are appreciated.

raymond

Try changing the OK button style from 0x10000001 to 0x10000000. (The "1" style is for a default style.)
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

Konai

That prevented the default behavior of acting like the IDC_OK button was clicked perfectly! Now I just need to figure out what message is being sent to the dialog when enter is pressed so that I can make it do some custom action if needed. I was under the impression that it would be WM_KEYUP and using VK_RETURN, but when I put code in to look for that message nothing happens at all. I am sure it is a fundamental mistake I am making, but I will continue to search for a good document on that subject.

Thank you for your help Raymond.

GoneFishing

read this and search for WM_GETDLGCODE in the old forum



RuiLoureiro


Konai

Thanks for the links and information! I will read them thoroughly as soon as I can.

Konai

QuoteIn a dialog box, of any kind, the CR key is returned as WM_COMMAND with an ID of 1, the ESC key is returned as ID 0.You can change the ID returned by CR by setting another control as the "default" control. Ofcourse if an editable control has the focus, it will intercept the CR before the dialog can process it. You can always subclass any control to change how it handles characters.

The above quote by donkey and reading some of the old posts has given me an idea of what I need to do. I will research how to sub class the edit control and see if that handles it the way I am trying to handle it. Also of note was the concept of changing the default button (perhaps an invisible one) but that idea does not appeal to me as it sounds more like a work around. Thank  you again for your suggestions / help.

RuiLoureiro

Hi Konay,
           When WM_INITDIALOG
           do this:
                ;------------------------------------------------
                ; Install a WH_MSGFILTER hook so our MsgHook
                ; callback can intercept the dialog's messages.
                ;------------------------------------------------
                invoke   GetCurrentThreadId
                invoke   SetWindowsHookEx, WH_MSGFILTER, offset DlgHook, 0, eax
                mov      _hHook1, eax

            When WM_CLOSE
            do this:
                invoke      UnhookWindowsHookEx, _hHook1

             Write your  own DlgHook procedure to get the messages.

Good luck  :t