11

The SetWindowText function's documentation does not set a limit on the length of the string that may be used as a window title.

In the documentation for WM_SETTEXT (the message sent by calling SetWindowText), it is noted that the return value of this message's processing may be:

FALSE (for an edit control), LB_ERRSPACE (for a list box), or CB_ERRSPACE (for a combo box) if insufficient space is available to set the text in the edit control.

However, it says nothing about the case when a window's title is being set. Is a strict limit set, or is it up to the programmer to use common sense to provide their own title length limit?


I have posted this because I am developing a graphics engine which allows the user to supply their own title for the main window. The idea is that I would define a constant such as

const static int MAX_APP_TITLE_LENGTH = /* ??? */;

within my application class, and check the length of the user-provided title string against this.

If the title string is too long, I can throw a warning message and truncate it, rather than passing it straight into SetWindowText with unintended consequences.


EDIT: After some discussion in the comments, it seems that Windows will not complain even if a string of length 100,000 is used as a window title, so this issue is not worth worrying about (beyond the basic sanitization of input, of course)!

Archimaredes
  • 1,397
  • 12
  • 26
  • 4
    What is motivating the question? – David Heffernan Feb 01 '16 at 16:35
  • I don't think the operating system imposes a limit. You can set whatever length you want. The same API is used to set the text in controls, such as Edit controls, which can store an awful lot of text (64k maybe? why does that sound right?). There is a `WM_GETTEXTLENGTH` message you can use to retrieve the length if you're trying to figure out what size buffer to allocate. – Cody Gray - on strike Feb 01 '16 at 16:39
  • @DavidHeffernan I edited the question to clarify this. – Archimaredes Feb 01 '16 at 16:39
  • 1
    Just tried with 100'000 char string. It works. That should be enough. – Jabberwocky Feb 01 '16 at 16:41
  • 4
    I think you are seriously over thinking this. Just let the user supply as long a name as they want. If they supply a name that is too long, they won't be able to see it anyway. The screen runs out of pixels long before the system complains. – David Heffernan Feb 01 '16 at 16:42
  • @DavidHeffernan Fair enough. It is worth checking that nothing will break, anyway. Thanks. – Archimaredes Feb 01 '16 at 16:43
  • 3
    You are planning to break the [Zero one infinity rule](https://en.wikipedia.org/wiki/Zero_one_infinity_rule) for no apparent reason. Let the user pass a `std::wstring`, and don't impose an arbitrary limit. If sending `WM_SETTEXT` fails, throw an exception. – IInspectable Feb 01 '16 at 18:33

2 Answers2

3

There is technically no limit to the title size, but the lpClassName field has a strict limit of 256 chars (i didnt want you to think you could have an infinite class name and your code crash.)

SOURCE: https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexw

0

If we are talking about a standard windows title, it appears that you can set any length you want using SetWindowText without error. GetWindowText will also return whatever title you have set. Unfortunately, only 255 characters will be shown in the window title bar!