首页 > 其他分享 >如何解决 WinForm窗体标题字符数限制 导致的显示不全问题?

如何解决 WinForm窗体标题字符数限制 导致的显示不全问题?

时间:2024-03-19 18:35:40浏览次数:26  
标签:tmp 字符 tmpWidth 不全 标题 窗体 WinForm

现在需要对窗体标题进行居中显示,通过在标题内容前增加空格的方式达到该目的。

实测是发现窗口标题的字符数量受到操作系统限制

网上查询的最大标题字符数是260个字符

实测最大字符数为587个

下面的代码可以勉强解决“由于最大字符数受到操作系统的限制导致最大化时显示不全”的问题

 1         string iniTitle = "";
 2         private void TitleCenterize()
 3         {
 4             this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
 5             string titleMsg;
 6             if (this.Text.Length<=20)
 7             {
 8                 iniTitle = this.Text;
 9                 titleMsg = this.Text;
10             }
11             else
12             {
13                 titleMsg = iniTitle;
14             }
15             
16             Graphics g = this.CreateGraphics();
17             Double startingPoint = (this.Width / 2) - (g.MeasureString(titleMsg, this.Font).Width / 2);
18             Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
19             String tmp = " ";
20             Double tmpWidth = 0;
21             int maxTitleLenght = 587;
22             if (startingPoint >= maxTitleLenght)
23             {
24                 startingPoint = maxTitleLenght;
25             }
26             while ((tmpWidth + widthOfASpace) < startingPoint)
27             {
28                tmp += " ";
29                tmpWidth += widthOfASpace;
30              }
31             this.Text = tmp + titleMsg;
32             int textLength = tmp.Length;
33         }

 

标签:tmp,字符,tmpWidth,不全,标题,窗体,WinForm
From: https://www.cnblogs.com/Nikole/p/18083667

相关文章

  • 1.字符串专题
    字符串专题\(A\)CF1037HSecurity\(B\)CF1073GYetAnotherLCPProblem\(C\)CF906EReverses\(D\)CF666EForensicExamination\(E\)P4199万径人踪灭\(F\)CF1535FStringDistance\(G\)CF1400Fx-primeSubstrings\(H\)CF955DScissors\(I\)CF153......
  • C语言进阶篇之字符函数和字符串函数(含模拟实现库函数)
    本篇主要整理了C语言字符函数和字符串函数的介绍,使用,以及库函数的模拟,持续更新中。老铁们,整理不易,创作不易,先赞后看养成习惯,你的支持是对我更新最大的鼓励!函数介绍与模拟实现1.1strlen求字符串长度size_tstrlen(constchar*str);注:1.字符串已经'\0'作为结束标......
  • HJ2 计算某字符出现次数
    https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1?tpId=37&tqId=21225&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=写......
  • 459. 重复的子字符串c
    voidbuild(int*next,char*s,intn){next[0]=-1;intindex=1,j=-1;while(index<n){if(j==-1||s[index-1]==s[j]){j++;next[index++]=j;}else{j=next[j];}}for(inti=0;......
  • 28. 找出字符串中第一个匹配项的下标c
    voidbulid(int*next,char*s,intn){next[0]=-1;intindex=1,j=-1;while(index<n){if(j==-1||s[index-1]==s[j]){j++;next[index++]=j;}else{j=next[j];}}}intstrStr(c......
  • C语言判断回⽂字符串
    使用C语言判断这个字符串是否是回⽂字符串(字符串的长度小于等于30,字符串不包含空格),如果是回文字符串输出Yes,如果不是回⽂字符串输出No。回文串:是一个正读和反读都一样的字符串方法:1.使用两个指针分别放置在字符串的头和尾,进行比较,如果相同,则头++,尾–.不同则结束判断......
  • java判断拼音字符串是不是汉字全拼
    publicstaticvoidmain(String[]args){Stringstr="wange";Stringstr1="huanggong";Stringstr2="wang文胜";Stringstr3="heihiyijiaren";Stringstr4="huangt......
  • 字符串增加千分位 负号提前处理
    *千分位DATA:LV_STRTYPESTRING,LV_CURRCTYPESTRING,LV_TMP1TYPESTRING,LV_TMP2TYPESTRING,LV_LENTYPEI,LV_LEN1TYPEI,LV_LEN2TYPEI,LV_LEN3TYPEI,LV_LEN4TYPEI.DATA:LV_CURRTYPEBAPICURR-BA......
  • 字符串的处理
    目录1、str.lower()2、str.upper()3、str.split(sep=None)4、str.count()5、str.find()6、str.index()7、str.startwith()、str.endswith()8、str.replace(old,news)9、str.center(width,fillchar)首先,字符串在python中是一个不可变的数据类型;其次,字符串包含了许多......
  • BCD转字符串
    //////BCD转字符串/////////偏移量///数字字符串位数//////publicstaticstringBCDToString(byte[]bcdNum,intoffset,intnumlen){stringretString="";intres;varlen=Math.DivRem(numlen,2,outres);if(res!=0){len++;}for(i......