I need to calculate Windows Form Width for displayed text. The form width is obviously in pixels so you would just want to obtain the width of the text in pixels but this doesn't work:
animationForm.Width = TextRenderer.MeasureText(_caption, animationForm.Font).Width;
The calculated form width is way too small (cuts off text) - what is going on here? MeasureText uses pixels, form width is in pixels.
This works better but I don't think is right:
animationForm.Width = (int)(_caption.Length * animationForm.Font.Size);
Help would be appreciated.
AnimationPic - PictureBox, Caption is set on a Label
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PrlSystems.MaimInvoicing.Forms
{
public partial class ProgressIndicatorForm : Form
{
private volatile bool _animating = false;
private Form _parent;
private string _caption = "";
private object _mutex = new object();
public ProgressIndicatorForm(Form parent)
{
InitializeComponent();
_parent = parent;
}
public string Caption
{
set { _caption = value; }
get { return _caption; }
}
public void StartAnimation()
{
lock (_mutex)
{
if (!_animating)
{
if (_parent != null)
{
_parent.Cursor = Cursors.WaitCursor;
_parent.Enabled = false;
}
_animating = true;
_SpawnAnimationThread();
}
}
}
public void StopAnimation()
{
lock (_mutex)
{
if (_animating)
{
_animating = false; // stop animation thread
if (_parent != null)
{
// restore parent form UI
_parent.Cursor = Cursors.Default;
_parent.Enabled = true;
_parent.Activate();
}
}
}
}
private void _SpawnAnimationThread()
{
Thread animationThread = new Thread(new ThreadStart(_Animate));
animationThread.Priority = ThreadPriority.Normal;
animationThread.IsBackground = true; // should terminate when application ends
animationThread.Start();
}
private void _Animate()
{
ProgressIndicatorForm animationForm = new ProgressIndicatorForm(_parent);
animationForm.CaptionLabel.Text = _caption;
animationForm.StartPosition = FormStartPosition.CenterScreen;
animationForm.TopMost = true;
animationForm.Width = _CalculateFormWidth(animationForm);
animationForm.Show();
while (_animating)
{
animationForm.CaptionLabel.Text = _caption;
animationForm.Width = _CalculateFormWidth(animationForm);
animationForm.BringToFront();
animationForm.Refresh();
}
animationForm.Close();
animationForm.Dispose();
}
private int _CalculateFormWidth(ProgressIndicatorForm animationForm)
{
int width = AnimationPic.Location.X + AnimationPic.Width +
TextRenderer.MeasureText(_caption, animationForm.Font).Width;
return width;
}
}
}
Image in the designer:
You also need to take account the extra sizes such as width of the form. Space around the label(if you are using label?)
Rather than
Form.Width
, you need to useForm.ClientSize.Width
.The latter will return the actual width of the form's client area; i.e., the place where you can draw stuff. From the Remarks section in the documentation:
Contrast that with
Form.Width
(orForm.Size.Width
), which returns the entire width of the form as it appears on the screen, including superfluous stuff in the non-client area, like window borders. Definitely don't waste any time trying to calculate and remove the dimensions for these items manually; it's already been done for you.Using the following code:
I get this:
Looks pretty good to me...
Have you tried specifying device context? Try this method overload
MeasureText(IDeviceContext, String, Font, Size)
.When you use
Form.Width
, the width contains also width of the window border. Try this: