/
WinWordControl.cs
408 lines (352 loc) · 9.58 KB
/
WinWordControl.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/// This code is written by Matthias Haenel
/// contact: www.intercopmu.de
///
/// you can use it free of charge, but please
/// mention my name ;)
///
/// WinWordControl utilizes MS-WinWord2000 and
/// WinWord-XP
///
/// It simulates a form element, with simple tricks.
///
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
namespace WinWordControl
{
/// <summary>
/// WinWordControl allows you to load doc-Files to your
/// own application without any loss, because it uses
/// the real WinWord.
/// </summary>
public class WinWordControl : System.Windows.Forms.UserControl
{
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
[DllImport("user32.dll")]
static extern int SetParent( int hWndChild, int hWndNewParent);
[DllImport("user32.dll", EntryPoint="SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
);
[DllImport("user32.dll", EntryPoint="MoveWindow")]
static extern bool MoveWindow(
int hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint
);
/* I was testing wheater i could fix some exploid bugs or not.
* I left this stuff in here for people who need to know how to
* interface the Win32-API
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern int GetWindowRect(int hwnd, ref RECT rc);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(
int hWnd,
int msg,
int wParam,
int lParam
);
*/
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
private Microsoft.Office.Interop.Word.Document document;
private Microsoft.Office.Interop.Word.ApplicationClass wd = null;
public static int wordWnd = 0;
private static string filename = null;
private static bool deactivateevents = false;
/// <summary>
/// needed designer variable
/// </summary>
private System.ComponentModel.Container components = null;
public WinWordControl()
{
InitializeComponent();
wordWnd = 0;
}
public Microsoft.Office.Interop.Word.Application Application
{
get
{
return (Microsoft.Office.Interop.Word.Application)wd;
}
}
public Microsoft.Office.Interop.Word.Document Document
{
get
{
return document;
}
}
/// <summary>
/// cleanup Ressources
/// </summary>
protected override void Dispose( bool disposing )
{
CloseControl();
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// !do not alter this code! It's designer code
/// </summary>
private void InitializeComponent()
{
//
// WinWordControl
//
this.Name = "WinWordControl";
this.Size = new System.Drawing.Size(440, 336);
this.Resize += new System.EventHandler(this.OnResize);
}
#endregion
/// <summary>
/// Preactivation
/// It's usefull, if you need more speed in the main Program
/// so you can preload Word.
/// </summary>
public void PreActivate()
{
if(wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();
}
/// <summary>
/// Close the current Document in the control --> you can
/// load a new one with LoadDocument
/// </summary>
public void CloseControl()
{
/*
* this code is to reopen Word.
*/
try
{
deactivateevents = true;
object dummy=null;
((_Document)document).Close(ref dummy, ref dummy, ref dummy);
((_Application)document.Application).Quit(ref dummy, ref dummy, ref dummy);
deactivateevents = false;
}
catch
{
}
}
/// <summary>
/// catches Word's close event
/// starts a Thread that send a ESC to the word window ;)
/// </summary>
/// <param name="doc"></param>
/// <param name="test"></param>
private void OnClose(Microsoft.Office.Interop.Word.Document doc, ref bool chancel)
{
if(!deactivateevents)
{
chancel=true;
}
}
/// <summary>
/// catches Word's open event
/// just close
/// </summary>
/// <param name="doc"></param>
private void OnOpenDoc(Microsoft.Office.Interop.Word.Document doc)
{
OnNewDoc(doc);
}
/// <summary>
/// catches Word's newdocument event
/// just close
/// </summary>
/// <param name="doc"></param>
private void OnNewDoc(Microsoft.Office.Interop.Word.Document doc)
{
if(!deactivateevents)
{
deactivateevents=true;
object dummy = null;
((_Document)doc).Close(ref dummy,ref dummy,ref dummy);
deactivateevents=false;
}
}
/// <summary>
/// catches Word's quit event
/// normally it should not fire, but just to be shure
/// safely release the internal Word Instance
/// </summary>
private void OnQuit()
{
wd=null;
}
/// <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)
{
deactivateevents = true;
filename = t_filename;
if(wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
wd.CommandBars.AdaptiveMenus = false;
wd.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
wd.ApplicationEvents4_Event_NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(OnNewDoc);
wd.DocumentOpen+= new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);
wd.ApplicationEvents2_Event_Quit += new Microsoft.Office.Interop.Word.ApplicationEvents2_QuitEventHandler(OnQuit);
}
catch{}
if(document != null)
{
try
{
object dummy=null;
wd.Documents.Close(ref dummy, ref dummy, ref dummy);
}
catch{}
}
if( wordWnd==0 ) wordWnd = FindWindow( "Opusapp", null);
if (wordWnd!=0)
{
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( wd == null )
{
throw new WordInstanceException();
}
if( wd.Documents == null )
{
throw new DocumentInstanceException();
}
if( wd != null && wd.Documents != null )
{
document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
}
if(document == null)
{
throw new ValidDocumentException();
}
}
catch
{
}
try
{
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.DisplayScreenTips=false;
wd.ActiveWindow.DisplayVerticalRuler=false;
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.ActivePane.DisplayRulers=false;
wd.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView; // .wdNormalView;
}
catch
{
}
/////////////////////////////////////////////////////////
//int counter = wd.ActiveWindow.Application.CommandBars.Count;
//for(int i = 0; i < counter;i++)
//{
// try
// {
// wd.ActiveWindow.Application.CommandBars[i].Enabled=false;
// }
// catch
// {
// }
//}
//////////////////////////////////////////////////////////////
try
{
wd.Visible = true;
wd.Activate();
SetWindowPos(wordWnd,this.Handle.ToInt32(),0,0,this.Bounds.Width+20,this.Bounds.Height+20, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
MoveWindow(wordWnd,-5,-33,this.Bounds.Width+5,this.Bounds.Height+35,true);
}
catch
{
MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
}
this.Parent.Focus();
}
deactivateevents = false;
}
/// <summary>
/// restores Word.
/// If the program crashed somehow.
/// Sometimes Word saves it's temporary settings :(
/// </summary>
public void RestoreWord()
{
try
{
/*
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=true;
}
catch
{
}
}
*/
}
catch{};
}
/// <summary>
/// internal resize function
/// utilizes the size of the surrounding control
///
/// optimzed for Word2000 but it works pretty good with WordXP too.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnResize(object sender, System.EventArgs e)
{
MoveWindow(wordWnd,-1,-20,this.Bounds.Width,this.Bounds.Height+50,true);
}
}
public class DocumentInstanceException : Exception
{}
public class ValidDocumentException : Exception
{}
public class WordInstanceException : Exception
{}
}