News:

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

Trying to create a moving button

Started by ekisner, December 30, 2010, 09:48:20 PM

Previous topic - Next topic

ekisner

So, I'm experimenting - poking things in various ways, doing pointless things, so that I can learn.

Specifically, I've created a button within a window, and when clicking that button I want it's X position to change by 15.

I'm doing my best to avoid the higher level macro stuff (.while, for example) as I wish to learn the language before I learn the shortcuts.  This is the reason for the large number of jumps and labels.  I know that invoke is one such shortcut, but I understand how to push before using call and it does clean code up a lot!

So, on to the code.  This is within my WndProc.

_WM_CREATE:
        invoke CreateWindowEx, NULL, addr ButtonClass, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, Button1X, Button1Y, 80, 30, hWnd, Button1ID, hInstance, NULL
        mov hButton1, eax
        jmp _END

snip
_WM_COMMAND:
        mov eax,wParam

        cmp ax, Button1ID
      je _Button1Pressed
        jmp _END

_Button1Pressed:
        shr eax,16
        cmp ax, BN_CLICKED
        jne _END
            invoke DestroyWindow, hButton1
            add Button1X, 15
            invoke CreateWindowEx, NULL, addr ButtonClass, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, Button1X, Button1Y, 80, 30, hWnd, Button1ID, hInstance, NULL
            invoke InvalidateRect, hWnd, NULL, TRUE
        jmp _END

snip

_END:
        xor eax,eax
        ret
    WndProc endp


So now in terms of behavior, the first click works fine.  The button moves 15px over.  Any further clicks, however, and the old button does not disappear, and the newly created button simply draws over top of it 15px over.  I am able to click both the old button and the new button.

Gunner

Do you WANT 2 buttons?  No, I am thinking you don't and just want to move your button by 15 each click?

Something like this should work:
add  Button1X, 15
invoke MoveWindow, hButton1, Button1X, Button1Y, 80, 30, TRUE

instead of destroying an creating a window in button1pressed
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

ekisner

Ah cool - guess I should have researched the WinAPI a bit better.  Your different API call worked perfectly.

Do you perhaps know why it would destroy the first control correctly, but not destroy subsequent controls?

Gunner

Quote from: ekisner on December 30, 2010, 10:12:22 PM
Do you perhaps know why it would destroy the first control correctly, but not destroy subsequent controls?

MoveWindow does not destroy a window..  It will move the window (or in your case a button) and redraw it.  It will move whatever window you pass MoveWindow the handle to...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

ekisner

Quote from: Gunner on December 30, 2010, 10:31:52 PM
MoveWindow does not destroy a window..  It will move the window (or in your case a button) and redraw it.  It will move whatever window you pass MoveWindow the handle to...

This I understand, I was referring to DestroyWindow not destroying the subsequently created windows.  Sorry for being unspecific.

Gunner

CreateWindow returns a handle to the newley created window (control) but you are not saving it.

_Button1Pressed:
        shr eax,16
        cmp ax, BN_CLICKED
        jne _END
            invoke DestroyWindow, hButton1
            add Button1X, 15
            invoke CreateWindowEx, NULL, addr ButtonClass, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, Button1X, Button1Y, 80, 30, hWnd, Button1ID, hInstance, NULL
        ; -----> you should be saving the handle here <----
all your button handles cannot be saved in the variable hButton1, you could create a struct for your handles...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

ekisner

AAAAAH!!  It makes perfect sense... I feel kinda stupid now.  Saved the initial button handle, just didn't save any of the others.

Thank you so much for explaining it to me.