1.1.47
by Pedro Fragoso
Import upstream version 0.13.6 |
1 |
/*
|
2 |
* ManagedWinapi - A collection of .NET components that wrap PInvoke calls to
|
|
3 |
* access native API by managed code. http://mwinapi.sourceforge.net/
|
|
4 |
* Copyright (C) 2006 Michael Schierl
|
|
5 |
*
|
|
6 |
* This library is free software; you can redistribute it and/or
|
|
7 |
* modify it under the terms of the GNU Lesser General Public
|
|
8 |
* License as published by the Free Software Foundation; either
|
|
9 |
* version 2.1 of the License, or (at your option) any later version.
|
|
10 |
* This library is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 |
* Lesser General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
|
16 |
* License along with this library; see the file COPYING. if not, visit
|
|
17 |
* http://www.gnu.org/licenses/lgpl.html or write to the Free Software
|
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
19 |
*/
|
|
20 |
using System; |
|
21 |
using System.Collections.Generic; |
|
22 |
using System.Text; |
|
23 |
using System.Windows.Forms; |
|
24 |
||
25 |
namespace ManagedWinapi.Windows |
|
26 |
{
|
|
27 |
||
28 |
/// <summary>
|
|
29 |
/// Called by an EventDispatchingNativeWindow when a window message is received
|
|
30 |
/// </summary>
|
|
31 |
/// <param name="m">The message to handle.</param>
|
|
32 |
/// <param name="handled">Whether the event has already been handled. If this value is true, the handler
|
|
33 |
/// should return immediately. It may set the value to true to indicate that no others
|
|
34 |
/// should handle it. If the event is not handled by any handler, it is passed to the
|
|
35 |
/// default WindowProc.</param>
|
|
36 |
public delegate void WndProcEventHandler(ref Message m, ref bool handled); |
|
37 |
||
38 |
/// <summary>
|
|
39 |
/// A Win32 native window that delegates window messages to handlers. So several
|
|
40 |
/// components can use the same native window to save "USER resources". This class
|
|
41 |
/// is useful when writing your own components.
|
|
42 |
/// </summary>
|
|
43 |
public class EventDispatchingNativeWindow : NativeWindow |
|
44 |
{
|
|
45 |
||
46 |
private static Object myLock = new Object(); |
|
47 |
private static EventDispatchingNativeWindow _instance; |
|
48 |
||
49 |
/// <summary>
|
|
50 |
/// A global instance which can be used by components that do not need
|
|
51 |
/// their own window.
|
|
52 |
/// </summary>
|
|
53 |
public static EventDispatchingNativeWindow Instance |
|
54 |
{
|
|
55 |
get
|
|
56 |
{
|
|
57 |
lock (myLock) |
|
58 |
{
|
|
59 |
if (_instance == null) |
|
60 |
_instance = new EventDispatchingNativeWindow(); |
|
61 |
return _instance; |
|
62 |
}
|
|
63 |
}
|
|
64 |
}
|
|
65 |
||
66 |
/// <summary>
|
|
67 |
/// Attach your event handlers here.
|
|
68 |
/// </summary>
|
|
69 |
public event WndProcEventHandler EventHandler; |
|
70 |
||
71 |
/// <summary>
|
|
72 |
/// Create your own event dispatching window.
|
|
73 |
/// </summary>
|
|
74 |
public EventDispatchingNativeWindow() |
|
75 |
{
|
|
76 |
CreateHandle(new CreateParams()); |
|
77 |
}
|
|
78 |
||
79 |
/// <summary>
|
|
80 |
/// Parse messages passed to this window and send them to the event handlers.
|
|
81 |
/// </summary>
|
|
82 |
/// <param name="m">A System.Windows.Forms.Message that is associated with the
|
|
83 |
/// current Windows message.</param>
|
|
84 |
protected override void WndProc(ref Message m) |
|
85 |
{
|
|
86 |
bool handled = false; |
|
87 |
if (EventHandler != null) |
|
88 |
EventHandler(ref m, ref handled); |
|
89 |
if (!handled) |
|
90 |
base.WndProc(ref m); |
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|