当前位置: 首页>>代码示例>>C#>>正文


C# ListBox.CreateGraphics方法代码示例

本文整理汇总了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;
            }
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:25,代码来源:ViewUtils.cs

示例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;
        }
开发者ID:JanDeHud,项目名称:LevelEditor,代码行数:74,代码来源:EnumUITypeEditor.cs

示例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;
        }
开发者ID:autocompaste,项目名称:AutoComPaste,代码行数:14,代码来源:AutoCompleteForm.cs

示例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);
        }
开发者ID:rossipedia,项目名称:DeviceNotifier,代码行数:13,代码来源:MessagesForm.cs

示例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;
     }
 }
开发者ID:zhh007,项目名称:CKGen,代码行数:12,代码来源:FrmSelectTable.cs


注:本文中的System.Windows.Forms.ListBox.CreateGraphics方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。