1
1
using System ;
2
2
using System . ComponentModel ;
3
- using System . Diagnostics ;
4
3
using System . Runtime . InteropServices ;
5
4
using System . Text ;
6
5
@@ -109,48 +108,58 @@ public bool Equals(HWND other)
109
108
}
110
109
}
111
110
111
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
112
+ internal static extern IntPtr SendMessage ( IntPtr hWnd , UInt32 Msg , IntPtr wParam , [ Out ] StringBuilder lParam ) ;
112
113
113
- [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , SetLastError = true , ExactSpelling = true ) ]
114
+
115
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , SetLastError = true , ExactSpelling = true ) ]
114
116
public static extern IntPtr GetWindowThreadProcessId ( [ In ] HWND hWnd , [ Out ] out uint ProcessId ) ;
115
117
116
118
[ DllImport ( "user32.dll" , CharSet = CharSet . Ansi ) ]
117
119
public static extern HWND GetForegroundWindow ( ) ;
118
120
119
121
[ DllImport ( "user32" , EntryPoint = "GetWindowTextA" , CharSet = CharSet . Ansi , SetLastError = true , ExactSpelling = true ) ]
120
122
public static extern int GetWindowText ( HWND hwnd , StringBuilder lpString , int cch ) ;
123
+
121
124
[ DllImport ( "user32" , EntryPoint = "GetWindowTextLengthA" , CharSet = CharSet . Ansi , SetLastError = true , ExactSpelling = true ) ]
122
125
public static extern int GetWindowTextLength ( HWND hwnd ) ;
123
126
124
127
[ DllImport ( "user32" , EntryPoint = "GetClassNameA" , CharSet = CharSet . Ansi , SetLastError = true , ExactSpelling = true ) ]
125
128
public static extern int GetClassName ( HWND hWnd , StringBuilder text , int count ) ;
126
-
127
- [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
128
- public static extern bool IsWindow ( HWND hwnd ) ;
129
+
130
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
131
+ public static extern bool IsWindow ( HWND hwnd ) ;
129
132
130
133
public delegate void WinEventDelegate ( IntPtr hWinEventHook , uint eventType , HWND hwnd , int idObject , int idChild , uint dwEventThread , uint dwmsEventTime ) ;
131
134
132
135
[ DllImport ( "user32.dll" ) ]
133
136
public static extern IntPtr SetWinEventHook ( uint eventMin , uint eventMax , IntPtr hmodWinEventProc , WinEventDelegate lpfnWinEventProc , uint idProcess , uint idThread , uint dwFlags ) ;
134
137
135
- internal const uint WINEVENT_OUTOFCONTEXT = 0 ;
136
- internal const int EVENT_OBJECT_DESTROY = 0x8001 ;
137
- internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003 ;
138
+ internal const uint WINEVENT_OUTOFCONTEXT = 0 ;
139
+ internal const int EVENT_OBJECT_DESTROY = 0x8001 ;
140
+ internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003 ;
138
141
internal const uint EVENT_SYSTEM_MINIMIZEEND = 0x0017 ;
139
- internal const int MAX_PATH = 260 ;
142
+ internal const int MAX_PATH = 260 ;
143
+ //
144
+ // Window text
145
+ //
146
+
147
+ internal const uint WM_GETTEXTLENGTH = 0x000E ;
148
+ internal const uint WM_GETTEXT = 0x000D ;
140
149
141
150
142
151
//
143
152
// IAccessible / OLEACC / WinEvents
144
153
//
145
154
146
- public const int CHILDID_SELF = 0 ;
155
+ public const int CHILDID_SELF = 0 ;
147
156
public const int STATE_SYSTEM_UNAVAILABLE = 0x00000001 ;
148
- public const int STATE_SYSTEM_FOCUSED = 0x00000004 ;
149
- public const int OBJID_CARET = - 8 ;
150
- public const int OBJID_CLIENT = - 4 ;
151
- public const int OBJID_MENU = - 3 ;
152
- public const int OBJID_SYSMENU = - 1 ;
153
- public const int OBJID_WINDOW = 0 ;
157
+ public const int STATE_SYSTEM_FOCUSED = 0x00000004 ;
158
+ public const int OBJID_CARET = - 8 ;
159
+ public const int OBJID_CLIENT = - 4 ;
160
+ public const int OBJID_MENU = - 3 ;
161
+ public const int OBJID_SYSMENU = - 1 ;
162
+ public const int OBJID_WINDOW = 0 ;
154
163
}
155
164
156
165
public static uint ForegroundProcessId
@@ -168,9 +177,9 @@ public static uint ForegroundProcessId
168
177
/// </summary>
169
178
public static string GetWindowText ( NativeMethods . HWND window )
170
179
{
171
- var length = NativeMethods . GetWindowTextLength ( window ) + 1 ;
172
- var sb = new StringBuilder ( length ) ;
173
- var result = NativeMethods . GetWindowText ( window , sb , NativeMethods . MAX_PATH ) ;
180
+ var length = ( int ) NativeMethods . SendMessage ( window , NativeMethods . WM_GETTEXTLENGTH , IntPtr . Zero , null ) ;
181
+ var sb = new StringBuilder ( length + 1 ) ;
182
+ var result = NativeMethods . SendMessage ( window , NativeMethods . WM_GETTEXT , sb . Capacity , sb ) ;
174
183
var lastWin32Error = Marshal . GetLastWin32Error ( ) ;
175
184
176
185
if ( result == 0 )
@@ -186,8 +195,8 @@ public static string GetWindowText(NativeMethods.HWND window)
186
195
/// </summary>
187
196
public static string GetWindowClass ( NativeMethods . HWND window )
188
197
{
189
- var sb = new StringBuilder ( NativeMethods . MAX_PATH ) ;
190
- var result = NativeMethods . GetClassName ( window , sb , NativeMethods . MAX_PATH ) ;
198
+ var sb = new StringBuilder ( NativeMethods . MAX_PATH ) ;
199
+ var result = NativeMethods . GetClassName ( window , sb , NativeMethods . MAX_PATH ) ;
191
200
var lastWin32Error = Marshal . GetLastWin32Error ( ) ;
192
201
193
202
if ( result == 0 )
0 commit comments