简体   繁体   English

更新标题栏Winapi

[英]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. 但是,当我调用SetWindowTextW ,标题栏不会更改:它保持以前的状态。

edit_handle is the handle to an EDIT control. edit_handle是EDIT控件的句柄。

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 . 如前所述,问题似乎是edit_handle的实际值。

Remeber that your window procedure is called by Windows each time your window receives a message. 请记住,每次窗口接收到消息时,Windows都会调用窗口过程。 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) . 如果需要“记住”与窗口关联的数据,请查看WIN API函数SetWindowLongPtr(hwnd, GWLP_USERDATA, ...)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: 由于每个窗口都分配有唯一的ID,因此您可以使用以下语句获取编辑控件的窗口句柄:

edit_handle = GetDlgItem(hwnd, ... ); 

You have to replace ... by the id of your edit control. 您必须用编辑控件的ID替换... If you're creating the edit control by yourself by calling CreateWindow(..) this is the value of the hMenu attribute. 如果要通过调用CreateWindow(..)自己创建编辑控件,则这是hMenu属性的值。 If using a dialog coming from a resource it is simply the ID of the control. 如果使用来自资源的对话框,则它只是控件的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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