Пример #1
0
        private void TreeViewCustomDraw(ref Message msg, NMTVCUSTOMDRAW tvDraw)
        {
            try
            {
                int hTreeNode = tvDraw.nmcd.dwItemSpec;

                if (hTreeNode == 0)
                {
                    msg.Result = new IntPtr(CommCtrl.CDRF_DODEFAULT);
                    return;
                }

                NuGenTreeNode curNode = NuGenTreeNode.FromHandle(this, new IntPtr(hTreeNode)) as NuGenTreeNode;

                if (curNode == null)
                {
                    msg.Result = new IntPtr(CommCtrl.CDRF_DODEFAULT);
                    return;
                }

                TreeView tree = curNode.TreeView;

                if (tree != null)
                {
                    if (!curNode.HasCheckBox)
                    {
                        NuGenTreeView.UncheckNode(curNode.TreeView.Handle, curNode.Handle.ToInt32());
                    }
                }
            }
            finally
            {
                msg.Result = new IntPtr(CommCtrl.CDRF_DODEFAULT);
            }
        }
Пример #2
0
        private void DoTreeCustomDrawing(ref Message m)
        {
            NMTVCUSTOMDRAW tvcd  = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
            IntPtr         hNode = (IntPtr)tvcd.nmcd.dwItemSpec;
            Rectangle      rect  = GetItemRect(hNode);

            // Create a graphic object from the Device context in the message
            Graphics g = Graphics.FromHdc(tvcd.nmcd.hdc);

            // If this item has the focus draw the higlighting rectangle
            if (itemHasFocus)
            {
                using (Brush brush = new SolidBrush(ColorUtil.VSNetSelectionColor))
                {
                    g.FillRectangle(brush, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                    g.DrawRectangle(SystemPens.Highlight, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                }
            }

            // Draw Text
            string itemText = GetItemText(hNode);
            Size   textSize = TextUtil.GetTextSize(g, itemText, Font);
            Point  pos      = new Point(rect.Left, rect.Top + (rect.Height - textSize.Height) / 2);

            g.DrawString(itemText, Font, SystemBrushes.ControlText, pos);

            // Put structure back in the message
            Marshal.StructureToPtr(tvcd, m.LParam, true);
            m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_SKIPDEFAULT;
        }
Пример #3
0
        /// <summary>
        /// Handles the NM_CUSTOMDRAW notification
        /// </summary>
        private void OnCustomDraw(ref NMTVCUSTOMDRAW customDraw, ref Message m)
        {
            switch (customDraw.nmcd.dwDrawStage)
            {
            case Win32Declarations.CDDS_PREPAINT:
                m.Result = (IntPtr)(Win32Declarations.CDRF_NOTIFYITEMDRAW | Win32Declarations.CDRF_NOTIFYPOSTPAINT);
                break;

            case Win32Declarations.CDDS_ITEMPREPAINT:
                TreeNode node = TreeNode.FromHandle(this, (IntPtr)customDraw.nmcd.dwItemSpec);

                if (myNodePainter != null && node != null && myNodePainter.IsHandled(node))
                {
                    //DrawNode( ref customDraw );
                    m.Result = (IntPtr)(Win32Declarations.CDRF_NOTIFYITEMDRAW | Win32Declarations.CDRF_NOTIFYPOSTPAINT /*| Win32Declarations.CDRF_SKIPDEFAULT */);
                }
                else
                {
                    m.Result = (IntPtr)Win32Declarations.CDRF_NOTIFYITEMDRAW;
                }
                break;

            case Win32Declarations.CDDS_ITEMPOSTPAINT:
                DrawNode(ref customDraw);
                break;

            case Win32Declarations.CDDS_POSTERASE:
                EraseNode(ref customDraw);
                break;

            default:
                m.Result = (IntPtr)Win32Declarations.CDRF_DODEFAULT;
                break;
            }
        }
Пример #4
0
        protected unsafe override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
            case 8270:
                NMHDR *ptr = (NMHDR *)((void *)m.LParam);
                if (ptr->code == NM_CUSTOMDRAW)
                {
                    NMTVCUSTOMDRAW nMTVCUSTOMDRAW = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
                    int            dwDrawStage    = nMTVCUSTOMDRAW.nmcd.dwDrawStage;
                    if (dwDrawStage == (CDDS_ITEM | CDDS_PREPAINT))
                    {
                        TreeNode treeNode = TreeNode.FromHandle(this, nMTVCUSTOMDRAW.nmcd.dwItemSpec);
                        if (treeNode != null)
                        {
                            using (Graphics graphics = Graphics.FromHdcInternal(nMTVCUSTOMDRAW.nmcd.hdc))
                            {
                                var bounds = treeNode.Bounds;
                                using (var brush = new SolidBrush(GetNodeRowBackgroundColor(treeNode)))
                                {
                                    graphics.FillRectangle(brush, new Rectangle(0, bounds.Top, this.Width, bounds.Height));
                                }
                            }
                        }
                    }
                }
                break;

            case WM_ERASEBKGND:
                // in this handler erase only part of background that is not covered by nodes
                TreeNode  lastVisible = FindLastVisible(Nodes);
                Rectangle r;
                if (lastVisible != null)
                {
                    r = new Rectangle(0, lastVisible.Bounds.Bottom, Width, Height);
                }
                else
                {
                    r = new Rectangle(0, 0, Width, Height);
                }
                using (var g = CreateGraphics())
                    g.FillRectangle(Brushes.White, r);
                m.Result = (IntPtr)1;
                return;                         // skip default processing

            case WM_HSCROLL:
                Invalidate();
                break;
            }
            base.WndProc(ref m);
        }
