CStatic 不会在每次更改其文本时都失效

2022-01-12 00:00:00 static text c++ mfc invalidation

我正在尝试动态更改 CStatic 控件的文本.我的成员变量名为 mStatic,类型为 CStatic.我已将 ID 更改为 IDC_MYSTATIC 而不是 IDC_STATIC.

I am trying to dynamically change the text of a CStatic control. My member variable is called mStatic of the type CStatic. I have changed the ID to IDC_MYSTATIC instead of IDC_STATIC.

当我想更改控件的文本时,我正在调用 mStatic.SetWindowText("asdfasdf").我会定期在计时器中执行此操作.

I am calling mStatic.SetWindowText("asdfasdf") when I want to change the text of the control. I do this periodically in a timer.

现在我遇到的问题是调用 SetWindowText() 后之前的文本没有被删除.它只是不断堆积,直到我在屏幕上弄得一团糟.

Now I have the problem that the previous text is not erased after I call the SetWindowText(). It just keeps piling up until I get a mess on the screen.

父窗口具有带位图背景的分层属性.我还设置了 color_key 属性,因此位图的某种颜色被视为透明(即它不会被绘制,并且会让鼠标消息通过).mStatic 控件绘制在具有位图背景的不透明部分上.

The parent window has the layered property with a bitmap background. I have also set the color_key property so a certain color of the bitmap is viewed as transparent (I.e. It will not be drawn and will let mouse messages through). The mStatic control is drawn on the parts not transparent, that have a bitmap background.

为什么窗口没有失效?

推荐答案

遇到了同样的问题.以下代码修复了它:

Had the same issue. The following code fixed it:

mStatic.SetWindowText("New text");
CRect rect;
mStatic.GetWindowRect(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect);
UpdateWindow();

相关文章