News:

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

What is wrong in my code?

Started by Mo4x, January 30, 2006, 11:06:31 AM

Previous topic - Next topic

Mo4x

Normally I programming in Borland Delphi 7, but my cousin say me about assembler, and I start to learn this programming language.
I'm a new in assembler programming. I insert Button on Window1. There is my code.
.Const

.Data?

.Data

MsgText DB "Text!!!", 0
MsgCaption DB "Caption!!!", 0

.Code

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

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

Window1Button1 Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
Invoke MessageBox, 0, Addr MsgText, Addt MsgCaption, MB_OK
Invoke ExitProcess, 0
Return FALSE
Window1Button1 EndP

Ramon Sala

Hi Mo4x,

The problem is that Window1Button1 procedure receives all messages for Button1, so you have to filter them in order to process just the one(s) you need, if not all messages for Button1 call the message box and your program will probably crash. For example, if you decide the message box to appear when the mouse left button is pressed on Button1, just write the following code in the Button1 procedure:

Window1Button1 Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
   .If uMsg == WM_LBUTTONDOWN
      Invoke MessageBox, 0, Addr MsgText, Addt MsgCaption, MB_OK
      Return TRUE
   .EndIf
   Return FALSE
Window1Button1 EndP

Note the use of the .If/.EndIf directives to process just the WM_LBUTTONDOWN message.

In the other hand, if you want the message box to appear when the Button1 is clicked (mouse left button pressed and then released), you have to process the WM_COMMAND message through the owner window procedure (Window1) in the following way:

Window1Procedure Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
   .If uMsg == WM_CREATE
      
   .ElseIf uMsg == WM_COMMAND
      LoWord wParam
      .If Ax == IDC_WINDOW1_BUTTON1
         HiWord wParam
         .If Ax == BN_CLICKED
             Invoke MessageBox, 0, Addr MsgText, Addt MsgCaption, MB_OK
            Return TRUE
         .EndIf
      .EndIf

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

In that case, you can delete the whole Window1Button1 procedure unless you need to process any message just for Button1.

IMPORTANT: In all cases you never have to call the ExitProcess function. Instead, if you want to close the main window and exit the program, just send it an WM_CLOSE message in the following way:

      Invoke SendMessage, App.Main, WM_CLOSE, 0, 0
      Return TRUE

App.Main is the handle of the main window (in your code Window1) and is available from any part of the code, but if you send the message through the Window1 code (for example in the WM_COMMAND message) you also can use hWnd. After sending the WM_CLOSE message, the program is finished, so you should return TRUE.


Regards,

Ramon
Greetings from Catalonia

Mo4x

Thanks! Now I understand  :U . I'm accustomed to Delphi. I programming in Win32Asm for only 2 weeks. Big thanks.

Ramon Sala

Greetings from Catalonia

J4V

Hi Ramon,
I'm new in win32asm like Mo4x.
First, I want to say you congratulations, your IDE is amazing!
Second, I have a question about this thread, you told to Mo4x another way to make the same thing, write the code inside the Window1Button1 or inside the Window1Procedure, looks like the same to me. What way do you prefers and why you prefers this way?

J4V

PS: Sorry about my english, usually I talk in spanish... :)

Ramon Sala

Hi j4V,

Let's see. Windows O.S. already sends a message when a button has been clicked. This message arrives to its parent window through the WM_COMMAND message, so why not to use it and avoid writing code? If you control a button click through the Button object procedure, you have to control the WM_LBUTTONDOWN message and then, while the button is pressed, you have to control the WM_LBUTTONUP taking into account if it has been released while the mouse cursor was inside the button area (then it is a click) or outside the button area (then it is not a click). It is much easier to use the Windows message BN_CLICKED, received through the WM_COMMAND message in the parent window. In most cases you do not need the Button procedure at all, so you can delete it. The button procedure (or any other child control procedure) could be usefull, for example, to control the WM_LBUTTONUP (that is, when the mouse left button is released over the child control) in order to show a context menu.

Thanks for your kind words and for using Easy Code.

Regards,

Ramon
Greetings from Catalonia