none
How to position MDI form windows title bar text at center RRS feed

  • Question

  • i tried this code but did not worked

    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(); } private void frmMain_SizeChanged(object sender, EventArgs e) { UpdateTextPosition(); }

    private void frmMain_Load(object sender, EventArgs e)
            {
                UpdateTextPosition();

    }

    above code doesn not work. i am working with MDI form and its window state is Maximized. share some suggestion to get the code worked. thanks

    Friday, October 25, 2019 3:33 PM

Answers

  • Hi Sudip_inn,

    Maybe you can try to modify the code like this.

        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 += "1 ";
            tmpWidth += widthOfASpace;
        }
    
        this.Text = tmp.Replace("1", " ") + text.Trim();

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, November 5, 2019 8:03 AM
    Monday, October 28, 2019 2:44 AM
    Moderator