`SetWindowLong()` 函数即使在调用 `SetWindowPos()` 后也不会改变窗口样式 [英] `SetWindowLong()` function doesn't change window style even after calling `SetWindowPos()`

查看:33
本文介绍了`SetWindowLong()` 函数即使在调用 `SetWindowPos()` 后也不会改变窗口样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create the static control with the code below:

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样式,那么创建的静态控件就出现凹陷了.

If I enable the SS_SUNKEN style in the creation code above, the created static control appears sunken successfully.

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

But, what I'm trying to do is the change the control style after its creation.
I tried this:

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 的文档中,它说:

I call SetWindowPos() after calling SetWindowLongW() because in the documentation of SetWindowLong, it says:

某些窗口数据会被缓存,因此在调用 SetWindowPos 函数之前,您使用 SetWindowLong 所做的更改不会生效.具体来说,如果您更改任何框架样式,则必须使用 SWP_FRAMECHANGED 标志调用 SetWindowPos 才能正确更新缓存.

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 的文档中,它说:

And, in the documentation of SetWindowPos, it says:

如果您使用 SetWindowLong 更改了某些窗口数据,则必须调用 SetWindowPos 以使更改生效.对 uFlags 使用以下组合:SWP_NOMOVE |SWP_NOSIZE |SWP_NOZORDER |SWP_FRAMECHANGED.

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(),我的静态控件的样式也没有改变.

Even after changing SetWindowLongW() and SetWindowPos() the style of my static control does not change.

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

What am I doing wrong, or what am I missing?

推荐答案

SS_SUNKEN 在扩展样式(GWL_EXSTYLE)窗口中有效设置WS_EX_STATICEDGE很长,所以你可以适当地更新 GWL_EXSTYLE 并像你目前所做的那样重新定位.

SS_SUNKEN effectively sets WS_EX_STATICEDGE in the extended styles (GWL_EXSTYLE) window long, so you can update GWL_EXSTYLE appropriately and reposition as you're currently doing.

这篇关于`SetWindowLong()` 函数即使在调用 `SetWindowPos()` 后也不会改变窗口样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