flat assembler
Message board for the users of flat assembler.

Index > Windows > Setting Focus To a Form Control

Author
Thread Post new topic Reply to topic
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
For some reason I'm having a lot of trouble with this. (I'm such a noob) What I'm trying to do is have a form with 2 buttons and when I hit enter, to have the focus to switch to the other button. You hit enter once and it sets a break on the serial port, and press it again to clear it.

There has to be a way of doing this without using a message processing loop and CreateWindowxxx. I tried calling DefWindowProc and DefDlgProc, from my dialog proc, but neither seems to work. There is an example here:

http://afsalm.wordpress.com/2008/09/04/how-to-set-focus-to-a-control-when-a-dialog-startup/

It's probably really easy, but for some reason I can't seem to grasp it.

The two buttons are B_SET and B_CLEAR. For instance I tried:

Code:
    invoke DefWindowProc,[hWnd],WM_NEXTDLGCTL,B_SET,TRUE
    


But nothing happens. Thanks for looking at this.


Description: Source Code
Download
Filename: Serial Bit Twiddler.asm
Filesize: 3.49 KB
Downloaded: 30 Time(s)

Post 20 Feb 2011, 23:39
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Maybe you should post the whole code ?

it says
Code:

push IDC_CONTROL_NAME
push hWndDlg
call   [GetDlgItem]

MOV [hWnd],EAX

push EAX
call   [SetFocus]
MOV EAX,FALSE             ; return FALSE

       JMP   .DefWindowProc

   OR

PUSH TRUE
PUSH hWnd
PUSH WM_NEXTDLGCTL
PUSH hWndDlg ; This window
CALL  [PostMessage]

MOV EAX,0 or XOR EAX,EAX ; return TRUE
       JMP    .DefWindowProc


    


Last edited by typedef on 21 Feb 2011, 00:37; edited 2 times in total
Post 21 Feb 2011, 00:23
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
I attached it, but here it is in post form. Or do you mean to post the variable declarations? They're in a separate file.

Code:
;=======================================================================
format PE GUI 4.0
;=======================================================================
include 'win32a.inc'    ;*
include 'rc.inc'        ;*
;=======================================================================
section '.code' code readable executable

entry $

    invoke GetModuleHandle,0
    mov [hInstance],eax
    invoke DialogBoxParam,eax,D_MAIN,0,dlg_proc,0
    invoke ExitProcess,0

;=======================================================================
proc dlg_proc, hWnd, uMsg, wParam, lParam
    cmp [uMsg],WM_CLOSE
    jne @F
.end_dlg:
    invoke EndDialog,[hWnd],0
.exit_true:
    mov eax,TRUE
    ret
@@:
    cmp [uMsg],WM_INITDIALOG
    jne @F
    invoke CreateFile,com1,GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0
    cmp eax,INVALID_HANDLE_VALUE
    je .ThrowError
    mov [FileHandle],eax
    invoke DefWindowProc,[hWnd],WM_NEXTDLGCTL,B_SET,TRUE
    jmp .exit_true
@@:    
;-----------------------------------------------------------------------
    cmp [uMsg],WM_COMMAND
    jne .exit_false
    mov eax,[wParam] ; Start looking for hotkeys
    dec eax                ; case 1 of wparam = enter pressed
    jz .SET                 ; enter key = toggle break status 
    dec eax                ; case 2 of wparam = esc pressed
    jz .end_dlg            ; esc = close window
;-----------------------------------------------------------------------
    cmp [wParam],B_CLEAR
    jne @F
.CLEAR:
    invoke ClearCommBreak,[FileHandle]
    call GetError
    invoke SetDlgItemText,[hWnd],EDIT_1,ErrorMsg
    jmp .exit_true
@@:
;-----------------------------------------------------------------------
    cmp [wParam],B_SET
    jne @F
.SET:
    invoke SetCommBreak,[FileHandle]
    call GetError
    invoke SetDlgItemText,[hWnd],EDIT_1,ErrorMsg
    invoke DefWindowProc,[hWnd],WM_NEXTDLGCTL,B_CLEAR,TRUE
    jmp .exit_true
