views:

698

answers:

4

Is there a way to center-align the text in a WinForms form? Also known as the title bar caption or title bar text? So far the only way I can see to do it is pad the string with spaces. I am setting the title bar caption using the Form.Text property.

I should add that I am using a 3rd party ribbon form, so the app looks like a Microsoft Office 2007 application. And those apps center-align the text, presumably because when the text is left-aligned it gets added to the jumble of buttons on the top left and looks bad.

+2  A: 

Honestly - don't. Windows users expect certain things to work in a certain way, and this would not meet standard practices. Not to mention that the button in the taskbar would then no longer show the titlebar text as it would be pushed to the right.

You can take over the non-client area of a form completely, in which case you can do what you like. Even if you did this, though, my recommendation for your design would be to have the title at the top left, close button at the top right, etc.

Neil Barnwell
With the ribbon bar, users expect the text to be centered, and I am using a ribbon bar. Sorry I left that out before; I didn't think it was relevant to the question.
skypecakes
Ribbon bar custom draws the title bar - you can tell because buttons are added to the caption.
Michael
Good point. In retrospect, this is a question better posed to the 3rd party provider. I'm checking with them.
skypecakes
+1  A: 

The title bar is rendered by the system and there is no option for centering the text.

In order to effectively center, you'd need to draw the title bar yourself - this can be done in native code by handling WM_NCPAINT messages and such but not sure how this can easily be done in .NET.

But why do you want to change? Windows UX standards have the text left aligned.

Michael
A: 

I don't believe you can.

You could hide the title bar, and replace it with a user control and implement the same functionality a title bar has, but I don't think that would be a good idea.

Consistency for the user is probably more important than whatever reason you have for wanting to center the text.

Brandon
Actually, consistency is the reason I want to do it. See updated question. Thanks for your suggestion. Hope I can find something easier than that!
skypecakes
+1  A: 

For anyone who is interested, I am using Syncfusion Essential Tools. The solution is this:

this.ribbonToolbar.TitleAlignment = Syncfusion.Windows.Forms.Tools.TextAlignment.Center;

The title alignment is a property of the ribbon toolbar and not of the RibbonForm, which explains why I didn't find it before. Thanks to all who responded.

skypecakes