Bonjour,
J'ai un pti souci, j'ai besoin d'utiliser du WPF depuis du COM (je réalise un add-in word, qui utilise une factory initialisant un composant COM)
J'ai d'abord réalisé un UserControl simple en WinForm, et ca marche:
Ce code marche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 namespace MyNameSpace { [ProgId("MyNameSpace.MyUC")] [Guid("A1DC8CCB-6AC3-4922-BC16-E8485C003E13")] [ComVisible(true)] public partial class MyUC: UserControl { public MyUC() { InitializeComponent(); } /// <summary> /// Register the class as a control and set it's CodeBase entry /// </summary> /// <param name="key">The registry key of the control</param> [ComRegisterFunction()] public static void RegisterClass(string key) { StringBuilder sb = new StringBuilder(key); sb.Replace(@"HKEY_CLASSES_ROOT\", ""); RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); RegistryKey ctrl = k.CreateSubKey("Control"); ctrl.Close(); RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true); inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase); inprocServer32.Close(); k.Close(); } /// <summary> /// Called to unregister the control /// </summary> /// <param name="key">Tke registry key</param> [ComUnregisterFunction()] public static void UnregisterClass(string key) { StringBuilder sb = new StringBuilder(key); sb.Replace(@"HKEY_CLASSES_ROOT\", ""); RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); k.DeleteSubKey("Control", false); k.OpenSubKey("InprocServer32", true); k.DeleteSubKey("CodeBase", false); k.Close(); } } }
seulement pour un UserControl écrit en WPF: (c'est le meme code ou presque )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 namespace MyNameSpace { /// <summary> /// Logique d'interaction pour MainUC.xaml /// </summary> [Guid("40B453FD-63CC-46FB-97DA-13828191071C")] [ProgId("MyNameSpace.MyUC")] [ClassInterface(ClassInterfaceType.None)] [ComVisible(true)] public partial class MyUC : UserControl { public MyUC() { InitializeComponent(); } /// <summary> /// Register the class as a control and set it's CodeBase entry /// </summary> /// <param name="key">The registry key of the control</param> [ComRegisterFunction()] public static void RegisterClass(string key) { StringBuilder sb = new StringBuilder(key); sb.Replace(@"HKEY_CLASSES_ROOT\", ""); RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); RegistryKey ctrl = k.CreateSubKey("Control"); ctrl.Close(); RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true); inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase); inprocServer32.Close(); k.Close(); } /// <summary> /// Called to unregister the control /// </summary> /// <param name="key">Tke registry key</param> [ComUnregisterFunction()] public static void UnregisterClass(string key) { StringBuilder sb = new StringBuilder(key); sb.Replace(@"HKEY_CLASSES_ROOT\", ""); RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); k.DeleteSubKey("Control", false); k.OpenSubKey("InprocServer32", true); k.DeleteSubKey("CodeBase", false); k.Close(); } } }
Cela ne marche pu dutout... (je n'ai pas réussi à capter l'erreur mais j'essayerai de la poster quand même héhé..)
Voila...
Bon l'astuce pour le moment c'est de docker mon UserControl fait en WPF dans mon UserControl WinForm, et d'appeler dans ma factory le UserControl WinForm....
Mais bon c'est un peu moyen quoi...
Partager