lost_in_the_source
lost_in_the_source

Reputation: 11237

Updating Title Bar Winapi

I am updating the title bar of a window:

/* inside the window procedure */
HWND edit_handle;
/* ... */
case WM_COMMAND: {
    if (LOWORD(wParam) == 2) { /* 2 is the code for the button */
        int len = GetWindowTextLengthW(edit_handle);
        if (len > 0) {
            wchar_t buf[len + 1];
            GetWindowTextW(edit_handle, buf, len + 1);
            SetWindowTextW(hwnd, buf);
        }
    }
    break;
    }

However, when I call SetWindowTextW, the title bar does not change: it remains the way it was before.

edit_handle is the handle to an EDIT control.

Before(when the window just loaded):

Before I edited the EDIT

After Pressing OK button After Pressing the OK

Upvotes: 1

Views: 189

Answers (1)

Lukas Thomsen
Lukas Thomsen

Reputation: 3207

As pointed out before the problem seems to be the actual value of edit_handle.

Remeber that your window procedure is called by Windows each time your window receives a message. Therefore the values of your local variables assigned while processing a previous message are gone...

If you need to "remember" data associated with your window look at the WIN API functions SetWindowLongPtr(hwnd, GWLP_USERDATA, ...) and GetWindowLongPtr(hwnd, GWLP_USERDATA). Those functions set and query a "variable" of the window which is big enough to hold a pointer to some data to remeber.

In your case the solution is simpler. Since each window has an unique id assigned to it you can use the following statement to obtain the window handle of your edit control:

edit_handle = GetDlgItem(hwnd, ... ); 

You have to replace ... by the id of your edit control. If you're creating the edit control by yourself by calling CreateWindow(..) this is the value of the hMenu attribute. If using a dialog coming from a resource it is simply the ID of the control.

Upvotes: 4

Related Questions