Пример #5
0
        private bool NotifyTreeCustomDraw(ref Message m)
        {
            m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_DODEFAULT;
            NMTVCUSTOMDRAW tvcd = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));

            //IntPtr thisHandle = Handle;

            if (tvcd.nmcd.hdr.hwndFrom != Handle)
            {
                return(false);
            }
            switch (tvcd.nmcd.dwDrawStage)
            {
            case (int)CustomDrawDrawStateFlags.CDDS_PREPAINT:
                // Ask for Item painting notifications
                m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYITEMDRAW;
                break;

            case (int)CustomDrawDrawStateFlags.CDDS_ITEMPREPAINT:

                itemHasFocus = false;
                if ((tvcd.nmcd.uItemState & (int)CustomDrawItemStateFlags.CDIS_FOCUS) != 0)
                {
                    itemHasFocus = true;
                }

                // Set the text and background text color to the window bakcground
                // text so that we don't see the text being painted
                tvcd.clrText = ColorUtil.RGB(SystemColors.Window.R,
                                             SystemColors.Window.G, SystemColors.Window.B);
                tvcd.clrTextBk = ColorUtil.RGB(SystemColors.Window.R,
                                               SystemColors.Window.G, SystemColors.Window.B);

                // Put structure back in the message
                Marshal.StructureToPtr(tvcd, m.LParam, true);
                m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYPOSTPAINT;
                break;

            case (int)CustomDrawDrawStateFlags.CDDS_ITEMPOSTPAINT:
                DoTreeCustomDrawing(ref m);
                break;

            default:
                break;
            }
            return(false);
        }
Пример #6
0
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == 0x204e && this.NodeTable2 != null)
            {
                //获取参数
                NMTVCUSTOMDRAW lParam = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
                //获取当前的节点
                TreeNode node = this.NodeTable2[lParam.nmcd.dwItemSpec] as TreeNode;
                if (node != null && node.IsSelected)
                {
                    lParam.clrTextBk = ColorTranslator.ToWin32(this.selectedBackColor);
                    lParam.clrText   = ColorTranslator.ToWin32(Color.White);
                    Marshal.StructureToPtr(lParam, m.LParam, false);
                }
            }

            base.WndProc(ref m);
        }
