EDN Admin
Well-known member
Hi there... So basicly for testing i want my application to get the handel of the window the application opens and then cast an object as its form. To then use the Form object to do what ever, either focus or change the name or w.e But. Every
time i get a proper Windows Handle and Verify that it is the correct handle for the window im looking for using SpyWindows (it displays windows handels in HEX and DEC) i then call a couple different methods to try to Cast and Return a Form Object
but it always comes up null...
<div style="color:black; background-color:white
<pre> IntPtr temphandle = <span style="color:blue new IntPtr();
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(MAINAPP.Applications[x].ProcessName);
<span style="color:blue if(processes.Length >= 1)
{
<span style="color:blue for(<span style="color:blue int y = 0; y < processes.Length; x++)
{
<span style="color:blue if(processes[y].StartTime == MAINAPP.Applications[x].StartTime)
{
temphandle = MAINAPP.GetWindowAPI(processes[y].MainWindowTitle, <span style="color:blue true);
<span style="color:blue break;
}
}
}<br/><br/> FocusWindow(temphandle, MAINAPP.Applications[x].ProcessID);
[/code]
***Keep in mind im cycling through all open processes because i may have multiple windows with the same name or other objects so we match a couple other variables to ensure that it is the application we truly want. ***
Here is our FindWindow Method that uses the User32 to find the Window Handle.... Scince using the Process.Handle and Process.MainWindowHandle seem to work for me half the time for some reason...
<pre>internal IntPtr GetWindowAPI(string WindowName, bool wait)
{
return FindWindow(WindowName, wait);
}<br/>[DllImport("user32.dll", SetLastError = true)]<br/> static extern IntPtr FindWindow(string lpClassName, string lpWindowName);<br/><br/> // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.<br/><br/> internal static IntPtr FindWindow(string windowName, bool wait)<br/> {<br/> IntPtr hWnd = FindWindow(null, windowName);<br/> while (wait && (int)hWnd == 0)<br/> {<br/> System.Threading.Thread.Sleep(500);<br/> hWnd = FindWindow(null, windowName);<br/> }<br/><br/> return hWnd;<br/> }[/code]
<br/>
<br/>
And here are the FocusWindow Methods ive used, This is where the casting is, and then we try to Focus, just as a simple test.But i always get a object null exception because the form wont cast :
<div style="color:black; background-color:white
<pre> <span style="color:blue internal <span style="color:blue void FocusWindow(IntPtr handle, <span style="color:blue int processid)
{
<span style="color:blue try
{
System.Windows.Forms.Form someform = GetForm(handle);
<span style="color:blue if (someform == <span style="color:blue null)
{
someform = ((System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(handle));
}
<span style="color:blue if (someform != <span style="color:blue null)
{
someform.Activate();
System.Windows.Forms.MessageBox.Show(<span style="color:#a31515 "omg?");
}
<span style="color:blue else
{
System.Windows.Forms.MessageBox.Show(<span style="color:#a31515 "null");
}
}
<span style="color:blue catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString() + <span style="color:#a31515 " HANDLE ++ " + handle.ToString() + <span style="color:#a31515 " AND PROCESS ID === " + processid + <span style="color:#a31515 " AND HANDLE FROM PROCESS ID === " + System.Diagnostics.Process.GetProcessById(processid).Handle.ToString());
}
}<br/><br/><br/><span style="color:gray ///<span style="color:green / AND THEN OUR GETFORM METHOD<br/><br/> static public System.Windows.Forms.Form GetForm(IntPtr handle)<br/> {<br/> return handle == IntPtr.Zero ?<br/> null :<br/> System.Windows.Forms.Control.FromHandle(handle) as System.Windows.Forms.Form;<br/> }<br/><br/>
[/code]
<br/>
Can any one help me with this? As you can see ive tried many diffrent ways... So Now i cant get a matching Windows Handel all the time... But i cant Get a Form object... now ive done this in another part of my application however it looks at
the Handle of the Main Application and im able to cast an object from it, however applications Launched from Within my application come up null?
Did that make sense let me rephrase... if i get the windows handle of my Applications main window and cast it as a form it works.. other windows dont...
Any help?? Thanks guys?
<br/>
<br/>
<br/>
View the full article
time i get a proper Windows Handle and Verify that it is the correct handle for the window im looking for using SpyWindows (it displays windows handels in HEX and DEC) i then call a couple different methods to try to Cast and Return a Form Object
but it always comes up null...
<div style="color:black; background-color:white
<pre> IntPtr temphandle = <span style="color:blue new IntPtr();
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(MAINAPP.Applications[x].ProcessName);
<span style="color:blue if(processes.Length >= 1)
{
<span style="color:blue for(<span style="color:blue int y = 0; y < processes.Length; x++)
{
<span style="color:blue if(processes[y].StartTime == MAINAPP.Applications[x].StartTime)
{
temphandle = MAINAPP.GetWindowAPI(processes[y].MainWindowTitle, <span style="color:blue true);
<span style="color:blue break;
}
}
}<br/><br/> FocusWindow(temphandle, MAINAPP.Applications[x].ProcessID);
[/code]
***Keep in mind im cycling through all open processes because i may have multiple windows with the same name or other objects so we match a couple other variables to ensure that it is the application we truly want. ***
Here is our FindWindow Method that uses the User32 to find the Window Handle.... Scince using the Process.Handle and Process.MainWindowHandle seem to work for me half the time for some reason...
<pre>internal IntPtr GetWindowAPI(string WindowName, bool wait)
{
return FindWindow(WindowName, wait);
}<br/>[DllImport("user32.dll", SetLastError = true)]<br/> static extern IntPtr FindWindow(string lpClassName, string lpWindowName);<br/><br/> // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.<br/><br/> internal static IntPtr FindWindow(string windowName, bool wait)<br/> {<br/> IntPtr hWnd = FindWindow(null, windowName);<br/> while (wait && (int)hWnd == 0)<br/> {<br/> System.Threading.Thread.Sleep(500);<br/> hWnd = FindWindow(null, windowName);<br/> }<br/><br/> return hWnd;<br/> }[/code]
<br/>
<br/>
And here are the FocusWindow Methods ive used, This is where the casting is, and then we try to Focus, just as a simple test.But i always get a object null exception because the form wont cast :
<div style="color:black; background-color:white
<pre> <span style="color:blue internal <span style="color:blue void FocusWindow(IntPtr handle, <span style="color:blue int processid)
{
<span style="color:blue try
{
System.Windows.Forms.Form someform = GetForm(handle);
<span style="color:blue if (someform == <span style="color:blue null)
{
someform = ((System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(handle));
}
<span style="color:blue if (someform != <span style="color:blue null)
{
someform.Activate();
System.Windows.Forms.MessageBox.Show(<span style="color:#a31515 "omg?");
}
<span style="color:blue else
{
System.Windows.Forms.MessageBox.Show(<span style="color:#a31515 "null");
}
}
<span style="color:blue catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString() + <span style="color:#a31515 " HANDLE ++ " + handle.ToString() + <span style="color:#a31515 " AND PROCESS ID === " + processid + <span style="color:#a31515 " AND HANDLE FROM PROCESS ID === " + System.Diagnostics.Process.GetProcessById(processid).Handle.ToString());
}
}<br/><br/><br/><span style="color:gray ///<span style="color:green / AND THEN OUR GETFORM METHOD<br/><br/> static public System.Windows.Forms.Form GetForm(IntPtr handle)<br/> {<br/> return handle == IntPtr.Zero ?<br/> null :<br/> System.Windows.Forms.Control.FromHandle(handle) as System.Windows.Forms.Form;<br/> }<br/><br/>
[/code]
<br/>
Can any one help me with this? As you can see ive tried many diffrent ways... So Now i cant get a matching Windows Handel all the time... But i cant Get a Form object... now ive done this in another part of my application however it looks at
the Handle of the Main Application and im able to cast an object from it, however applications Launched from Within my application come up null?
Did that make sense let me rephrase... if i get the windows handle of my Applications main window and cast it as a form it works.. other windows dont...
Any help?? Thanks guys?
<br/>
<br/>
<br/>
View the full article