コード例 #1
0
        private void NotifyCustomDrawMenuBar(ref Message m)
        {
            m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT;
            NativeMethods.LPNMTBCUSTOMDRAW tbcd = (NativeMethods.LPNMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.LPNMTBCUSTOMDRAW));

            bool hot      = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_HOT) != 0);
            bool selected = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_SELECTED) != 0);

            if (hot || selected)
            {
                NativeMethods.RECT rect = tbcd.nmcd.rc;

                using (Graphics graphics = Graphics.FromHdc(tbcd.nmcd.hdc))
                {
                    graphics.FillRectangle(SystemBrushes.Highlight, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                }

                using (TextGraphics textGraphics = new TextGraphics(tbcd.nmcd.hdc))
                {
                    Font   font  = this.Font;
                    string text  = this.items[tbcd.nmcd.dwItemSpec].Text;
                    Size   size  = textGraphics.MeasureText(text, font);
                    Point  point = new Point(rect.left + ((rect.right - rect.left - size.Width) / 2), rect.top + ((rect.bottom - rect.top - size.Height) / 2));
                    textGraphics.DrawText(text, point, font, SystemColors.HighlightText);
                }

                m.Result = (IntPtr)NativeMethods.CDRF_SKIPDEFAULT;
            }
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        private void NotifyCustomDraw(ref Message m)
        {
            m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT;
            NativeMethods.LPNMTBCUSTOMDRAW tbcd = (NativeMethods.LPNMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.LPNMTBCUSTOMDRAW));

            switch (tbcd.nmcd.dwDrawStage)
            {
            case NativeMethods.CDDS_PREPAINT:
                m.Result = (IntPtr)NativeMethods.CDRF_NOTIFYITEMDRAW;
                break;

            case NativeMethods.CDDS_ITEMPREPAINT:
                if (this.style == CommandBarStyle.Menu)
                {
                    this.NotifyCustomDrawMenuBar(ref m);
                }
                if (this.style == CommandBarStyle.ToolBar)
                {
                    this.NotifyCustomDrawToolBar(ref m);
                }
                break;
            }
        }