How To Center Align the Title Bar text In Windows Form?

前端 未结 4 889
不思量自难忘°
不思量自难忘° 2021-01-03 04:00

I am developing a Windows Form Application. I want to Align the Text to Center or say to Right of Title Bar of Form. How can I do it ??

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 04:01

    It can be done with custom form - you will have to create your own title bar. See V4Vendettas comment;

    Other approach (link) - is to create your own handler for form resize and insert there followong code. It will add appropriate amount of spaces from the left size of text. However you will have to add form.Refresh() and call that method in form.Load; also your window will have "..." as a text in task bar.

    private void UpdateTextPosition()
    {
        Graphics g = this.CreateGraphics();
        Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
        Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
        String tmp = " ";
        Double tmpWidth = 0;
    
        while ((tmpWidth + widthOfASpace) < startingPoint)
        {
           tmp += " ";
           tmpWidth += widthOfASpace;
        }
    
        this.Text = tmp + this.Text.Trim();
    }
    

提交回复
热议问题