private void WmCustomDraw(ref Message m) { m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT;//在列表项绘制循环过程不再发送 NativeMethods.NMTBCUSTOMDRAW tbcd = (NativeMethods.NMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.NMTBCUSTOMDRAW)); switch (tbcd.nmcd.dwDrawStage) { case NativeMethods.CDDS_PREPAINT: //开始画之前通知项 m.Result = (IntPtr)NativeMethods.CDRF_NOTIFYITEMDRAW; //列表项绘制前后发送消息 break; case NativeMethods.CDDS_ITEMPREPAINT: WmPrepaint(ref m); break; } }
private void WmPrepaint(ref Message m) { m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT; NativeMethods.NMTBCUSTOMDRAW tbcd = (NativeMethods.NMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.NMTBCUSTOMDRAW)); MenuBoxItem item = this.items[(int)tbcd.nmcd.dwItemSpec]; bool hot = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_HOT) != 0); bool selected = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_SELECTED) != 0); bool mchecked = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_CHECKED) != 0); bool disabled = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_DISABLED) != 0); bool focus = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_FOCUS) != 0); bool grayed = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_GRAYED) != 0); if (hot || selected) { DrawIEMenuBarItem(item, ref tbcd); m.Result = (IntPtr)NativeMethods.CDRF_SKIPDEFAULT; } }
private void DrawIEMenuBarItem(MenuBoxItem item, ref NativeMethods.NMTBCUSTOMDRAW tbcd) { NativeMethods.RECT rect = tbcd.nmcd.rc; rect.top += 1; IntPtr hDC = tbcd.nmcd.hdc; string text = item.Text; ///FillRect IntPtr hBrush = UnsafeNativeMethods.CreateSolidBrush(hightLightColor); UnsafeNativeMethods.FillRect(hDC, ref rect, hBrush); ///MeasureText NativeMethods.RECT lprt = new NativeMethods.RECT(); UnsafeNativeMethods.DrawText(hDC, text, text.Length, ref lprt, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT | NativeMethods.DT_CALCRECT);//获得文字高度 ///Drawtext IntPtr hOldFont = UnsafeNativeMethods.SelectObject(hDC, hFont); int oldBkMode = UnsafeNativeMethods.SetBkMode(hDC, NativeMethods.TRANSPARENT); int oldTextColor = UnsafeNativeMethods.SetTextColor(hDC, hightLightTextColor); NativeMethods.RECT clip = new NativeMethods.RECT(); clip.left = rect.left + ((rect.right - rect.left - lprt.right) / 2) + 2; clip.top = rect.top + ((rect.bottom - rect.top - lprt.bottom) / 2) + 2; clip.right = clip.left + lprt.right; clip.bottom = clip.top + lprt.bottom; UnsafeNativeMethods.DrawText(hDC, text, text.Length, ref clip, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT); ///Free UnsafeNativeMethods.SetTextColor(hDC, oldTextColor); UnsafeNativeMethods.SetBkMode(hDC, oldBkMode); UnsafeNativeMethods.SelectObject(hDC, hOldFont); if (hBrush != IntPtr.Zero) { UnsafeNativeMethods.DeleteObject(hBrush); } }