Exemplo n.º 1
0
        /// <summary>
        /// Handle the CustomDraw windows message
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal virtual bool HandleHeaderCustomDraw(ref Message m) {
            const int CDRF_NEWFONT = 2;
            const int CDRF_SKIPDEFAULT = 4;
            const int CDRF_NOTIFYPOSTPAINT = 0x10;
            const int CDRF_NOTIFYITEMDRAW = 0x20;

            const int CDDS_PREPAINT = 1;
            const int CDDS_POSTPAINT = 2;
            const int CDDS_ITEM = 0x00010000;
            const int CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT);
            const int CDDS_ITEMPOSTPAINT = (CDDS_ITEM | CDDS_POSTPAINT);

            NativeMethods.NMCUSTOMDRAW nmcustomdraw = (NativeMethods.NMCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.NMCUSTOMDRAW));
            //System.Diagnostics.Debug.WriteLine(String.Format("header cd: {0:x}, {1}, {2:x}", nmcustomdraw.dwDrawStage, nmcustomdraw.dwItemSpec, nmcustomdraw.uItemState));
            switch (nmcustomdraw.dwDrawStage) {
                case CDDS_PREPAINT:
                    this.cachedNeedsCustomDraw = this.NeedsCustomDraw();
                    m.Result = (IntPtr)CDRF_NOTIFYITEMDRAW;
                    return true;

                case CDDS_ITEMPREPAINT:
                    int columnIndex = nmcustomdraw.dwItemSpec.ToInt32();
                    OLVColumn column = this.ListView.GetColumn(columnIndex);

                    // These don't work when visual styles are enabled
                    //NativeMethods.SetBkColor(nmcustomdraw.hdc, ColorTranslator.ToWin32(Color.Red));
                    //NativeMethods.SetTextColor(nmcustomdraw.hdc, ColorTranslator.ToWin32(Color.Blue));
                    //m.Result = IntPtr.Zero;

                    if (this.cachedNeedsCustomDraw) {
                        using (Graphics g = Graphics.FromHdc(nmcustomdraw.hdc)) {
                            g.TextRenderingHint = ObjectListView.TextRenderingHint;
                            this.CustomDrawHeaderCell(g, columnIndex, nmcustomdraw.uItemState);
                        }
                        m.Result = (IntPtr)CDRF_SKIPDEFAULT;
                    } else {
                        const int CDIS_SELECTED = 1;
                        bool isPressed = ((nmcustomdraw.uItemState & CDIS_SELECTED) == CDIS_SELECTED);

                        Font f = this.CalculateFont(column, columnIndex == this.ColumnIndexUnderCursor, isPressed);

                        this.fontHandle = f.ToHfont();
                        NativeMethods.SelectObject(nmcustomdraw.hdc, this.fontHandle);

                        m.Result = (IntPtr)(CDRF_NEWFONT | CDRF_NOTIFYPOSTPAINT);
                    }

                    return true;

                case CDDS_ITEMPOSTPAINT:
                    if (this.fontHandle != IntPtr.Zero) {
                        NativeMethods.DeleteObject(this.fontHandle);
                        this.fontHandle = IntPtr.Zero;
                    }
                    break;
            }

            return false;
        }
Exemplo n.º 2
0
        internal virtual bool HandleHeaderCustomDraw(ref Message m)
        {
            const int CDRF_NEWFONT        = 2;
            const int CDRF_SKIPDEFAULT    = 4;
            const int CDRF_NOTIFYITEMDRAW = 0x20;

            const int CDDS_PREPAINT     = 1;
            const int CDDS_ITEM         = 0x00010000;
            const int CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT);

            NativeMethods.NMCUSTOMDRAW nmcustomdraw = (NativeMethods.NMCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.NMCUSTOMDRAW));
            //System.Diagnostics.Debug.WriteLine(String.Format("header cd: {0:x}, {1}, {2:x}", nmcustomdraw.dwDrawStage, nmcustomdraw.dwItemSpec, nmcustomdraw.uItemState));
            switch (nmcustomdraw.dwDrawStage)
            {
            case CDDS_PREPAINT:
                this.cachedNeedsCustomDraw = this.NeedsCustomDraw;
                m.Result = (IntPtr)CDRF_NOTIFYITEMDRAW;
                return(true);

            case CDDS_ITEMPREPAINT:
                int       columnIndex = nmcustomdraw.dwItemSpec.ToInt32();
                OLVColumn column      = this.ListView.GetColumn(columnIndex);

                // These don't work when visual styles are enabled
                //NativeMethods.SetBkColor(nmcustomdraw.hdc, ColorTranslator.ToWin32(Color.Red));
                //NativeMethods.SetTextColor(nmcustomdraw.hdc, ColorTranslator.ToWin32(Color.Blue));
                //m.Result = IntPtr.Zero;

                if (this.cachedNeedsCustomDraw)
                {
                    using (Graphics g = Graphics.FromHdc(nmcustomdraw.hdc)) {
                        g.TextRenderingHint = ObjectListView.TextRendereringHint;
                        this.CustomDrawHeaderCell(g, columnIndex, nmcustomdraw.uItemState);
                    }
                    m.Result = (IntPtr)CDRF_SKIPDEFAULT;
                }
                else
                {
                    Font f = column.HeaderFont ?? this.ListView.HeaderFont ?? this.ListView.Font;

                    if (this.HotFontStyle != FontStyle.Regular && columnIndex == this.ColumnIndexUnderCursor)
                    {
                        f = new Font(f, this.HotFontStyle);
                    }

                    NativeMethods.SelectObject(nmcustomdraw.hdc, f.ToHfont());

                    m.Result = (IntPtr)CDRF_NEWFONT;
                }

                return(true);
            }

            return(false);
        }