private void NotifyCustomDrawToolBar(ref Message m) { m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT; NativeMethods.DLLVERSIONINFO dvi = new NativeMethods.DLLVERSIONINFO(); dvi.cbSize = Marshal.SizeOf(typeof(NativeMethods.DLLVERSIONINFO)); NativeMethods.DllGetVersion(ref dvi); if (dvi.dwMajorVersion < 6) { NativeMethods.LPNMTBCUSTOMDRAW tbcd = (NativeMethods.LPNMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.LPNMTBCUSTOMDRAW)); NativeMethods.RECT rc = tbcd.nmcd.rc; Rectangle rectangle = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); Graphics graphics = Graphics.FromHdc(tbcd.nmcd.hdc); CommandBarItem item = items[tbcd.nmcd.dwItemSpec]; bool hot = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_HOT) != 0); bool selected = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_SELECTED) != 0); bool disabled = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_DISABLED) != 0); CommandBarCheckBox checkBox = item as CommandBarCheckBox; if ((checkBox != null) && (checkBox.IsChecked)) { ControlPaint.DrawBorder3D(graphics, rectangle, Border3DStyle.SunkenOuter); } else if (selected) { ControlPaint.DrawBorder3D(graphics, rectangle, Border3DStyle.SunkenOuter); } else if (hot) { ControlPaint.DrawBorder3D(graphics, rectangle, Border3DStyle.RaisedInner); } Image image = item.Image; if (image != null) { Size size = image.Size; Point point = new Point(rc.left + ((rc.right - rc.left - size.Width) / 2), rc.top + ((rc.bottom - rc.top - size.Height) / 2)); NativeMethods.DrawImage(graphics, image, point, disabled); } m.Result = (IntPtr)NativeMethods.CDRF_SKIPDEFAULT; } }
private void DrawImage(Graphics graphics, Rectangle bounds, bool selected, bool disabled) { Rectangle rectangle = new Rectangle(bounds.X, bounds.Y, imageSize.Width + 6, bounds.Height); Size checkSize = SystemInformation.MenuCheckSize; Rectangle checkRectangle = new Rectangle(bounds.X + 1 + ((imageSize.Width + 6 - checkSize.Width) / 2), bounds.Y + ((bounds.Height - checkSize.Height) / 2), checkSize.Width, checkSize.Height); CommandBarCheckBox checkBox = item as CommandBarCheckBox; if (this.IsFlatMenu) { DrawBackground(graphics, rectangle, selected); if ((checkBox != null) && (checkBox.IsChecked)) { int height = bounds.Height - 2; graphics.DrawRectangle(SystemPens.Highlight, new Rectangle(bounds.X + 1, bounds.Y + 1, imageSize.Width + 3, height - 1)); graphics.FillRectangle(SystemBrushes.Menu, new Rectangle(bounds.X + 2, bounds.Y + 2, imageSize.Width + 2, height - 2)); } Image image = item.Image; if (image != null) { Point point = new Point(bounds.X + 3, bounds.Y + ((bounds.Height - image.Height) / 2)); NativeMethods.DrawImage(graphics, image, point, disabled); } else { if ((checkBox != null) && (checkBox.IsChecked)) { Color color = (disabled ? SystemColors.GrayText : SystemColors.MenuText); this.DrawCheck(graphics, checkRectangle, color); } } } else { Image image = item.Image; if (image == null) { this.DrawBackground(graphics, rectangle, selected); if ((checkBox != null) && (checkBox.IsChecked)) { Color color = (disabled ? (selected ? SystemColors.GrayText : SystemColors.ControlDark) : (selected ? SystemColors.HighlightText : SystemColors.MenuText)); this.DrawCheck(graphics, checkRectangle, color); } } else { DrawBackground(graphics, rectangle, false); if ((checkBox != null) && (checkBox.IsChecked)) { ControlPaint.DrawBorder3D(graphics, rectangle, Border3DStyle.SunkenOuter); } else if (selected) { ControlPaint.DrawBorder3D(graphics, rectangle, Border3DStyle.RaisedInner); } Point point = new Point(bounds.X + 3, bounds.Y + ((bounds.Height - image.Height) / 2)); NativeMethods.DrawImage(graphics, image, point, disabled); } } }