⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 documentprotectorform.cs

📁 文件保护 文件保护 文件保护
💻 CS
📖 第 1 页 / 共 3 页
字号:
            // If there are no children to the given node,
            // there is nothing to propagate up to the node,
            // so just return.
            if (node.Nodes.Count == 0)
                return;

            bool areSame = true;

            // Loop through the children of the given node
            // and make sure that their checked status corresponds
            // correctly with their area's corresponding protection status.
            foreach (TreeNode childNode in node.Nodes)
            {
                if (childNode.Tag != null)
                {
                    Debug.Assert(childNode.Tag is WordProtection.WordArea);

                    WordProtection.WordArea area =
                        (WordProtection.WordArea)childNode.Tag;

                    childNode.Checked = !area.IsProtected;
                }

                // Propagate changes down to each child node in the tree.
                this.PropagateTreeChanges(childNode);

                // If any child nodes are different, then the parent
                // node should remain unchecked.  If they are all
                // the same, check the parent node as well.
                if (node.FirstNode.Checked != childNode.Checked)
                    areSame = false;
            }

            // Update the current node to be the same
            // as its children if they are all the same.
            if (areSame)
                node.Checked = node.FirstNode.Checked;
            else
                node.Checked = false;

            // Update the protection of the node
            // in the case that it may have changed.
            SetNodeProtection(node);
        }

        /// <summary>
        /// Updates the given node and its children to
        /// the correct visual state and protection state.
        /// </summary>
        /// <param name="node">
        /// The node to be updated.
        /// </param>
        /// <param name="check">
        /// Whether the node should be checked or not.
        /// </param>
        private void UpdateNodeProtectionState(TreeNode node, bool check)
        {
            Debug.Assert(node != null);

            // Set the node to the appropriate check state.
            node.Checked = check;
            SetNodeProtection(node);

            // Propagate the check status down to the
            // children of the given node.
            foreach (TreeNode childNode in node.Nodes)
            {
                UpdateNodeProtectionState(childNode, check);
            }
        }

        /// <summary>
        /// Sets the appropriate protection for the
        /// node represented by the given TreeNode.
        /// </summary>
        /// <param name="node">
        /// The node who's corresponding area should be updated.
        /// </param>
        private static void SetNodeProtection(TreeNode node)
        {
            Debug.Assert(node != null);

            // If this node represents an area, setting the
            // check state needs to also update the area's
            // protection state.
            if (node.Tag != null)
            {
                Debug.Assert(node.Tag is WordProtection.WordArea);

                WordProtection.WordArea area = (WordProtection.WordArea)node.Tag;

                if (node.Checked)
                    area.Unprotect();
                else
                    area.Protect();
            }
        }
        #endregion
        #endregion

        #region Event Handlers
        #region Form Event Handlers
        /// <summary>
        /// Handles the Form Load event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void DocumentProtectorForm_Load(object sender, System.EventArgs e)
        {
            // Create a new instance of Word to use in this application.
            // Initialize the application to not be visible, not run
            // macros, and to not display any alerts to the user.
            this.application = new Word.ApplicationClass();
            this.application.AutomationSecurity =
                Office.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
            this.application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
            this.application.Visible = false;

            // Initialize the Open dialog to start at the same directory
            // that Word uses as its default file path for the current user.
            this.openDialog.InitialDirectory =
                this.application.Options.get_DefaultFilePath(
                Word.WdDefaultFilePath.wdDocumentsPath);
            this.application.DocumentBeforeClose +=
                new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler
                (application_DocumentBeforeClose);
        }

        /// <summary>
        /// Handles the From Closing event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">FormClosingEventArgs that contains event data.</param>
        private void DocumentProtectorForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            // Check to see if the current document can be closed.
            if (this.CanCloseDocument())
            {
                // Close the document.  The user should have saved changes
                // to the document by now, so no need to save changes when
                // closing it.
                this.CloseOpenDocument(false);
                if ((this.application.Documents.Count == 0) &&
                    (!this.application.Visible))
                {
                    // If there are no documents left in Word, shut it down.
                    object missing = System.Reflection.Missing.Value;

                    this.application.Quit(ref missing, ref missing, ref missing);
                }
            }
            else

                // Cannot close the current document, so do 
                // not close the form yet.
                e.Cancel = true;
        }
        #endregion

        #region Button Event Handlers
        /// <summary>
        /// Handles the Reset Button Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void resetButton_Click(object sender, System.EventArgs e)
        {
            this.RevertDocument();
        }

        #endregion

        #region Menu Item Event Handlers
        #region File Menu Item Event Handlers
        /// <summary>
        /// Handles the Open Menu Item Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void openMenuItem_Click(object sender, System.EventArgs e)
        {
            // Show the File Open Dialog if the user
            // indicates that it is ok to close the
            // current document.
            Debug.Assert(this.openDialog != null);
            if (this.CanCloseDocument())
            {
                if (this.openDialog.ShowDialog(this) == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    this.OpenNewDocument(this.openDialog.FileName);
                    Cursor.Current = Cursors.Default;
                }
            }
        }

        /// <summary>
        /// Handles the Save Menu Item Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void saveMenuItem_Click(object sender, System.EventArgs e)
        {
            // Save the currently open document.
            this.SaveDocument();
        }

        /// <summary>
        /// Handles the Exit Menu Item Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void exitMenuItem_Click(object sender, System.EventArgs e)
        {
            // Close the form.
            this.Close();
        }
        #endregion

        #region Protection Menu Item Handlers
        /// <summary>
        /// Handles the Protect/Unprotect Menu Item Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void protectMenuItem_Click(object sender, System.EventArgs e)
        {
            // Change the protection of the document.
            this.ToggleProtection();
        }

        /// <summary>
        /// Handles the Password Menu Item Click Event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contains event data.</param>
        private void passwordMenuItem_Click(object sender, System.EventArgs e)
        {
            // Allow the user to set the document password.
            this.SetDocumentPassword();
        }
        #endregion

        #region Help Menu Item Handlers
        private void aboutMenuItem_Click(object sender, System.EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }
        #endregion
        #endregion

        #region  TreeView Event Handlers
        /// <summary>
        /// Handles the Tree View AfterCheck event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">TreeViewEventArgs that contains event data.</param>
        private void documentItemsTree_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            Debug.Assert(this.documentItemsTree != null);
            try
            {
                // Stop handling the AfterCheck event while changes
                // are being propagated.
                this.documentItemsTree.AfterCheck -= new TreeViewEventHandler(documentItemsTree_AfterCheck);

                // Update the protection state of the given checked node
                // and its child nodes.
                this.UpdateNodeProtectionState(e.Node, e.Node.Checked);

                // Propagate changes down the tree to ensure all nodes
                // are correctly marked.
                Debug.Assert(this.documentItemsTree.TopNode != null);
                this.PropagateTreeChanges(this.documentItemsTree.TopNode);
            }
            finally
            {
                // Start handling the AfterCheck event again.
                this.documentItemsTree.AfterCheck += new TreeViewEventHandler(documentItemsTree_AfterCheck);
            }
        }
        #endregion

        #region Document Protection Event Handlers
        /// <summary>
        /// Handles the WordProtector DocumentProtected event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contain event data.</param>
        private void wordProtector_DocumentProtected(object sender, EventArgs e)
        {
            this.SetProtectionControls(false);
        }

        /// <summary>
        /// Handles the WordProtector DocumentUnprotected event.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">EventArgs that contain event data.</param>
        private void wordProtector_DocumentUnprotected(object sender, EventArgs e)
        {
            this.SetProtectionControls(true);
        }
        #endregion

        #region Word Event Handlers
        void application_DocumentBeforeClose(Microsoft.Office.Interop.Word.Document Doc, ref bool Cancel)
        {
            // If the form opened the document that is trying
            // to be closed, then cancel the document close
            // to ensure that the references to the document are
            // still available.
            if (Doc.FullName == this.wordProtector.FullName)
            {
                Cancel = true;
            }
        }
        #endregion
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -