Click here to Skip to main content
15,894,291 members
Articles / Programming Languages / C#

DSGraphEdit: A Reasonable Facsimile of Microsoft's GraphEdit in .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (79 votes)
28 Jan 2018MIT7 min read 302.7K   10K   142  
A library for adding DirectShow GraphEdit-like abilities to .NET applications
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using DirectShowLib;

namespace DaggerLib.DSGraphEdit
{
    [ToolboxItem(false)]
    public class DSFilterTreeView : TreeView
    {
        private ContextMenuStrip contextMenuStrip1;
        private System.ComponentModel.IContainer components;
        private ToolStripMenuItem _refreshFiltersContextMenuItem;
    
        public DSFilterTreeView()
        {
            InitializeComponent();
        }

        #region InitializeComponent

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this._refreshFiltersContextMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this._refreshFiltersContextMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(145, 26);
            // 
            // _refreshFiltersContextMenuItem
            // 
            this._refreshFiltersContextMenuItem.Name = "_refreshFiltersContextMenuItem";
            this._refreshFiltersContextMenuItem.Size = new System.Drawing.Size(144, 22);
            this._refreshFiltersContextMenuItem.Text = "Refresh Filters";
            this._refreshFiltersContextMenuItem.Click += new System.EventHandler(this._refreshFiltersContextMenuItem_Click);
            // 
            // DSFilterTreeView
            // 
            this.ContextMenuStrip = this.contextMenuStrip1;
            this.LineColor = System.Drawing.Color.Black;
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            //only allow dragging of child nodes
            if ((e.Item as TreeNode).Level > 0)
            {
                DoDragDrop(e.Item, DragDropEffects.Move);
            }
            base.OnItemDrag(e);
        }

        public void SyncFilters()
        {
            // get a list of all standard Direct Show Filter Categories
            List<DsDevice> categories = new List<DsDevice>(DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.ActiveMovieCategories));

            // find tree nodes that haven't been created for a category yet
            foreach (DsDevice dev in categories)
            {
                if (GetParentNodeByName(dev.Name) == null)
                {
                    // create the IDSFilterCollection for this category
                    TreeNode tn = new TreeNode(dev.Name);
                    tn.Tag = new StandardFilterCategory(dev);
                    Nodes.Add(tn);
                }
            }

            this.BeginUpdate();
            foreach (TreeNode tn in Nodes)
            {
                IDSFilterCollection categoryCollection = (IDSFilterCollection)tn.Tag;
                // sync the filters in the filter collection with the tree view
                categoryCollection.SyncTreeNodes(tn.Nodes);
            }
            Sort();
            this.EndUpdate();
        }

        public void AddCategory(IDSFilterCollection categoryCollection)
        {
            // create a parent node for the filter collection
            TreeNode tn = new TreeNode(categoryCollection.CategoryName);
            tn.Tag = categoryCollection;
            Nodes.Add(tn);
        }

        public TreeNode GetParentNodeByName(string name)
        {
            foreach (TreeNode tn in Nodes)
            {
                if (tn.Text == name) return tn;
            }

            return null;
        }

        public static DSFilterTreeViewNode GetTreeNodeByDevicePath(string path,TreeNodeCollection collection)
        {
            foreach (DSFilterTreeViewNode tn in collection)
            {
                if (tn.DevicePath == path) return tn;
            }

            return null;
        }

        public static DSFilterTreeViewNode GetTreeNodeByGuid(Guid guid,TreeNodeCollection collection)
        {
            foreach (DSFilterTreeViewNode tn in collection)
            {
                if (tn.ClassGuid == guid) return tn;
            }

            return null;
        }

        private void _refreshFiltersContextMenuItem_Click(object sender, EventArgs e)
        {
            SyncFilters();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
United States United States
AKA Rich Insley.

I have over 25 years experience in programming, and I'm completely self taught. (Except for one year at California State University Fresno where I had to learn the God awful language Miranda (http://miranda.org.uk/). I've spent 10 years as a Paratrooper in the US Army during the Clinton Administration.

Comments and Discussions