@@ -19,9 +19,9 @@ You should have received a copy of the GNU Affero General Public License along w
1919using System . Windows . Forms ;
2020using System . Runtime . InteropServices ;
2121
22- class CenterWinDialog : IDisposable {
22+ partial class CenterWinDialog : IDisposable {
2323 private int mTries = 0 ;
24- private Form mOwner ;
24+ private readonly Form mOwner ;
2525
2626 public CenterWinDialog ( Form owner ) {
2727 mOwner = owner ;
@@ -32,20 +32,19 @@ public CenterWinDialog(Form owner) {
3232 private void findDialog ( ) {
3333 // Enumerate windows to find the message box
3434 if ( mTries < 0 ) return ;
35- EnumThreadWndProc callback = new EnumThreadWndProc ( checkWindow ) ;
35+ EnumThreadWndProc callback = new ( checkWindow ) ;
3636 if ( EnumThreadWindows ( GetCurrentThreadId ( ) , callback , IntPtr . Zero ) ) {
3737 if ( ++ mTries < 10 ) mOwner . BeginInvoke ( new MethodInvoker ( findDialog ) ) ;
3838 }
3939 }
4040 private bool checkWindow ( IntPtr hWnd , IntPtr lp ) {
4141 // Checks if <hWnd> is a dialog
42- StringBuilder sb = new StringBuilder ( 260 ) ;
43- GetClassName ( hWnd , sb , sb . Capacity ) ;
44- if ( sb . ToString ( ) != "#32770" ) return true ;
42+ StringBuilder sb = new ( 260 ) ;
43+ int classNameResult = GetClassName ( hWnd , sb , sb . Capacity ) ;
44+ if ( classNameResult == 0 || sb . ToString ( ) != "#32770" ) return true ;
4545 // Got it
46- Rectangle frmRect = new Rectangle ( mOwner . Location , mOwner . Size ) ;
47- RECT dlgRect ;
48- GetWindowRect ( hWnd , out dlgRect ) ;
46+ Rectangle frmRect = new ( mOwner . Location , mOwner . Size ) ;
47+ GetWindowRect ( hWnd , out RECT dlgRect ) ;
4948 MoveWindow ( hWnd ,
5049 frmRect . Left + ( frmRect . Width - dlgRect . Right + dlgRect . Left ) / 2 ,
5150 frmRect . Top + ( frmRect . Height - dlgRect . Bottom + dlgRect . Top ) / 2 ,
@@ -63,8 +62,12 @@ public void Dispose() {
6362 private static extern bool EnumThreadWindows ( int tid , EnumThreadWndProc callback , IntPtr lp ) ;
6463 [ DllImport ( "kernel32.dll" ) ]
6564 private static extern int GetCurrentThreadId ( ) ;
66- [ DllImport ( "user32.dll" ) ]
67- private static extern int GetClassName ( IntPtr hWnd , StringBuilder buffer , int buflen ) ;
65+ [ DllImport ( "user32.dll" , CharSet = CharSet . Unicode ) ]
66+ private static extern int GetClassName (
67+ IntPtr hWnd ,
68+ [ MarshalAs ( UnmanagedType . LPWStr , SizeParamIndex = 2 ) ] StringBuilder buffer ,
69+ int buflen
70+ ) ;
6871 [ DllImport ( "user32.dll" ) ]
6972 private static extern bool GetWindowRect ( IntPtr hWnd , out RECT rc ) ;
7073 [ DllImport ( "user32.dll" ) ]
0 commit comments