home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / mmc / CTeamPropPage.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  11.1 KB  |  333 lines

  1. //-------------------------------------------------------------
  2. // CTeamPropPage.cs
  3. //
  4. // This implements the application-specific interface IPropSheetPage
  5. // to create a property page that is used by each team in the snapin.
  6. //
  7. // The page allows the user to determine which HTML pages to display
  8. // in the result pane for each team.
  9. //
  10. // This property page uses WFC extensively.
  11. //-------------------------------------------------------------
  12.  
  13. using System;
  14. using System.Runtime.InteropServices;
  15. using System.Drawing;
  16. using System.WinForms;
  17.  
  18. namespace Microsoft.SampleMMC
  19. {
  20. public class CTeamPropPage : IPropSheetPage
  21. {
  22.     int         m_hWnd;         // The window handle for the page
  23.     CNode       m_Node;         // The node this property page represents
  24.     int         m_iDefault;     // The "default page" MMC will display
  25.     String[]    m_saResults;    // Our local copy of links
  26.  
  27.     // The list of controls on this page
  28.     ListBox     m_lbList;       // The listbox that holds possible links
  29.     TextBox     m_txtCur;       // The textbox that holds "working" link
  30.     Button      m_btnAdd;       // The "Add" button 
  31.     Button      m_btnDelete;    // The "Delete" button
  32.     Button      m_btnSet;       // The "Set" button (Sets the default link)
  33.     Label       m_label1;       // The label describing the page
  34.  
  35.     //-------------------------------------------------
  36.     // Init
  37.     //
  38.     // This function is in place in order to set up
  39.     // the page. Primarily, it is here so the Property
  40.     // page can get a reference to the Node it represents,
  41.     // so it may modify its values
  42.     //-------------------------------------------------
  43.     public void Init(ref Object info)
  44.     {
  45.         m_Node = (CNode)info;
  46.         m_iDefault = m_Node.ResultNum;
  47.  
  48.         // Now copy over the node's links and store them locally (for editing)
  49.         if (m_Node.m_oResults != null)
  50.         {
  51.             int iNum = ((String[])(m_Node.m_oResults)).Length;
  52.             m_saResults = new String[iNum];
  53.             for(int i=0; i<iNum; i++)
  54.              m_saResults[i] = ((String[])(m_Node.m_oResults))[i];
  55.         }
  56.         else
  57.             m_saResults=null;
  58.     }// Init
  59.  
  60.     //-------------------------------------------------
  61.     // Title
  62.     //
  63.     // Returns the title of the property page (the same
  64.     // title as the node it represents)
  65.     //-------------------------------------------------
  66.     public String Title()
  67.     {
  68.         return m_Node.DisplayName;
  69.     }// Title
  70.  
  71.     //-------------------------------------------------
  72.     // Icon
  73.     //
  74.     // Returns a handle to the icon representing the 
  75.     // property page (the same as the node it represents)
  76.     //-------------------------------------------------
  77.     public int Icon()
  78.     {
  79.         return m_Node.IconHandle;
  80.     }// Icon
  81.  
  82.     //-------------------------------------------------
  83.     // InsertPropSheetPageControls
  84.     //
  85.     // This function creates all the controls for the 
  86.     // property sheet and places them on the form
  87.     //-------------------------------------------------
  88.     public int InsertPropSheetPageControls(int hWnd)
  89.     {
  90.         m_lbList      = new ListBox();
  91.         m_txtCur      = new TextBox();
  92.         m_btnAdd      = new Button();
  93.         m_btnDelete   = new Button();
  94.         m_btnSet      = new Button();
  95.         m_label1      = new Label();
  96.  
  97.         
  98.         m_hWnd = hWnd;
  99.  
  100.         // Set up the Listbox first
  101.         
  102.         // We need to set the listbox's parent window to the
  103.         // window handle that was passed into the function
  104.         SetParent(m_lbList.Handle, m_hWnd);
  105.         m_lbList.Location = new Point(40, 136);
  106.         m_lbList.Size = new Size(208, 95);
  107.         m_lbList.TabIndex = 0;
  108.         m_lbList.Text = "Listed Sites";
  109.         m_lbList.UseTabStops = true;
  110.         // Add an event handler
  111.         m_lbList.AddOnClick(new EventHandler(onlbListClick));
  112.         InsertStrings();
  113.  
  114.         // Now set up the edit box
  115.         SetParent(m_txtCur.Handle, m_hWnd);
  116.         m_txtCur.Location = new Point(40, 48);
  117.         m_txtCur.Size = new Size(208, 20);
  118.         m_txtCur.TabIndex = 1;
  119.         m_txtCur.Text = "";
  120.  
  121.         // Now the add button
  122.         SetParent(m_btnAdd.Handle, m_hWnd);
  123.         m_btnAdd.Location = new Point(40, 88);
  124.         m_btnAdd.Size = new Size(64, 24);
  125.         m_btnAdd.TabIndex = 2;
  126.         m_btnAdd.Text = "Add";
  127.         // Adding an event handler here...
  128.         m_btnAdd.AddOnClick(new EventHandler(onbtnAddClick));
  129.  
  130.  
  131.         // Add the set button
  132.         SetParent(m_btnSet.Handle, m_hWnd);
  133.         m_btnSet.Location = new Point(110, 88);
  134.         m_btnSet.Size = new Size(64, 24);
  135.         m_btnSet.TabIndex = 3;
  136.         m_btnSet.Text = "Set";
  137.         // Event handler...
  138.         m_btnSet.AddOnClick(new EventHandler(onbtnSetClick));
  139.  
  140.         // Add the delete button
  141.         SetParent(m_btnDelete.Handle, m_hWnd);
  142.         m_btnDelete.Location = new Point(184, 88);
  143.         m_btnDelete.Size = new Size(64, 24);
  144.         m_btnDelete.TabIndex = 4;
  145.         m_btnDelete.Text = "Delete";
  146.         // Event handler....
  147.         m_btnDelete.AddOnClick(new EventHandler(onbtnDeleteClick));
  148.  
  149.  
  150.         // Add the label for the page
  151.         SetParent(m_label1.Handle, m_hWnd);
  152.         m_label1.Location = new Point(48, 8);
  153.         m_label1.Size = new Size(192, 24);
  154.         m_label1.TabStop = false;
  155.         m_label1.Text = "Selected Page to Visit";
  156.  
  157.         return 1;
  158.  
  159.     }// InsertPropSheetPageControls
  160.  
  161.     //-------------------------------------------------
  162.     // InsertStrings
  163.     //
  164.     // This function will add all the possibilities for
  165.     // links into the listbox
  166.     //-------------------------------------------------
  167.     private void InsertStrings()
  168.     {
  169.         String sToAdd;
  170.  
  171.         // In case there's stuff already in the listbox, let's
  172.         // clear it out
  173.         m_lbList.Items.Clear();
  174.  
  175.         // Check to make sure we have result data to display
  176.         if (m_saResults != null)
  177.         {
  178.             int iLength = m_saResults.Length;
  179.  
  180.             // We run through the list backwards, so we can insert everything
  181.             // at the front of the list and it comes out in the correct order
  182.             for(int i=iLength-1; i>=0; i--)
  183.             {
  184.                 sToAdd = m_saResults[i];
  185.  
  186.                 // Just to be cool, we'll put a * in front of the default link
  187.                 if (m_iDefault == i)
  188.                     sToAdd = "* " + sToAdd;
  189.                     
  190.                 // And now insert the item into the list
  191.                 m_lbList.InsertItem(0,sToAdd);
  192.             }
  193.         }
  194.     }// InsertStrings
  195.  
  196.     //-------------------------------------------------
  197.     // onbtnSetClick
  198.     //
  199.     // Event handler for the clicking of the set button
  200.     //-------------------------------------------------
  201.     void onbtnSetClick(Object o, EventArgs e)
  202.     {
  203.         // Make sure they have a valid item selected
  204.         if (m_lbList.SelectedIndex != -1)
  205.         {
  206.             m_iDefault = m_lbList.SelectedIndex;
  207.  
  208.             // Let's reinsert all the strings into the listbox
  209.             // so we can get the * infront of our newly selected item
  210.             InsertStrings();
  211.             // Now turn on the apply button
  212.             ActivateApply();
  213.         }            
  214.     }// onbtnSetClick
  215.  
  216.     //-------------------------------------------------
  217.     // onlbListClick
  218.     //
  219.     // Event handler for the clicking of an item in the 
  220.     // listbox
  221.     //-------------------------------------------------
  222.     void onlbListClick(Object o, EventArgs e)
  223.     {
  224.         // We'll put the currently selected item in
  225.         // the "working" box
  226.         if (m_lbList.SelectedIndex >=0)
  227.             m_txtCur.Text = m_saResults[m_lbList.SelectedIndex];
  228.     }// onlbListClick
  229.  
  230.     //-------------------------------------------------
  231.     // onbtnDeleteClick
  232.     //
  233.     // Event handler for the clicking of the delete button
  234.     //-------------------------------------------------
  235.     void onbtnDeleteClick(Object o, EventArgs e)
  236.     {
  237.         // Make sure they have a valid item selected
  238.         if (m_lbList.SelectedIndex != -1)
  239.         {
  240.             // If that was the default link, we need to make a new
  241.             // link the default
  242.             if (m_lbList.SelectedIndex == m_iDefault && m_iDefault>0)
  243.                 m_iDefault--;
  244.  
  245.             // Let's remove this item from our local Result Array
  246.             String[] tempResult = new String[m_saResults.Length-1];
  247.             int i=0, j=0; 
  248.             for(i=0;i< m_saResults.Length; i++)
  249.             {
  250.                 if (i != m_lbList.SelectedIndex)
  251.                     tempResult[j++] = m_saResults[i];
  252.             }
  253.  
  254.             m_saResults = tempResult;
  255.             
  256.             // Reinsert all the strings
  257.             InsertStrings();
  258.             // Activate the Apply button
  259.             ActivateApply();
  260.         }
  261.     }// onbtnDeleteClick
  262.     
  263.     //-------------------------------------------------
  264.     // onbtnAddClick
  265.     //
  266.     // Event handler for the clicking of the add button
  267.     //-------------------------------------------------
  268.     void onbtnAddClick(Object o, EventArgs e)
  269.     {
  270.         // Make sure we're not trying to add nothing
  271.         if (!m_txtCur.Text.Equals(""))
  272.         {
  273.  
  274.             // Let's add the selected item into our local Result array
  275.             String[] tempResult = new String[m_saResults.Length+1];
  276.             int i;
  277.             for(i=0; i<m_saResults.Length; i++)
  278.                 tempResult[i] = m_saResults[i];
  279.  
  280.             // Now add the new guy
  281.             tempResult[i] = m_txtCur.Text;
  282.  
  283.             m_saResults = tempResult;
  284.  
  285.             // Now clear out the text field.
  286.             m_txtCur.Text = "";
  287.  
  288.             // Now re-add everything to the listbox
  289.             InsertStrings();
  290.             // Turn on the apply button
  291.             ActivateApply();
  292.         }
  293.     }// onbtnAddClick
  294.  
  295.     //-------------------------------------------------
  296.     // ApplyData
  297.     //
  298.     // This function will write all the Property page
  299.     // information to the node it represents
  300.     //-------------------------------------------------
  301.     public int ApplyData()
  302.     {
  303.         m_Node.m_oResults = m_saResults;
  304.         m_Node.ResultNum = m_iDefault;
  305.  
  306.         return 1;
  307.     }// ApplyData
  308.  
  309.     //-------------------------------------------------
  310.     // ActivateApply
  311.     //
  312.     // This function will turn on the apply button by sending
  313.     // a message to the parent window
  314.     //-------------------------------------------------
  315.     private void ActivateApply()
  316.     {
  317.         SendMessage(GetParent(m_hWnd), PSM.CHANGED, (uint)m_hWnd, 0);
  318.     }// ActivateApply
  319.  
  320.     //-------------------------------------------------
  321.     // We need to import the Win32 API calls used to deal with
  322.     // window messaging.
  323.     //-------------------------------------------------
  324.     [DllImport("user32.dll")]
  325.     public static extern int SendMessage(int hWnd, uint Msg, uint wParam, uint lParam);
  326.     [DllImport("user32.dll")]
  327.     public static extern int SetParent(int hWndChild, int hWndNewParent);
  328.     [DllImport("user32.dll")]
  329.     public static extern int GetParent(int hWnd);    
  330. }// class CTeamPropPage
  331. }// namespace Microsoft.SampleMMC
  332.  
  333.