OnDrawItem: WM_DRAWITEM

 子控件有自画属性且控件需重画时,父窗口会调用该函数

  在具有Owner Draw属性的控件需要重画的时候,就会激发OnDrawItem

以上引用自:https://m.tsingfun.com/it/cpp/1460.html

———————————————————————————————————-

下面通过实例来理解一下

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Drawing;
  6 using System.Windows.Forms;
  7 using System.Drawing.Text;
  8 
  9 namespace OwnerDrawMenu
 10 {
 11     class OwnerDrawMenu:Form
 12     {
 13         const int iFontPointSize = 18;  //用于菜单项目
 14         MenuItem miFacename;
 15 
 16         static void Main(string[] args)
 17         {
 18             Application.Run(new OwnerDrawMenu());
 19         }
 20         public OwnerDrawMenu()
 21         {
 22             Text = "Owner Draw Menu";
 23 
 24             //Top Level items
 25             Menu = new MainMenu();
 26             Menu.MenuItems.Add("&Facename");
 27 
 28             //子菜单项的内容
 29             string[] astrText = { "&Times New Roman","&Arial","&Courier New"};
 30             MenuItem[] ami = new MenuItem[astrText.Length];
 31 
 32             EventHandler ehOnClick = new EventHandler(MenuFacenameOnClick);
 33             MeasureItemEventHandler ehOnMeasureItem =
 34                 new MeasureItemEventHandler(MenuFacenameOnMeasureItem);
 35             DrawItemEventHandler ehOnDrawItem =
 36                 new DrawItemEventHandler(MenuFacenameOnDrawItem);
 37 
 38             for (int i = 0; i < ami.Length; i++)
 39             {
 40                 ami[i] = new MenuItem(astrText[i]);
 41                 //ami[i].OwnerDraw = true;
 42                 ami[i].RadioCheck = true;
 43                 ami[i].Click += ehOnClick;
 44                 ami[i].MeasureItem += ehOnMeasureItem;
 45                 ami[i].DrawItem += ehOnDrawItem;
 46             }
 47             miFacename = ami[0];
 48             miFacename.Checked = true;
 49 
 50             Menu.MenuItems[0].MenuItems.AddRange(ami);
 51         }
 52 
 53         void MenuFacenameOnClick(object obj, EventArgs ea)
 54         {
 55             miFacename.Checked = false;
 56             miFacename = (MenuItem)obj;
 57             miFacename.Checked = true;
 58 
 59             Invalidate();
 60         }
 61 
 62         void MenuFacenameOnMeasureItem(object obj, MeasureItemEventArgs miea)
 63         {
 64             MenuItem mi = (MenuItem)obj;
 65             Font font = new Font(mi.Text.Substring(1),iFontPointSize);
 66 
 67             StringFormat strfmt = new StringFormat();
 68             strfmt.HotkeyPrefix = HotkeyPrefix.Show;
 69 
 70             SizeF sizef = miea.Graphics.MeasureString(mi.Text,font,1000,strfmt);
 71 
 72             miea.ItemWidth = (int)Math.Ceiling(sizef.Width);
 73             miea.ItemHeight = (int)Math.Ceiling(sizef.Height);
 74 
 75             miea.ItemWidth += SystemInformation.MenuCheckSize.Width * 
 76                                 miea.ItemHeight / 
 77                                     SystemInformation.MenuCheckSize.Height;
 78             miea.ItemWidth -= SystemInformation.MenuCheckSize.Width;
 79         }
 80 
 81         void MenuFacenameOnDrawItem(object obj, DrawItemEventArgs diea)
 82         {
 83             MenuItem mi = (MenuItem)obj;
 84             Graphics grfx = diea.Graphics;
 85             Brush brush;
 86 
 87             //创建字体和格式
 88             Font font = new Font(mi.Text.Substring(1),iFontPointSize);
 89             StringFormat strfmt = new StringFormat();
 90             strfmt.HotkeyPrefix = HotkeyPrefix.Show;
 91 
 92             //计算选择标记和文字矩形
 93             Rectangle rectCheck = diea.Bounds;
 94 
 95             rectCheck.Width = SystemInformation.MenuCheckSize.Width * rectCheck.Height / SystemInformation.MenuCheckSize.Height;
 96             Rectangle rectText = diea.Bounds;
 97             rectText.X += rectCheck.Width;
 98 
 99             //绘制
100             diea.DrawBackground();
101             if ((diea.State & DrawItemState.Checked) != 0)
102                  ControlPaint.DrawMenuGlyph(grfx,rectCheck,MenuGlyph.Arrow);   //此行代码为绘制菜单前的三角形“箭头”
103 
104             //测试diea.state
105             Console.WriteLine("对应的菜单 is {0}",obj.ToString());
106             Console.WriteLine("(diea.State & DrawItemState.Checked) is {0}", (diea.State & DrawItemState.Checked));
107             
108             if ((diea.State & DrawItemState.Checked) != 0)
109             
110                 brush = new SolidBrush(Color.Red);              //如果选中菜单的某一项内容,则此项内容的文字就变为红色;
111             else
112                 brush = new SolidBrush(Color.Yellow);           //未选中的其它菜单项内容,其文字就为黄色;
113 
114             grfx.DrawString(mi.Text,font,brush,rectText,strfmt);
115         }
116 
117         protected override void OnPaint(PaintEventArgs pea)
118         {
119             Graphics grfx = pea.Graphics;
120             Font font = new Font(miFacename.Text.Substring(1),12);
121             StringFormat strfmt = new StringFormat();
122             strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
123 
124             grfx.DrawString(Text,font,new SolidBrush(ForeColor),0,0);
125         }
126     }
127 }

 

本次代码注释掉了第41行//ami[i].OwnerDraw = true;

运行代码,效果如下:

 

从上面的运行结果可以看到,菜单使用的是系统自带的菜单格式与字体,字体也没有颜色显示;

使用调试程序,也可以看到,程序没有进入MenuFacenameOnDrawItem函数,也就是说没有激发OnDrawItem事件。

 

原文地址:http://www.cnblogs.com/chenlight/p/16885411.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性