home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_PSView_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-06-20  |  11.4 KB  |  382 lines

  1. namespace Microsoft.CLR.Interop.Sample.WMI.PSView
  2. {
  3.  
  4.     using System;
  5.     using System.Drawing;
  6.     using System.Windows.Forms;
  7.     using System.Diagnostics;
  8.     using System.Collections;
  9.     using System.Runtime.InteropServices;
  10.     using WBEMSCRIPTING;
  11.  
  12.  
  13.     public class MTNode : TreeNode
  14.     {
  15.         public MTNode(String text, ISWbemObject WbemObj) : base(text)
  16.         {
  17.             bInfoAdded = false;
  18.             mWbemObj = WbemObj;
  19.         }
  20.  
  21.         public virtual void AddContentsToSibling(System.Windows.Forms.GroupBox GrpBox)
  22.         {
  23.             GrpBox.Controls.Clear();
  24.             GrpBox.Text = null;
  25.             if (mContents != null ) 
  26.             {
  27.                 for (int i = 0; i < mContents.Length; ++i)
  28.                     GrpBox.Controls.Add(mContents[i]);
  29.                 GrpBox.Text = sTitle;
  30.             }
  31.         }
  32.  
  33.         public virtual void QueryInformation()
  34.         {}
  35.  
  36.         public int nNodeIndex = 0;
  37.         public System.Windows.Forms.Label [] mContents;
  38.         public String sTitle;
  39.         public bool bInfoAdded;
  40.         public ISWbemObject mWbemObj;
  41.         public String sClass;
  42.     }
  43.  
  44.     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45.     public class PSNode : MTNode
  46.     {
  47.         public PSNode(String text, ISWbemObject WbemObj) : base(text, WbemObj)
  48.         {    
  49.             nNodeIndex = 0;
  50.             sPSName = text;
  51.             sTitle = sPSName;
  52.             sTitle += " Information";
  53.         }
  54.  
  55.         public override void QueryInformation()
  56.         {
  57.             if (bInfoAdded || mWbemObj == null)
  58.                 return;
  59.  
  60.             bInfoAdded = true;
  61.             
  62.             try
  63.             {
  64.                 ISWbemPropertySet WbemPropSet = mWbemObj.Properties_;
  65.                 mContents = new Label[20];
  66.                 int nIndex = 0;
  67.                 int xStart1 = 10, yStart1 = 25;
  68.                 int xSize = 110, ySize = 16;
  69.  
  70.                 foreach( Object ob in WbemPropSet)
  71.                 {
  72.                     ISWbemProperty WbemProp = (ISWbemProperty) ob;
  73.  
  74.                     //check to see if name or value is too long for the window.
  75.                     if (WbemProp.Name.Length > 20 || WbemProp.get_Value().ToString().Length > 20)
  76.                         continue;
  77.  
  78.                     mContents[nIndex] = new Label();
  79.                     mContents[nIndex].Text = WbemProp.Name;
  80.                     mContents[nIndex].Location = new System.Drawing.Point(xStart1, yStart1);
  81.                     mContents[nIndex].Size = new System.Drawing.Size(xSize, ySize);
  82.  
  83.                     nIndex ++;
  84.                     mContents[nIndex] = new Label();
  85.                     mContents[nIndex].Location = new System.Drawing.Point(xStart1 + xSize, yStart1);
  86.                     String sValue = WbemProp.get_Value().ToString();
  87.                     mContents[nIndex].Text = sValue;
  88.                     mContents[nIndex].Size = new System.Drawing.Size(xSize + 70, ySize);
  89.  
  90.                     nIndex ++;
  91.                     yStart1 += ySize;
  92.  
  93.                     //only display the first 15 items. 
  94.                     if (nIndex >= 30)
  95.                         break;
  96.  
  97.                 }
  98.  
  99.             }
  100.             catch(Exception )
  101.             {
  102.                 
  103.             }
  104.             
  105.         }
  106.         
  107.         private String sPSName;
  108.  
  109.     }
  110.  
  111.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112.  
  113. }
  114.  
  115. namespace Microsoft.CLR.Interop.Sample.WMI.PSView
  116. {
  117.                     
  118.     using System;
  119.     using System.Collections;
  120.     using System.Threading;
  121.     using System.IO;
  122.     using System.Resources;   
  123.     using System.ComponentModel;
  124.     using System.Drawing;
  125.     using System.Windows.Forms;
  126.     using System.Runtime.InteropServices;
  127.     using WBEMSCRIPTING;
  128.  
  129.     public class PSForm : System.Windows.Forms.Form
  130.     {
  131.  
  132.         public PSForm()
  133.         {
  134.             InitializeComponent();
  135.             FillTree();
  136.         }
  137.  
  138.         private void InfoTree_AfterSelect(object source, TreeViewEventArgs e)
  139.         {
  140.             Text = "Process and Services Explorer - " + e.Node.Text;
  141.             MTNode MNode = (MTNode) e.Node;
  142.             MNode.QueryInformation();
  143.             MNode.AddContentsToSibling(GrpBox);
  144.         }
  145.  
  146.         private void InfoTree_BeforeExpand(object source, TreeViewCancelEventArgs e)
  147.         {
  148.             //MTNode MNode = (MTNode) e.Node;
  149.             //MNode.QueryInformation();
  150.             //MNode.AddContentsToSibling(GrpBox);
  151.         }
  152.  
  153.         private void FillTree()
  154.         {
  155.             SWbemLocator Locator = null;
  156.             ISWbemServices Server = null;
  157.             ISWbemObjectSet ObjSet = null;
  158.             ISWbemObject WbemObj = null;
  159.             ISWbemPropertySet WbemPropSet = null;
  160.  
  161.             try
  162.             {
  163.                 Locator = new SWbemLocator();
  164.                 //Connect to local machine
  165.                 Server = Locator.ConnectServer(null, null, null, null, null, null, 0, null);                
  166.             }
  167.             catch(Exception )
  168.             {
  169.                 return;
  170.             }
  171.  
  172.             //Add the top node
  173.             MTNode aNode = new MTNode("My Computer", null); 
  174.             aNode.ImageIndex = 0;
  175.             aNode.SelectedImageIndex = 1;
  176.             InfoTree.Nodes.Add(aNode);
  177.  
  178.             MTNode PNode = new MTNode("Processes", null);
  179.             aNode.Nodes.Add(PNode);
  180.  
  181.             MTNode SNode = new MTNode("Services", null);
  182.             aNode.Nodes.Add(SNode);
  183.  
  184.             
  185.             ObjSet = Server.ExecQuery("select * from Win32_Process",
  186.                                       "WQL", 0x10, null);
  187.  
  188.             String sName;
  189.             foreach(Object o in ObjSet)
  190.             {
  191.                 WbemObj = (ISWbemObject) o;
  192.                 WbemPropSet = WbemObj.Properties_;
  193.                 sName = WbemPropSet.Item("Caption", 0).get_Value().ToString();                
  194.                 PSNode pNode = new PSNode(sName, WbemObj);
  195.                 PNode.Nodes.Add(pNode);
  196.             }
  197.  
  198.             ObjSet = Server.ExecQuery("select * from Win32_Service",
  199.                                       "WQL", 0x10, null);
  200.  
  201.             foreach(Object ob in ObjSet)
  202.             {
  203.                 WbemObj = (ISWbemObject) ob;
  204.                 WbemPropSet = WbemObj.Properties_;
  205.                 sName = WbemPropSet.Item("Caption", 0).get_Value().ToString();                
  206.                 PSNode sNode = new PSNode(sName, WbemObj);
  207.                 SNode.Nodes.Add(sNode);
  208.             }
  209.  
  210.         }
  211.  
  212.         private int Refresh_GetExpanded(TreeNode Node, string[] ExpandedNodes, int StartIndex) {
  213.             
  214.             if (StartIndex < ExpandedNodes.Length)
  215.             {
  216.                 if (Node.IsExpanded)
  217.                 {
  218.                     ExpandedNodes[StartIndex] = Node.Text;
  219.                     StartIndex++;
  220.                     for (int i = 0; i < Node.Nodes.Count; i++)
  221.                     {
  222.                         StartIndex = Refresh_GetExpanded(Node.Nodes[i], 
  223.                                                          ExpandedNodes,
  224.                                                          StartIndex);
  225.                     }
  226.                 }
  227.                 return StartIndex;
  228.             } 
  229.             return -1;
  230.         }
  231.  
  232.         private void Refresh_Expand(TreeNode Node, string[] ExpandedNodes)
  233.         {
  234.             
  235.             for (int i = ExpandedNodes.Length - 1; i >= 0; i--)
  236.             {
  237.                 if (ExpandedNodes[i] == Node.Text)
  238.                 {
  239.                     /*
  240.                     * For the expand button to show properly, one level of 
  241.                     * invisible children have to be added to the tree. 
  242.                     */
  243.                     //AddDependentModules((ProcessNode) Node);
  244.                     Node.Expand();
  245.  
  246.                     /* If the node is expanded, expand any children that were 
  247.                     * expanded before the refresh. 
  248.                     */
  249.                     for (int j = 0; j < Node.Nodes.Count; j++)
  250.                      {
  251.                         Refresh_Expand(Node.Nodes[j], ExpandedNodes);
  252.                     }
  253.  
  254.                     return;
  255.                 }
  256.             }
  257.         }
  258.  
  259.         private void Refresh(TreeNode node)
  260.         {
  261.         }
  262.  
  263.         private System.ComponentModel.Container components;
  264.         private System.Windows.Forms.TreeView InfoTree;
  265.         private System.Windows.Forms.Panel Panel2;
  266.         private System.Windows.Forms.Splitter Splitter1;
  267.  
  268.         private System.Windows.Forms.ImageList imageList1;
  269.         private System.Windows.Forms.ToolTip toolTip1;
  270.         private System.Windows.Forms.GroupBox GrpBox;
  271.         private System.Drawing.Bitmap [] MBitmap;
  272.  
  273.         private void InitializeComponent()
  274.         {
  275.             this.Icon = new Icon("Machine.ico");
  276.             this.components = new System.ComponentModel.Container();
  277.             this.InfoTree = new System.Windows.Forms.TreeView();
  278.             this.toolTip1 = new System.Windows.Forms.ToolTip(components);
  279.             this.imageList1 = new System.Windows.Forms.ImageList();
  280.             this.GrpBox = new System.Windows.Forms.GroupBox();
  281.  
  282.             this.MBitmap = new System.Drawing.Bitmap[10];
  283.  
  284.             Icon Icon0 = new Icon("NetHood.ico");
  285.             Icon Icon1 = new Icon("netHood.ico");
  286.             Icon Icon2 = new Icon("MyComp.ico");
  287.             Icon Icon3 = new Icon("MyComp.ico");
  288.             Icon Icon4 = new Icon("Gears.ico");
  289.             Icon Icon5 = new Icon("GearsH.ico");
  290.             Icon Icon6 = new Icon("Group.ico");
  291.             Icon Icon7 = new Icon("GroupH.ico");
  292.             Icon Icon8 = new Icon("User.ico");
  293.             Icon Icon9 = new Icon("UserH.ico");
  294.             MBitmap[0] = Icon0.ToBitmap();
  295.             MBitmap[1] = Icon1.ToBitmap();
  296.             MBitmap[2] = Icon2.ToBitmap();
  297.             MBitmap[3] = Icon3.ToBitmap();
  298.             MBitmap[4] = Icon4.ToBitmap();
  299.             MBitmap[5] = Icon5.ToBitmap();
  300.             MBitmap[6] = Icon6.ToBitmap();
  301.             MBitmap[7] = Icon7.ToBitmap();
  302.             MBitmap[8] = Icon8.ToBitmap();
  303.             MBitmap[9] = Icon9.ToBitmap();
  304.  
  305.             for (int i = 0; i < 10; ++i)
  306.                 imageList1.Images.Add(MBitmap[i]);
  307.  
  308.             InfoTree.ImageList = (ImageList)imageList1;
  309.             InfoTree.ForeColor = (Color)System.Drawing.SystemColors.WindowText;
  310.             InfoTree.Location = new System.Drawing.Point(0, 0); 
  311.             InfoTree.AllowDrop = true;
  312.             InfoTree.HotTracking = true;
  313.             InfoTree.TabIndex = 0;
  314.             InfoTree.Indent = 19;
  315.             InfoTree.Text = "treeView1";
  316.             //ProcessTree.SelectedImageIndex = 1;
  317.             InfoTree.Size = new System.Drawing.Size(250, 400);
  318.             InfoTree.Scrollable = true;
  319.             toolTip1.SetToolTip(InfoTree, "Services and users");
  320.             InfoTree.AfterSelect += new TreeViewEventHandler(InfoTree_AfterSelect);
  321.             InfoTree.BeforeExpand += new TreeViewCancelEventHandler(InfoTree_BeforeExpand);
  322.  
  323.             InfoTree.Dock = System.Windows.Forms.DockStyle.Left;
  324.  
  325.             Splitter1 = new System.Windows.Forms.Splitter();
  326.             Splitter1.BackColor = System.Drawing.Color.Black;
  327.             Splitter1.Dock = System.Windows.Forms.DockStyle.Left;
  328.             Splitter1.Location = new System.Drawing.Point(250, 0);
  329.             Splitter1.Size = new System.Drawing.Size(3, 400);
  330.             Splitter1.TabIndex = 2;
  331.             Splitter1.TabStop = false;
  332.  
  333.             GrpBox.Location = new System.Drawing.Point(16, 15);
  334.             GrpBox.TabIndex = 0;
  335.             GrpBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
  336.             GrpBox.TabStop = false;
  337.             GrpBox.Text = "";
  338.             GrpBox.Size = new System.Drawing.Size(318, 200);
  339.  
  340.             Panel2 = new System.Windows.Forms.Panel();
  341.             Panel2.Text = "ContentPanel";
  342.             Panel2.Location = new System.Drawing.Point(253, 0);
  343.             Panel2.Size = new System.Drawing.Size(350, 200);
  344.             Panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  345.             Panel2.Dock = System.Windows.Forms.DockStyle.Fill;
  346.             toolTip1.SetToolTip(Panel2, "Information about the selected item in tree.");
  347.             Panel2.TabIndex = 0;
  348.  
  349.             Panel2.Controls.Add(GrpBox);
  350.       
  351.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  352.             this.ClientSize = new System.Drawing.Size(600, 400);
  353.             this.Text = "TreeView";
  354.             this.FormBorderStyle = FormBorderStyle.Fixed3D;
  355.             
  356.             //this.MinimumSize = new System.Drawing.Size(502, 400);
  357.         
  358.             toolTip1.Active = true;
  359.             //@design toolTip1.SetLocation(new System.Drawing.Point(20, 70));
  360.         
  361.             //imageList1.ImageStream = (ImageListStreamer)resources.GetObject("imageList1.ImageStream");
  362.             imageList1.TransparentColor = (Color)System.Drawing.Color.Transparent;
  363.             //@design imageList1.SetLocation(new System.Drawing.Point(20, 10));
  364.         
  365.             this.Controls.Add(Panel2);
  366.             this.Controls.Add(Splitter1);
  367.             this.Controls.Add(InfoTree);
  368.         
  369.         }
  370.  
  371.         [STAThread]
  372.         public static int Main(string[] args)
  373.         { 
  374.             Application.Run(new PSForm());
  375.             return 100;
  376.         }
  377.     }
  378. }
  379.  
  380.  
  381.  
  382.