示例#1
0
        /// <summary>
        /// Automatic calculation of the required width of the drop-down list
        /// </summary>
        /// <param name="comboBox">Current ComboBox</param>
        /// <param name="rightSpaceWidth">Additional white space on the right</param>
        /// <param name="minDropDownWidth">Minimum width of the drop-down list. If value is '-1', minimum width == comboBox.Width</param>
        /// <example>
        /// 	<code>
        ///         //Demo #1
        ///         var names = new[] { "Aleksey", "Alexander", "Anton", "Vladislav" };
        ///         comboBox1.DataSource = names; // or comboBox1.Items.AddRange(names);
        ///         comboBox1.Width = 40;
        ///         comboBox1.MeasureDropDownWidth();
        /// 
        ///         //Demo #2
        ///         comboBox1.SizeChanged += (s, e) => comboBox1.MeasureDropDownWidth();
        ///         comboBox1.Width = 150;
        ///         comboBox1.Anchor |= AnchorStyles.Right;
        /// 	</code>
        /// </example>
        /// <remarks>
        /// 	Contributed by nagits, http://about.me/AlekseyNagovitsyn
        /// </remarks>
        public static void MeasureDropDownWidth(this ComboBox comboBox, int rightSpaceWidth = 15, int minDropDownWidth = -1)
        {
            if(comboBox.Items.Count == 0)
            {
                comboBox.DropDownWidth = Math.Max(comboBox.Width, minDropDownWidth);
                return;
            }

            var graphics = comboBox.CreateGraphics();

            float measureWidth = 0;
            foreach (object item in comboBox.Items)
            {
                string text;
                if (comboBox.DataSource == null || string.IsNullOrEmpty(comboBox.DisplayMember))
                {
                    text = item.ToString();
                }
                else
                {
                    var propertyInfo = item.GetType().GetProperty(comboBox.DisplayMember);
                    text = propertyInfo != null ? propertyInfo.GetValue(item, null).ToString() : item.ToString();
                }

                measureWidth = Math.Max(measureWidth, graphics.MeasureString(text, comboBox.Font).Width);
            }

            var newWidth = (int)Math.Round(measureWidth);
            newWidth += rightSpaceWidth;
            newWidth = Math.Min(newWidth, Screen.GetWorkingArea(comboBox).Width);

            comboBox.DropDownWidth = Math.Max(newWidth, minDropDownWidth == -1 ? comboBox.Width : minDropDownWidth);
            graphics.Dispose();
        }
示例#2
0
		public static SizeF MesureString( this System.Windows.Forms.ListView List, string Text ) {
			SizeF size;
			using( Graphics g = List.CreateGraphics() )
				size = g.MeasureString( Text, List.Font );

			return size;
		}
 /// <summary>
 /// Shrinks the left column (property names) on the <see cref="PropertyGrid"/>.
 /// </summary>
 /// <param name="propertyGrid">The <see cref="PropertyGrid"/>.</param>
 /// <param name="padding">The number of extra pixels to pad. Can be negative to shrink the column more.</param>
 public static void ShrinkPropertiesColumn(this PropertyGrid propertyGrid, int padding = 0)
 {
     using (var g = propertyGrid.CreateGraphics())
     {
         var tabs = propertyGrid.PropertyTabs.Cast<PropertyTab>();
         var font = propertyGrid.Font;
         var w = tabs.Max(x => g.MeasureString(x.TabName, font).Width + (x.Bitmap != null ? x.Bitmap.Width : 0));
         MoveSplitter(propertyGrid, (int)w + padding);
     }
 }
        public static Image PrintInvisibleControl(this Control targetControl)
        {
            using (Graphics g = targetControl.CreateGraphics())
            {
                //new bitmap object to save the image        
                var bmp = new Bitmap(targetControl.Width, targetControl.Height);
                //Drawing control to the bitmap        
                targetControl.DrawToBitmap(bmp, new Rectangle(0, 0, targetControl.Width, targetControl.Height));

                return bmp;
            }
        }