Пример #7
0
        /// <summary>
        /// Erases node
        /// </summary>
        private void EraseNode(ref NMTVCUSTOMDRAW customDraw)
        {
            try
            {
                //TreeNode node = TreeNode.FromHandle(this, (IntPtr)customDraw.nmcd.dwItemSpec);

                using (Graphics g = Graphics.FromHdc(customDraw.nmcd.hdc))
                {
                    //int offset = CalculateOffset(node);
                    Rectangle rect = new Rectangle(customDraw.nmcd.rc.left /* + offset*/, customDraw.nmcd.rc.top, customDraw.nmcd.rc.right - customDraw.nmcd.rc.left /* - offset*/, customDraw.nmcd.rc.bottom - customDraw.nmcd.rc.top);

                    g.FillRectangle(new SolidBrush(BackColor), rect);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("CustomTreeView.EraseNode failed : " + ex, "UI");
            }
        }
Пример #8
0
        protected virtual void WmUser7246(ref Message m)
        {
            NMHDR nmHeader = (NMHDR)m.GetLParam(typeof(NMHDR));

            if (nmHeader.code == CommCtrl.NM_CUSTOMDRAW)
            {
                NMTVCUSTOMDRAW tvDraw = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));

                if (CommCtrl.CDDS_PREPAINT == tvDraw.nmcd.dwDrawStage)
                {
                    m.Result = new IntPtr(CommCtrl.CDRF_NOTIFYITEMDRAW);
                }
                else if (CommCtrl.CDDS_ITEMPREPAINT == tvDraw.nmcd.dwDrawStage)
                {
                    this.TreeViewCustomDraw(ref m, tvDraw);
                }
            }

            base.WndProc(ref m);
        }
Пример #9
0
        /// <summary>
        /// Handles the WM_NOTIFY message of the parent control
        /// </summary>
        /// <param name="m">The message to handle</param>
        /// <returns>Whether the message was handled</returns>
        private void OnWmNotify(ref Message m)
        {
            // Marshal lParam into NMHDR:
            NMHDR hdr = new NMHDR();

            hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));

            switch (hdr.code)
            {
            case Win32Declarations.NM_CUSTOMDRAW:
                NMTVCUSTOMDRAW customDraw = new NMTVCUSTOMDRAW();
                customDraw = (NMTVCUSTOMDRAW)Marshal.PtrToStructure(m.LParam, typeof(NMTVCUSTOMDRAW));

                OnCustomDraw(ref customDraw, ref m);

                break;

            default:
                base.WndProc(ref m);
                break;
            }
        }
Пример #10
0
        private bool NotifyTreeCustomDraw(ref Message m)
        {
            m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_DODEFAULT;
            NMTVCUSTOMDRAW tvcd       = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
            IntPtr         thisHandle = Handle;

            if (tvcd.nmcd.hdr.hwndFrom != Handle)
            {
                return(false);
            }

            switch (tvcd.nmcd.dwDrawStage)
            {
            case (int)CustomDrawDrawStateFlags.CDDS_ITEMPREERASE:
                m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_SKIPDEFAULT;
                break;

            case (int)CustomDrawDrawStateFlags.CDDS_PREPAINT:
                // Ask for Item painting notifications
                m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYITEMDRAW;
                break;

            case (int)CustomDrawDrawStateFlags.CDDS_ITEMPREPAINT:
                bool bSelected = m_SelectedItems.Contains(tvcd.nmcd.dwItemSpec);
                tvcd.clrTextBk = (bSelected) ? ColorUtil.RGB(SystemColors.Highlight) : ColorUtil.RGB(SystemColors.Window);
                tvcd.clrText   = (bSelected) ? ColorUtil.RGB(SystemColors.HighlightText) : ColorUtil.RGB(SystemColors.WindowText);


                // Put structure back in the message
                Marshal.StructureToPtr(tvcd, m.LParam, true);
                m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYPOSTPAINT;
                break;

            default:
                break;
            }
            return(false);
        }
Пример #11
0
        /// <summary>
        /// Draws node
        /// </summary>
        private void DrawNode(ref NMTVCUSTOMDRAW customDraw)
        {
            try
            {
                TreeNode node = TreeNode.FromHandle(this, (IntPtr)customDraw.nmcd.dwItemSpec);

                if (myNodePainter != null && myNodePainter.IsHandled(node))
                {
                    Rectangle rect = new Rectangle(customDraw.nmcd.rc.left, customDraw.nmcd.rc.top, customDraw.nmcd.rc.right - customDraw.nmcd.rc.left, customDraw.nmcd.rc.bottom - customDraw.nmcd.rc.top);

                    if (MultiSelect)
                    {
                        SetNodeSelectedState(node, (node == SelectedNode || mySelectedNodes.Contains(node)));
                    }

                    myNodePainter.Draw(node, customDraw.nmcd.hdc, rect);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("CustomTreeView.DrawNode failed : " + ex, "UI");
            }
        }