本文整理汇总了C#中System.Windows.Forms.ListBox.CreateGraphics方法的典型用法代码示例。如果您正苦于以下问题:C# ListBox.CreateGraphics方法的具体用法?C# ListBox.CreateGraphics怎么用?C# ListBox.CreateGraphics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.ListBox
的用法示例。
在下文中一共展示了ListBox.CreateGraphics方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DisplayHScrollOnListBoxIfNecessary
// Fix for Dev10 Bug 592077: Display Horizontal Scroll bar if the name exceeds the container.
internal static void DisplayHScrollOnListBoxIfNecessary(ListBox listBox)
{
// Display a horizontal scroll bar if necessary.
listBox.HorizontalScrollbar = true;
// Create a Graphics object to use when determining the size of the largest item in the ListBox.
var g = listBox.CreateGraphics();
// Determine the size for HorizontalExtent using the MeasureString method.
var maxHorizontalSize = -1;
for (var i = 0; i < listBox.Items.Count; i++)
{
var hzSize = (int)g.MeasureString(listBox.Items[i].ToString(), listBox.Font).Width;
if (hzSize > maxHorizontalSize)
{
maxHorizontalSize = hzSize;
}
}
// Set the HorizontalExtent property.
if (maxHorizontalSize != -1)
{
listBox.HorizontalExtent = maxHorizontalSize;
}
}
示例2: EditValue
/// <summary>
/// Edits the specified object's value using the editor style indicated by the
/// System.Drawing.Design.UITypeEditor.GetEditStyle() method</summary>
/// <param name="context">An System.ComponentModel.ITypeDescriptorContext that can be used to gain
/// additional context information</param>
/// <param name="provider">An System.IServiceProvider that this editor can use to obtain services</param>
/// <param name="value">The object to edit</param>
/// <returns>The new value of the object. If the value of the object has not changed, this should
/// return the same object it was passed.</returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
m_editorService =
provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (m_editorService != null)
{
ListBox listBox = new ListBox();
listBox.DrawMode = DrawMode.OwnerDrawFixed;
listBox.DrawItem += listBox_DrawItem;
listBox.MouseMove += listBox_MouseMove;
for (int i = 0; i < m_names.Length; i++)
{
listBox.Items.Add(m_names[i]);
if (m_names[i].Equals(value) ||
m_values[i].Equals(value))
{
listBox.SelectedIndex = i;
}
}
// size control so all strings are completely visible
using (Graphics g = listBox.CreateGraphics())
{
float width = 0f;
foreach (string name in m_names)
{
float w = g.MeasureString(name, listBox.Font).Width;
width = Math.Max(width, w);
}
float height = m_names.Length * listBox.ItemHeight;
int scrollBarThickness = SystemInformation.VerticalScrollBarWidth;
if (height > listBox.Height - 4) // vertical scrollbar?
width += SystemInformation.VerticalScrollBarWidth;
if (width > listBox.Width)
listBox.Width = (int)width + 6;
}
listBox.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox.MouseDown += listBox_OnMouseDown;
listBox.MouseUp += listBox_OnMouseUp;
listBox.MouseLeave += listBox_OnMouseLeave;
listBox.PreviewKeyDown += listBox_OnPreviewKeyDown;
m_editorService.DropDownControl(listBox);
int index = listBox.SelectedIndex;
if (index >= 0)
{
object newValue = null;
if (value is int)
newValue = m_values[index];
else
newValue = m_names[index];
// be careful to return the same object if the value didn't change
if (!newValue.Equals(value))
value = newValue;
}
}
return value;
}
示例3: ListBox_initWidth
private void ListBox_initWidth(ListBox listBox)
{
int width = 0;
Graphics g = listBox.CreateGraphics();
foreach (object item in listBox.Items)
{
string text = item.ToString();
SizeF s = g.MeasureString(text, listBox.Font);
if (s.Width > width)
width = (int)s.Width;
}
listBox.HorizontalExtent = width + 2;
}
示例4: ResizeToFit
private void ResizeToFit(ListBox list, string message)
{
using (var gfx = list.CreateGraphics())
{
var size = gfx.MeasureString(message, list.Font);
var w = (int)Math.Ceiling(size.Width);
list.Width = Math.Max(list.Width, w);
list.Height = list.PreferredHeight;
}
Width = list.Width + (list.Left * 2);
Height = list.Height + (list.Top * 2);
}
示例5: SetColumnWith
private void SetColumnWith(ListBox lb)
{
List<ITableInfo> ds = lb.DataSource as List<ITableInfo>;
if (ds != null)
{
string txt = (from p in ds
orderby p.RawName.Length descending
select p.RawName).First();
int width = (int)lb.CreateGraphics().MeasureString(txt, lb.Font).Width;
lb.ColumnWidth = width;
}
}