private void BorderWidthSetup() { const string Sample = "Hairline"; // should be not smaller than the biggest text const int PadX = 4; const int PadY = 3; const int Gap = 8; // gap between text and line const int LineLength = 64; RibbonItemCollection items = rcmbBorderWidth.Items; items.BeginUpdate(); foreach (BordersForm.BorderWidthItem bwi in BordersForm.s_borderWidths) { RibbonButton item = new RibbonButton(bwi.Width.ToString()); item.OwnerDraw = true; item.MeasureItem += (ss, ee) => { SizeF s = ee.Graphics.MeasureString(Sample, this.Font, PointF.Empty, StringFormat.GenericDefault); ee.ItemHeight = (int)Math.Round(s.Height + PadY * 2); ee.ItemWidth = (int)Math.Round(s.Width + LineLength + PadX * 2 + Gap); }; item.DrawItem += (ss, ee) => { ee.DrawBackground(); using (Pen p = new Pen(ee.ForeColor, ((float)bwi.Width / 1440f) * ee.Graphics.DpiY)) using (SolidBrush b = new SolidBrush(ee.ForeColor)) using (var sf = new StringFormat(StringFormat.GenericDefault)) { var offset = ee.Graphics.MeasureString(Sample, this.Font, PointF.Empty, StringFormat.GenericDefault).Width + PadX + Gap / 2; sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Center; ee.Graphics.DrawString(bwi.Text, this.Font, b, ee.Bounds.Left + offset - Gap / 2, ee.Bounds.Top + ee.Bounds.Height / 2, sf); if (bwi.Width > 0) { ee.Graphics.DrawLine(p, ee.Bounds.Left + offset + Gap / 2, ee.Bounds.Top + ee.Bounds.Height / 2, ee.Bounds.Right - PadX, ee.Bounds.Top + ee.Bounds.Height / 2); } } }; items.Add(item); } items.EndUpdate(); }