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

前端 未结 4 803
不思量自难忘°
不思量自难忘° 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();
    }
    
    0 讨论(0)
  • 2021-01-03 04:02

    If it's a text that's inside a control (Like a label), you can edit the property named "TextAlign" to accomplish what you want inside Windows Form Designer.

    Or programmatically,

    Label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    

    If you're talking about aligning the text of the title bar, there is no way to do this through Winforms. You will have to use something like this: http://www.codeproject.com/Articles/93959/WinForm-Extended

    0 讨论(0)
  • 2021-01-03 04:14

    Inside Windows Form Designer. Make the changes like this.lable1.AutoSize = false;

    Then:

    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    
    0 讨论(0)
  • 2021-01-03 04:23
     Graphics g = this.CreateGraphics();
            double fw = this.Width; // form width
            double tw = g.MeasureString(this.Text.Trim(), this.Font).Width; \\ text width
            double rp = (fw - tw) / 2;
            int tt = Convert.ToInt32(rp); 
            string st = " ";
            st = st.PadRight(tt / 3);
            this.Text = st + this.Text.Trim();
    
    0 讨论(0)
提交回复
热议问题