File tree Expand file tree Collapse file tree 2 files changed +14
-5
lines changed
Expand file tree Collapse file tree 2 files changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,9 @@ static class User32
3232 [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
3333 public static extern int GetWindowText ( IntPtr handle , StringBuilder title , int size ) ;
3434
35+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
36+ public static extern int GetWindowTextLength ( IntPtr handle ) ;
37+
3538 [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
3639 public static extern int GetClassName ( IntPtr handle , StringBuilder className , int size ) ;
3740
Original file line number Diff line number Diff line change @@ -307,13 +307,19 @@ public static void SetOpacity(IntPtr hWnd, byte opacity)
307307 SetLayeredWindowAttributes ( hWnd , 0 , opacity , LWA_ALPHA ) ;
308308 }
309309
310-
311310 public static string GetWindowText ( IntPtr hWnd )
312311 {
313- var builder = new StringBuilder ( 1024 ) ;
314- User32 . GetWindowText ( hWnd , builder , builder . Capacity ) ;
315- var windowText = builder . ToString ( ) ;
316- return windowText ;
312+ var length = GetWindowTextLength ( hWnd ) ;
313+ if ( length > 0 )
314+ {
315+ var builder = new StringBuilder ( length + 1 ) ;
316+ User32 . GetWindowText ( hWnd , builder , builder . Capacity ) ;
317+ return builder . ToString ( ) ;
318+ }
319+ else
320+ {
321+ return string . Empty ;
322+ }
317323 }
318324
319325 public static string GetClassName ( IntPtr hWnd )
You can’t perform that action at this time.
0 commit comments