March 22nd, 2012

Why does my window style change when I call SetWindowText?

A customer observed some strange behavior with window styles:

We ran some weird behavior: Calling the Set­Window­Text function causes a change in window styles. Specifically, calling Set­Window­Text results in the WM_STYLE­CHANGING and WM_STYLE­CHANGED messages, and sometimes the result is that the WS_TAB­STOP style is removed. Is this a bug? What would cause this?

The Set­Window­Text message sends the WM_SET­TEXT message to the control, at which point anything that happens is the window’s own responsibility. If it wants to change styles based on the text you sent, then that’s what happens. The window manager doesn’t do anything special to force it to happen or to prevent it.

That’s weird, because I’m not even listening for WM_SET­TEXT messages. I also verified that there is no call into my code during the call to the the Set­Window­Text function.

I’m assuming that the window belongs to the same process as the caller. If the window belongs to another process, then the rules are different.

I’m changing the text of a window created by the same thread.

Okay, so let’s see what we have so far. The customer is calling the Set­Window­Text function to change the text of a window created on the same thread. There is no handler for the WM_SET­TEXT message, and yet the window style is changing. At this point, you might start looking for more obscure sources for the problem, like say a global hook of some sort. While I considered the possibilities, the customer added,

It may be worth noting that I’m using the Sys­Link.

Okay, now things are starting to make sense, and it didn’t help that the customer provided misleading information in the description of the problem. For example, when the customer wrote, “There is no handler for the WM_SET­TEXT message,” the customer was not referring to the window whose window text is changing but to some other unrelated window. It’s like responding to the statement “A confirmation letter should have been sent to the account holder” with “I never got the confirmation letter,” and then the person spends another day trying to figure out why the confirmation letter was never sent before you casually mention, “Oh, I’m not the account holder.” The WM_SET­TEXT message is sent to the window you passed to Set­Window­Text; in this case, it’s the Sys­Link window. It is therefore the window procedure of the Sys­Link window that is relevant here.

The Sys­Link control remembers whether it was originally created with the WS_TAB­STOP, and if the markup it is given has no tab stops, then it removes the style; if the markup has tab stops, then it re-adds the style.

How do I add a tab stop to a string? I couldn’t find any reference to it and all my guesses failed.

The tab stops in question are the hyperlinks you added when you used the <A>...</A> notation. If the text has no hyperlinks, then the control removes the WS_TAB­STOP style because it is no longer something you can tab to.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.