コード例 #1
0
 /// <summary>
 /// !do not alter this code! It's designer code
 /// </summary>
 private void InitializeComponent()
 {
     this.ucWordType1 = new WFControls.CS.Document.ucWordType();
     this.SuspendLayout();
     //
     // ucWordType1
     //
     this.ucWordType1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(163)))), ((int)(((byte)(61)))));
     this.ucWordType1.Location  = new System.Drawing.Point(0, 271);
     this.ucWordType1.Name      = "ucWordType1";
     this.ucWordType1.Size      = new System.Drawing.Size(678, 33);
     this.ucWordType1.TabIndex  = 0;
     //
     // WinWordControl
     //
     this.AllowDrop = true;
     this.Controls.Add(this.ucWordType1);
     this.Name    = "WinWordControl";
     this.Size    = new System.Drawing.Size(678, 353);
     this.Load   += new System.EventHandler(this.WinWordControl_Load);
     this.Resize += new System.EventHandler(this.OnResize);
     this.ResumeLayout(false);
 }
コード例 #2
0
        /// <summary>
        /// Loads a document into the control
        /// </summary>
        /// <param name="t_filename">path to the file (every type word can handle)</param>
        public void LoadDocument(string t_filename, bool ManualOpen)
        {
            docFullName      = t_filename;
            deactivateevents = true;
            FileID           = string.Empty;
            filename         = t_filename;
            try
            {
                if (wAppC == null)
                {
                    wAppC = new Word.ApplicationClass();
                }
                try
                {
                    wAppC.CommandBars.AdaptiveMenus = false;
                    wAppC.DocumentBeforeClose      += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
                    wAppC.NewDocument  += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
                    wAppC.DocumentOpen += new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
                    // wd.DocumentBeforeSave += new Word.ApplicationEvents2_DocumentBeforeSaveEventHandler(OnBeforSaveDoc);
                    wAppC.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
                }
                catch { }

                if (wDoc != null)
                {
                    try
                    {
                        object dummy = null;
                        wAppC.Documents.Close(ref dummy, ref dummy, ref dummy);

                        wordWnd = 0;
                        //wDoc = null;
                    }
                    catch { }
                }

                if (wordWnd == 0)
                {
                    wordWnd = FindWindow("Opusapp", null);
                }
                if (wordWnd != 0)
                {
                    if (!ManualOpen)
                    {
                        SetParent(wordWnd, this.Handle.ToInt32());
                    }
                    object fileName    = filename;
                    object newTemplate = false;
                    object docType     = 0;
                    object readOnly    = true;
                    object isVisible   = true;
                    object missing     = System.Reflection.Missing.Value;

                    try
                    {
                        if (wAppC == null)
                        {
                            throw new WordInstanceException();
                        }

                        if (wAppC.Documents == null)
                        {
                            throw new DocumentInstanceException();
                        }

                        if (wAppC != null && wAppC.Documents != null)
                        {
                            //document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

                            object file       = fileName; //this is the path
                            object nullobject = System.Reflection.Missing.Value;
                            wDoc = wAppC.Documents.Open(ref file, ref nullobject, ref nullobject, ref nullobject,
                                                        ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                                                        ref nullobject, ref nullobject, ref nullobject, ref nullobject);


                            //MessageBox.Show(document.Path + document.FullName);
                        }

                        if (wDoc == null)
                        {
                            throw new ValidDocumentException();
                        }
                    }
                    catch
                    {
                    }

                    try
                    {
                        wAppC.ActiveWindow.DisplayRightRuler        = false;
                        wAppC.ActiveWindow.DisplayScreenTips        = false;
                        wAppC.ActiveWindow.DisplayVerticalRuler     = false;
                        wAppC.ActiveWindow.DisplayRightRuler        = false;
                        wAppC.ActiveWindow.ActivePane.DisplayRulers = false;
                        wAppC.ActiveWindow.ActivePane.View.Type     = Word.WdViewType.wdPrintView;

                        //wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;//wdWebView; // .wdNormalView;
                    }
                    catch
                    {
                    }


                    /// Code Added
                    /// Disable the specific buttons of the command bar
                    /// By default, we disable/hide the menu bar
                    /// The New/Open buttons of the command bar are disabled
                    /// Other things can be added as required (and supported ..:) )
                    /// Lots of commented code in here, if somebody needs to disable specific menu or sub-menu items.
                    ///



                    int counter = wAppC.ActiveWindow.Application.CommandBars.Count;
                    for (int i = 1; i <= counter; i++)
                    {
                        try
                        {
                            String nm = wAppC.ActiveWindow.Application.CommandBars[i].Name;
                            /// MessageBox.Show("The menu:" + nm);

                            ////int count_control1 = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
                            ////for (int j = 1; j <= count_control1; j++)
                            //////for (int j = 1; j <= count_control; j++)
                            ////{
                            ////    try
                            ////    {
                            ////        //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
                            ////        //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false;
                            ////    }
                            ////    catch (Exception ex) { }

                            ////}


                            if (nm == "Standard")
                            {
                                //nm=i.ToString()+" "+nm;
                                //MessageBox.Show(nm);
                                int count_control = wAppC.ActiveWindow.Application.CommandBars[i].Controls.Count;
                                for (int j = 1; j <= 2; j++)
                                //for (int j = 1; j <= count_control; j++)
                                {
                                    try
                                    {
                                        //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
                                        wAppC.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false;
                                    }
                                    catch (Exception ex) { ErrorManager.WriteMessage("LoadDocument,part1", ex.ToString(), this.ParentForm.Text); }
                                }
                            }

                            if (nm == "Menu Bar")
                            {
                                //To disable the menubar, use the following (1) line
                                wAppC.ActiveWindow.Application.CommandBars[i].Enabled = false;

                                /// If you want to have specific menu or sub-menu items, write the code here.
                                /// Samples commented below

                                //							MessageBox.Show(nm);
                                //int count_control=wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
                                //MessageBox.Show(count_control.ToString());

                                /*
                                 * //for(int j=1;j<=count_control;j++)
                                 * for(int j=1;j<=count_control-1;j++)
                                 * {
                                 *  /// The following can be used to disable specific menuitems in the menubar
                                 *  //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled=false;
                                 *  wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption = "سلام";
                                 *  wd.ActiveWindow.Application.CommandBars[i].Controls[j].Delete((object)true);
                                 *
                                 *  ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
                                 *  ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
                                 *  ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].accChildCount.ToString());
                                 *
                                 *
                                 *  ///The following can be used to disable some or all the sub-menuitems in the menubar
                                 *
                                 *
                                 *  Office.CommandBarPopup c;
                                 *  c = (Office.CommandBarPopup)wd.ActiveWindow.Application.CommandBars[i].Controls[j];
                                 *
                                 *  for (int k = 1; k <= c.Controls.Count; k++)
                                 *  {
                                 *      //MessageBox.Show(k.ToString()+" "+c.Controls[k].Caption + " -- " + c.Controls[k].DescriptionText + " -- " );
                                 *      try
                                 *      {
                                 *          c.Controls[k].Enabled = false;
                                 *         // c.Controls["Close Window"].Enabled = false;
                                 *      }
                                 *      catch
                                 *      {
                                 *
                                 *      }
                                 *  }
                                 *
                                 *
                                 *
                                 *  //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Control	 Controls[0].Enabled=false;
                                 * }
                                 */
                            }

                            nm = "";
                        }
                        catch (Exception ex)
                        {
                            ErrorManager.WriteMessage("LoadDocument,part2", ex.ToString(), this.ParentForm.Text);
                            MessageBox.Show(ex.ToString());
                        }
                    }



                    // Show the word-document
                    try
                    {
                        if (!wAppC.Visible)
                        {
                            wAppC.Visible = true;
                        }


                        wAppC.Activate();
                        if (!ManualOpen)
                        {
                            SetWindowPos(wordWnd, this.Handle.ToInt32(), 0, 0, this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME | SWP_NOSIZE);
                        }

                        //Call onresize--I dont want to write the same lines twice
                        OnResize();
                    }
                    catch
                    {
                        MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
                    }

                    /// We want to remove the system menu also. The title bar is not visible, but we want to avoid accidental minimize, maximize, etc ..by disabling the system menu(Alt+Space)
                    try
                    {
                        int hMenu = GetSystemMenu(wordWnd, false);
                        if (hMenu > 0)
                        {
                            int menuItemCount = GetMenuItemCount(hMenu);
                            RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 3, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 4, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 5, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 6, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 7, MF_REMOVE | MF_BYPOSITION);
                            RemoveMenu(hMenu, menuItemCount - 8, MF_REMOVE | MF_BYPOSITION);
                            DrawMenuBar(wordWnd);
                        }
                    }
                    catch { };


                    if (!ManualOpen)
                    {
                        this.Parent.Focus();
                    }
                }
                deactivateevents = false;
            }
            catch (Exception ex)
            {
                deactivateevents = false;
                ErrorManager.WriteMessage("LoadDocument,part4", ex.ToString(), this.ParentForm.Text);
            }
            WFControls.CS.Document.ucWordType c = new WFControls.CS.Document.ucWordType();
            this.Container.Add(c);
            c.Location = new Point(0, 300);
            c.Show();
            //MessageBox.Show(ucWordType1.Location.Y.ToString());
            //ucWordType1.BringToFront();
            //ucWordType1.Visible = true;
        }