在这段代码中,我创建了一些项目的上下文菜单,然后是我绘制它的最后一项。 我希望我绘制它的最后一个项目的字体大小和类型与之前的3个项目相同,就像剪切复制粘贴一样。
我怎么知道哪个字体样式类型大小是第一项,所以我可以改变最后一个看起来像它们?
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
m.MenuItems.Add(new MenuItem("Cut"));
m.MenuItems.Add(new MenuItem("Copy"));
m.MenuItems.Add(new MenuItem("Paste"));
int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (currentMouseOverRow >= 0)
{
m.MenuItems.Add(new CustomMenuItem(string.Format("Row number {0}", currentMouseOverRow)));
m.MenuItems[m.MenuItems.Count - 1].OwnerDraw = true;
m.MenuItems[m.MenuItems.Count - 1].DrawItem += Cm_DrawItem;
m.MenuItems[m.MenuItems.Count - 1].MeasureItem += MeasureMenuItem;
}
m.Show(dataGridView1, new Point(e.X, e.Y));
}
}
然后画出:
void Cm_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
var item = (MenuItem)sender;
var g = e.Graphics;
var font = new Font("Arial", 10, FontStyle.Italic);
var brush = System.Drawing.Brushes.Red;
g.DrawString(item.Text, font, brush, e.Bounds.X, e.Bounds.Y);
font.Dispose();
}
然后测量:
void MeasureMenuItem(object sender, MeasureItemEventArgs e)
{
MenuItem m = (MenuItem)sender;
Font font = new Font(Font.FontFamily, Font.Size, Font.Style);
SizeF sze = e.Graphics.MeasureString(m.Text, font);
e.ItemHeight = (int)sze.Height;
e.ItemWidth = (int)sze.Width;
}
答案 0 :(得分:-1)
这就是我想要的。
var font = System.Drawing.SystemFonts.DefaultFont;