简体   繁体   中英

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):

在我编辑编辑之前

After Pressing OK button 按确定后

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM