如何使 WPF 窗口标题的文本长度大于 255 个字符? [英] How to make the text length of the title of a WPF Window greater 255 characters?
问题描述
在 VS2015 中创建 WPF 应用程序.将窗口的标题设置为长度为 290 的字符串.
Created WPF application in VS2015. Set the Window's title to a string of length 290.
<代码>标题= 123456789 223456789 323456789 423456789 523456789 623456789 723456789 823456789 923456789 133456789 233456789 333456789 433456789 533456789 633456789 733456789 833456789 933456789 143456789 243456789 343456789 443456789 543456789 643456789 743456789 843456789 943456789 153456789 253456789"; 代码>
当应用程序运行时,标题被截断为 255 个字符(最大化窗口和跨 2 个屏幕扩展的窗口).
When the application is run, the title is truncated to 255 characters (maximised window and window extended across 2 screens).
- 如何增加显示字符数?
- WPF 窗口标题的最大允许长度是多少?
我试过了
- 如何将 WPF 窗口的宽度设置为其标题栏中的内容?
- 在代码和 XAML 中设置标题栏文本.
推荐答案
255 个字符似乎是 DefWindowProc()
原生窗口函数的内部限制.
It seems that 255 chars is an internal limitation in the DefWindowProc()
native window function.
WPF 使用对本机方法的调用来更改窗口的标题:
WPF changes the windows's title using a call to a native method:
UnsafeNativeMethods.SetWindowText(new HandleRef(this, CriticalHandle), title);
这个方法接受任何长度的字符串,所以它应该可以实际工作.这不是 WPF 问题.
This method accepts strings of any length, so that should actually work. This is not a WPF problem.
但是……
即使您创建一个使用 DefWindowProc()
函数的标准和本机 Win32 窗口,您也会观察到相同的行为 - 窗口标题中最多 255 个字符.
Even if you create a standard and native Win32 window that uses the DefWindowProc()
function, you will observe the same behavior - maximum 255 characters in the window title.
Windows 绘制标题栏以响应 WM_NCPAINT
消息(连同窗口框架、系统按钮等).在处理 WM_NCPAINT
消息的 DefWindowProc()
代码中,该代码调用 GetWindowText()
以获取窗口的标题字符串.在这个对 GetWindowText()
的调用中,缓冲区的大小似乎是 255 个字符(在旧的 Windows 中,缓冲区只有 79 个字节).这将窗口标题的长度限制为 255 个字符.
Windows paints the title bar in response to a WM_NCPAINT
message (along with the window frames, the system button, and so on). Inside the DefWindowProc()
code that handles WM_NCPAINT
messages, the code calls GetWindowText()
to get the window's title string. In this call to GetWindowText()
, the size of the buffer seems to be 255 chars (in old Windows, the buffer was only 79 bytes). This limits the length of a window's title to 255 characters.
这篇关于如何使 WPF 窗口标题的文本长度大于 255 个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!