איך להגדיר את ה-owner של חלון wpf בתוסף וורד
-
הקוד הבא מיועד לתוספים ב-Word המבוססים על WPF.
הוא נועד להגדיר את חלון ה-WPF כחלון בן (Owned Window) של Word. חלון בן מתנהג באופן שונה מחלון עצמאי בכך שאינו מקבל פוקוס חוץ מחלון האב, והוא ממוזער יחד איתו. התנהגות שמצוייה בחלון דיאולוג שמקושר לחלון הפתוח. אחרת החלון יתנהל כמו תוכנה נפרדת לחלוטין מצד אופן התצוגה שלו ואף יופיע מעל חלונות של תוכנות אחרות שאינם קשורות אליו כלל.//יש להוסיף שם מחלקה - את שם הפרוייקט של התוסף אחרת globals לא ייקלט using System; using System.Diagnostics; using System.Windows; using System.Windows.Interop; public static class WordWindowOwner { public static void SetOwner(Window window) { try { var content = window.Content; //if (content != null) { window.Content = null; } // optional remove window content if nessecary for perfomance isssues IntPtr wordWindowHandle = IntPtr.Zero; var activeWindow = Globals.ThisAddIn.Application.ActiveWindow; wordWindowHandle = new IntPtr(activeWindow.Hwnd); WindowInteropHelper helper = new WindowInteropHelper(window); helper.Owner = wordWindowHandle; //if (content != null) { window.Content = content; } } catch (Exception ex) { Debug.WriteLine($"Error in SetOwner: {ex.Message}"); } } }