我需要计算显示文本的Windows窗体宽度。 形式宽度显然是在像素所以你只是想获得文本的宽度,以像素为单位,但是,这并不工作:计算显示文本的Windows窗体宽度
animationForm.Width = TextRenderer.MeasureText(_caption, animationForm.Font).Width;
计算的表格宽度来说太小了(切断文本) - 这里发生了什么? MeasureText使用像素,表单宽度以像素为单位。
这工作好,但我不认为这是正确的:
animationForm.Width = (int)(_caption.Length * animationForm.Font.Size);
帮助,将不胜感激。
AnimationPic - 图片框, 标题上一个标签组
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;
}
}
}
形象设计师:
我要澄清一些我遗漏的东西,但这很重要。我也有动画片。以下是整个公式:int width = AnimationPic.Location.X + AnimationPic.Width + TextRenderer.MeasureText(_caption,animationForm.Font).Width; – ActiveX 2011-05-18 14:08:35