News:

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

So FRUSTRATING...I just want the buttons caption

Started by Trope, September 26, 2005, 11:09:46 PM

Previous topic - Next topic

Trope

I swear I have been at this for over an hour, maybe more.

I have a button - one simple button:



     IDC_BUTTON



it's in a dialog box, created from resource file (if that matters, not sure).

i tried this...


       
       ; Get HANDLE to my button
invoke GetDlgItem,hWin,IDC__BUTTON
        invoke GetWindowText, IDC__BUTTON, ADDR alarmBtnBuffer, 512
invoke MessageBox, 0, ADDR alarmBtnBuffer, SADD("BTN CAPTION"), MB_OK



Anyone help me out here? Sample code would be great, but even better would be WHY mine is not working!

THANKS!

Trope

Never Mind! UGH....

I had this code in a function and WASN'T pushing the handle to the dialog....

I feel so stupid!

gabor

Well...

I think you could be lost in the woods of WinApi window handling mechanism.

The little piece of code you posted contains not much info, but I try to help.
First of all is the hWin the handle of the dialog box that contains the button? According to the WinApi help:


HWND GetDlgItem(
    HWND  hDlg,       // handle of dialog box
    int   nIDDlgItem  // identifier of control
);


If you want to retrieve the caption/text of a control item you could use this function:


UINT GetDlgItemText(
    HWND    hDlg,        // handle of dialog box
    int     nIDDlgItem,  // identifier of control
    LPTSTR  lpString,    // address of buffer for text
    int     nMaxCount    // maximum size of string
);


Other way would be to send messages to the control items, like WM_GETTEXT or WM_SETTEXT message. Well, it is not an other way at all, since the Get/SetDlgItemText itself sends such messages. That is why I believe that when you already have the handle of those control items, you could send these messages too to make the code slightly faster. (I don't think this could make big difference in speed, maybe when you have lots of controls.)

On the other hand it is quite comfortable to use resource files, since the identifiers used there can be used in the WinAPI functions too.

I hope I've helped!

Greets, Gábor

Derry

Hi Trope,

I don't know if this is part of the problem but the 2 code sections you supplied seem to be different, ie

IDC_BUTTON  ; in the resource file i assume

IDC__BUTTON  ; when you attempt to use it

ignore me if I'm wrong

Derry

Trope

Well, this appears to work well ( for me anyways )



invoke GetDlgItem, hWin, IDC_ALARM
mov hAlarmButton, eax

        invoke GetWindowText, hAlarmButton, ADDR alarmBtnBuffer, 10




Is something wrong with this approach?

gabor

Hi!

There is nothing wrong with that approach! It is correct this way!

What I suggested
1. GetDlgItemText(hDlg,nIDDlgItem,lpString,nMaxCount) that uses the resource ID of the control item

2. send a message directly to the control item: SendMessage(hWnd,WM_GETTEXT,NUL,lpText), after getting the control's handle via
   GetDlgItem(hDlg,nIDDlgItem)

These are equivalent, because the 1st solution first gets the handle of the control item (like GetDlgItem does) and then sends the WM_GETTEXT message, like in the 2nd solution.

Your solution, using the GetWindowText is 100% the same, you can read it for yourself in the WinAPI help, that this function utilizes the WM_GETTEXT message sending method too. Here's the remark from the help: "This function causes a WM_GETTEXT message to be sent to the specified window or control."

I too had many problems till I understood a bit of these methods and message sending stuff. Well keep up learning! This part is rather Windows programing, not MASM and this knowledge is coding language independent. (You use the same API functions in C++ or C#)

Greets, Gábor