@@:
;-----------------------------------------------------------------------
;    cmp [wParam],?? ; << add new command
;    jne @F
;    jmp .exit_true
;@@:
;-----------------------------------------------------------------------
.exit_false:
    xor eax,eax
    ret
;-----------------------------------------------------------------------
.ThrowError:
    invoke MessageBox,0,ErrorMsg,SerialErr,MB_OK
    jmp .end_dlg
;-----------------------------------------------------------------------
.Toggle:
    mov eax,[CommBreak]
    cmp eax,TRUE
    je B_CLEAR
    jmp B_SET
endp

proc GetError
    invoke GetLastError
    mov [ErrNum],eax
    invoke wsprintf,ErrorMsg,wstr,[ErrNum]
    add esp,12
        ret
endp
;=======================================================================

;=======================================================================
include 'idata.inc'
include  'data.inc'
;=======================================================================
section '.rsrc' resource data readable
;-----------------------------------------------------------------------
  directory RT_DIALOG,dialogs

;-----------------------------------------------------------------------
include "dialogs.tab" ;*
;-----------------------------------------------------------------------
include "dialogs.dat" ;*
;=======================================================================
    


Code:
section '.data' data readable writeable
;-----------------------------------------------------------------------
;   initialized data here
;

hello       db "hello",0
com1      db "COM1",0
SerialErr db "Serial Port Error!",0
ErrorMsg rb 50
FileHandle dd ?
wstr        db "Error Code %i",0
ErrNum   dd ?
CommBreak dd FALSE

;=======================================================================
align 4
;-----------------------------------------------------------------------
;   uninitialized data here
;

hInstance   dd ?    


dialogs.tab

Code:
  resource dialogs,\
    D_MAIN,LANG_ENGLISH+SUBLANG_DEFAULT,dialog0    

dialogs.dat

Code:
  dialog dialog0,"FBASE",0,0,178,48,0x10c80800,0x00000000,,"MS Sans Serif",8
    dialogitem "Edit","",EDIT_1,7,8,108,13,0x50010080,0x00000200
    dialogitem "Edit","",EDIT_2,7,28,108,13,0x50010080,0x00000200
    dialogitem "Button","Clear",B_CLEAR,122,27,49,15,0x50010000,0x00000000
    dialogitem "Button","Set",B_SET,122,7,49,15,0x50010001,0x00000000
  enddialog

    
Post 21 Feb 2011, 00:26
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
BTW, are you enjoying the snow as much as I am?
Post 21 Feb 2011, 00:33
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
I think you should put your DefWindowProc on it's own label so that every unprocessed MSG is sent to the system through it.
It seems like you are "holding" it.

Look at the code above.

Oh yeah, dude, It's snowing like crazy up here in Michigan... Storm O_o...

I hope I have a snow day so I don't waste my time on a 4 hr art class.. LOL
Post 21 Feb 2011, 00:34
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Quote:

jmp B_SET
<------I dont see a return op code here
endp
Post 21 Feb 2011, 00:40
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
Hehheh. I'm 'up here' as well.

I got it to work! For some reason, the call to GetDlgItem wasn't working right when I tried it before. This had me really upset. Thanks a lot! Laughing
Post 21 Feb 2011, 01:02
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
typedef wrote:
Quote:

jmp B_SET
<------I dont see a return op code here
endp


It's supposed to be
Code:
jmp .SET    
. The return opcode is called in .exit_true, which is called in .SET
Post 21 Feb 2011, 01:05
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
But why jump instead of setting it up there ? That takes up some space you know ?.

Aynway, happy belated snow day dude ! LOL Very Happy
Post 21 Feb 2011, 01:18
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
typedef wrote:
But why jump instead of setting it up there ?

I dunno. I used a template file and that's how it was set up. Here's the finished code, if you're interested. You can put an LED between TXD and GND on your COM1 port and watch it go on and off. The idea is to be able to identify an unknown serial cable.

