News:

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

I'm really losing it ...

Started by tbohon, August 09, 2011, 09:11:19 PM

Previous topic - Next topic

tbohon

Simple Windows executable - one static text box, a 'change' button and an 'exit' button on the form.  Nothing complicated.

I can't get either of the buttons to work - nothing happens with either of them, period.  Not sure what I've missed but here's my code, maybe someone can show me what I'm doing wrong with the Invoke SendMessage statements (which is where I presume the problems lie) ...



.Const

.Data?

.Data
mystring DB 'My string is here!', 0

.Code

Window1Procedure Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
.If uMsg == WM_CREATE

.ElseIf uMsg == WM_COMMAND

.ElseIf uMsg == WM_CLOSE
Invoke IsModal, hWnd
.If Eax
Invoke EndModal, hWnd, IDCANCEL
Mov Eax, TRUE ;Return TRUE
Ret
.EndIf
.EndIf
Xor Eax, Eax ;Return FALSE
Ret
Window1Procedure EndP

Window1Static1 Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
Xor Eax, Eax ;Return FALSE
Ret
Window1Static1 EndP

Window1btnChange Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM

Invoke SendMessage, hWnd, Addr mystring, 0, 0

Xor Eax, Eax ;Return FALSE
Ret
Window1btnChange EndP

Window1btnExit Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
Invoke SendMessage, hWnd, WM_CLOSE, 0, 0
Xor Eax, Eax ;Return FALSE
Ret
Window1btnExit EndP



Thanks in advance.

Tom

Ramon Sala

#1
Hi tbohon,

Thanks for using Easy Code!

First of all, you have to filter messages in Window1btnChange and Window1btnExit procedures if you are going to process one or more messages for the corresponding object. Otherwise, as all messages fired by buttons are sent to its window procedure (if existing), it will run the instructions being there for each message and the application may crash. So, you can do the following:

Window1btnExit Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
    .If uMsg == WM_LBUTTONDOWN
        Invoke SendMessage, App.Main, WM_CLOSE, 0, 0
        Return TRUE
    .EndIf
    Xor Eax, Eax ;Return FALSE
    Ret
Window1btnExit EndP

But the WM_CLOSE message has to be sent to the main window, which you can get with the Main member of the App object, that is App.Main.

Also, in the Window1btnChange procedure, which message do you want to send? The second parameter (which has to be the message) is Addr mystring, which is not a message. If you like (I guess) to set the mystring text to the Static1 object, then you first need to get the Static1 handle like this:

Window1btnChange Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
    .If uMsg == WM_LBUTTONDOWN
        Invoke GetWindowItem, hWnd, IDC_WINDOW1_STATIC1
        Invoke SendMessage, Eax, WM_SETTEXT, 0, Addr mystring
        Return TRUE
    .EndIf
    Xor Eax, Eax ;Return FALSE
    Ret
Window1btnChange EndP


However, as you want to do this work when the button is clicked, remove the Window1Static1, Window1btnChange and Window1btnExit procedures and place the following code in the main window, which receives all notifications from its children through the WM_COMMAND message (added code in red chars):

Window1Procedure Proc hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
    .If uMsg == WM_CREATE

    .ElseIf uMsg == WM_COMMAND
        LoWord wParam
        .If Ax == IDC_WINDOW1_BTNCHANGE
            HiWord wParam
            .If Ax == BN_CLICKED
                Invoke GetWindowItem, hWnd, IDC_WINDOW1_STATIC1
                Invoke SendMessage, Eax, WM_SETTEXT, 0, Addr mystring
                Return TRUE
            .EndIf
        .ElseIf Ax == IDC_WINDOW1_BTNEXIT
            HiWord wParam
            .If Ax == BN_CLICKED
                Invoke SendMessage, hWnd, WM_CLOSE, 0, 0
                Return TRUE
            .EndIf
        .EndIf
    .ElseIf uMsg == WM_CLOSE
        Invoke IsModal, hWnd
        .If Eax
            Invoke EndModal, hWnd, IDCANCEL
            Mov Eax, TRUE ;Return TRUE
            Ret
        .EndIf
    .EndIf
    Xor Eax, Eax ;Return FALSE
    Ret
Window1Procedure EndP


I hope this can help you and you can understand how Windows child controls work.

Regards,

Ramon


Greetings from Catalonia

tbohon

Thank you Ramon.  Obviously I need to get back to my Petzold book ...  :(

Appreciate it!

Tom