c++ - `SetWindowLong()` 函数不执行 't change window style even after calling ` SetWindowPos()`

标签 c++ winapi win32gui window-style

normal static control sunken static control

我使用以下代码创建静态控件:

hWnd = CreateWindowExW( 0,
                        L"STATIC",
                        Content.c_str(),
                        SS_LEFT | WS_VISIBLE | WS_CHILD /*| SS_SUNKEN*/,
                        200,
                        120,
                        120,
                        40,
                        hWndParent,
                        NULL,
                        hInstance,
                        NULL);

如果我在上面的创建代码中启用 SS_SUNKEN 样式,创建的静态控件会成功地显示为凹陷。

但是,我要做的是在控件创建后更改控件样式。
我试过这个:

void BaseWindowClass::AddStyle(DWORD NewStyle)
{
    // NewStyle     = 0x00001000 = SS_SUNKEN
    LONG oldstyle, changedstyle;
    oldstyle=SetWindowLongW(hWnd, GWL_STYLE, changedstyle=GetWindowLongW(hWnd, GWL_STYLE) | NewStyle);
    UpdateWindowStyles();
    // oldstyle     = 0x50000000
    // changedstyle = 0x50001000 (everything looks normal)
}
void BaseWindowClass::UpdateWindowStyles()
{
    BOOL success;
    success=SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    // success = 0x00000001 (non-zero: SetWindowPos sucseeded)
}

文档:
SetWindowLong()
SetWindowPos()

我在调用 SetWindowLongW() 之后调用了 SetWindowPos(),因为在 SetWindowLong 的文档中,它说:

Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.

并且,在 SetWindowPos 的文档中,它说:

If you have changed certain window data using SetWindowLong, you must call SetWindowPos for the changes to take effect. Use the following combination for uFlags: SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED.

即使在更改 SetWindowLongW()SetWindowPos() 之后,我的静态控件的样式也没有改变。

我做错了什么,或者我错过了什么?

最佳答案

SS_SUNKEN 有效地在扩展样式 (GWL_EXSTYLE) 窗口中设置 WS_EX_STATICEDGE,因此您可以更新 GWL_EXSTYLE适本地重新定位,就像你目前所做的那样。

关于c++ - `SetWindowLong()` 函数不执行 't change window style even after calling ` SetWindowPos()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896059/

相关文章:

c++ - 派生类中具有相同名称但不同签名的函数

c++ - Boost信号2未编译

c++ - C++中的多重继承和多态性

forms - 使用 Z 顺序和位置在 MS Access 中组织打开的表单

c++ - 是否有相当于 slmgr.vbs/skms 的 C++/Winapi

windows - 如何在用户不活动一段时间后锁定应用程序?

c++ - 按钮按下处理 win32 c++

c++ - 如何使用 ReadFile 修复乱码?

c++ - 参数或方法重载的魔数(Magic Number)?

c++ - win32应用程序、windows窗体应用程序和控制台应用程序有什么区别?