示例#5
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Used to size a link label in mono because it is not working at all
		/// </summary>
		/// <param name="linkLabel"></param>
		/// ------------------------------------------------------------------------------------
		public static void SizeToContents(this LinkLabel linkLabel)
		{
			var w = linkLabel.ClientSize.Width;

			using (var g = linkLabel.CreateGraphics())
			{

				if (ArchivingDlgViewModel.IsMono)
				{
					// split at the existing like breaks
					var segments = linkLabel.Text.Replace("\r", "").Split(new[] {'\n'}, StringSplitOptions.None);
					var newText = new StringBuilder();

					foreach (var segment in segments)
					{
						var thisSegment = segment.Trim();

						while (g.MeasureString(thisSegment, linkLabel.Font).Width > w)
						{
							var line = string.Empty;
							var lastSpace = 0;

							for (var i = 0; i < thisSegment.Length; i++)
							{
								if (char.IsWhiteSpace(thisSegment[i]))
								{
									if (g.MeasureString(line, linkLabel.Font).Width > w)
									{
										newText.AppendLine(thisSegment.Substring(0, lastSpace));
										thisSegment = thisSegment.Substring(lastSpace + 1);
										break;
									}

									lastSpace = i;
								}
								line += thisSegment[i];
							}
						}

						// check for left-overs
						if (thisSegment.Length > 0)
							newText.AppendLine(thisSegment);
					}

					linkLabel.Text = newText.ToString();
				}

				var size = g.MeasureString(linkLabel.Text, linkLabel.Font, w);
				linkLabel.Height = (int)Math.Ceiling(size.Height);
			}
		}
示例#6
0
        public static void InsertImage(this RichTextBox rtb, Image img)
        {
            var rtf = new StringBuilder();

            // Append the RTF header
            rtf.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033");

            // Create the font table using the RichTextBox's current font and append
            // it to the RTF string
            rtf.Append(GetFontTable(rtb.Font));

            // Create the image control string and append it to the RTF string
            using (var mgfx = rtb.CreateGraphics())
                rtf.Append(GetImagePrefix(img, mgfx));

            // Create the Windows Metafile and append its bytes in HEX format
            using (var mgfx = rtb.CreateGraphics())
                rtf.Append(GetRtfImage(img, mgfx));

            // Close the RTF image control string
            rtf.Append(@"}");
            rtb.SelectedRtf = rtf.ToString();
        }
        public static void Highlighting(this RichTextBoxAdvanced box, int index, Color highlightColor)
        {
            int _lineWidth = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
            int _lineHeight = Convert.ToInt32(box.Font.Height * box.ZoomFactor);

            using (Graphics g = box.CreateGraphics())
            {
                Pen _pen = new Pen(highlightColor, 2);
                Brush brush = new SolidBrush(highlightColor);

                Rectangle _line = new Rectangle(0, _lineHeight * index, 500, _lineHeight);

                g.DrawRectangle(_pen, _line);

                _pen.Dispose();
            }
        }
示例#8
0
        /// <summary>
        /// Return Capture Screen (Bitmap file) of a Form Control
        /// </summary>
        /// <param name="ctr"></param>
        /// <returns></returns>
        public static Bitmap CaptureScreen(this Control ctr, int maxWidth = 700)
        {
            //int w = ctr.Size.Width;
            //int h = ctr.Size.Height;

            //if (maxWidth > 0 && w > maxWidth)
            //{
            //    h = h * maxWidth / w;
            //    w = maxWidth;
            //}

            Graphics mygraphics = ctr.CreateGraphics();
            Size s = ctr.Size;
            Bitmap memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();

            BitBlt(dc2, 0, 0, ctr.Size.Width, ctr.Size.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
            return memoryImage;
        }
 public static void AdjustTileToWidth(this ListView lvw, int maxLines = 1, int iconSpacing = 4)
 {
     const string str = "Wg";
     var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { IconTextSpacing = iconSpacing, MaxTextLines = maxLines };
     var sb = new System.Text.StringBuilder(str);
     for (int i = 0; i < maxLines; i++)
         sb.Append("\r" + str);
     using (Graphics g = lvw.CreateGraphics())
         lvTVInfo.TileSize = new Size(lvw.ClientSize.Width, Math.Max(lvw.LargeImageList.ImageSize.Height, TextRenderer.MeasureText(g, sb.ToString(), lvw.Font).Height));
     NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo);
     //var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { TileWidth = lvw.ClientSize.Width };
     //NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo);
     //NativeMethods.SendMessage(lvw.Handle, (uint)NativeMethods.ListViewMessage.SetExtendedListViewStyle, new IntPtr(0x200000), new IntPtr(0x200000));
 }