Code:
;=======================================================================
format PE GUI 4.0
;=======================================================================
include 'win32a.inc'    ;*
include 'rc.inc'        ;*
;=======================================================================
section '.code' code readable executable

entry $

    invoke GetModuleHandle,0
    mov [hInstance],eax
    invoke DialogBoxParam,eax,D_MAIN,0,dlg_proc,0
    invoke ExitProcess,0

;=======================================================================
proc dlg_proc, hWnd, uMsg, wParam, lParam
    cmp [uMsg],WM_CLOSE
    jne @F
.end_dlg:
    invoke EndDialog,[hWnd],0
.exit_true:
    mov eax,TRUE
    ret
@@:
    cmp [uMsg],WM_INITDIALOG
    jne @F
    invoke CreateFile,com1,GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0
    cmp eax,INVALID_HANDLE_VALUE
    je .ThrowError
    mov [FileHandle],eax
    invoke GetDlgItem,[hWnd],B_SET
        invoke SetFocus,eax
    jmp .exit_false
@@:    
;-----------------------------------------------------------------------
;    cmp [uMsg],?? << add new message
;    jne @F
;    
;    jmp .exit_true
;@@:    
;-----------------------------------------------------------------------
    cmp [uMsg],WM_COMMAND
    jne .exit_false
    mov eax,[wParam] ; Start looking for hotkeys
    cmp eax,2            ; case 2 of wparam = esc pressed
    jz .end_dlg            ; esc = close window
;-----------------------------------------------------------------------
    cmp [wParam],B_CLEAR
    jne @F
.CLEAR:
    invoke ClearCommBreak,[FileHandle]
    call GetError
    invoke SetDlgItemText,[hWnd],EDIT_1,ErrorMsg
    invoke GetDlgItem,[hWnd],B_SET
    invoke PostMessage,[hWnd],WM_NEXTDLGCTL,eax,TRUE
    jmp .exit_true
@@:
;-----------------------------------------------------------------------
    cmp [wParam],B_SET
    jne @F
.SET:
    invoke SetCommBreak,[FileHandle]
    call GetError
    invoke SetDlgItemText,[hWnd],EDIT_1,ErrorMsg
    invoke GetDlgItem,[hWnd],B_CLEAR
    invoke PostMessage,[hWnd],WM_NEXTDLGCTL,eax,TRUE
    jmp .exit_true
@@:
;-----------------------------------------------------------------------
;    cmp [wParam],?? ; << add new command
;    jne @F
;    jmp .exit_true
;@@:
;-----------------------------------------------------------------------
.exit_false:
    xor eax,eax
    ret
;-----------------------------------------------------------------------
.ThrowError:
    invoke MessageBox,0,ErrorMsg,SerialErr,MB_OK
    jmp .end_dlg
;-----------------------------------------------------------------------
endp

proc GetError
    invoke GetLastError
    mov [ErrNum],eax
    invoke wsprintf,ErrorMsg,wstr,eax
        add esp,12
  ret
endp
;=======================================================================

;=======================================================================
include 'idata.inc'
include  'data.inc'
;=======================================================================
section '.rsrc' resource data readable
;-----------------------------------------------------------------------
  directory RT_DIALOG,dialogs

;-----------------------------------------------------------------------
include "dialogs.tab" ;*
;-----------------------------------------------------------------------
include "dialogs.dat" ;*
;=======================================================================

    
Post 21 Feb 2011, 01:57
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
well I don't have any serial devices or 'virtual ones'.

Plus, 'rc.inc' not found............You didn't post it up here.

Nevermind though.

I'm creating a biological virus cell implementation
right now.
Post 21 Feb 2011, 02:26
View user's profile Send private message Reply with quote
Lightning Stalker



Joined: 14 Aug 2008
Posts: 14
Lightning Stalker
lol, sorry.

Code:
D_MAIN equ 1000
EDIT_1 equ 1001
EDIT_2 equ 1002
B_CLEAR equ 1003
B_SET equ 1004
IDC_BUTTON1003 equ 1003
    
Post 21 Feb 2011, 03:30
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.