The clsWin code is huge, a few hundred lines of code. I think though the error lies somewhere in my clsEnum. I use this to enumerate all of the open windows to find the ones I want. Ill post it here:
[CS]
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace cAIM
{
/// <summary>
/// Enumerates all instances of Windows and ands them to an instianted
/// clsCollection class. Must set Collections before using.
/// </summary>
public class clsEnum
{
public static clsCollection m_cCollection;
[DllImport("user32.dll")] private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder ClassName, int nMaxCount);
[DllImport("User32.Dll", CharSet=CharSet.Auto)] private static extern void GetClassName(IntPtr h, StringBuilder s, int nMaxCount);
private delegate bool EnumWindowsProc(IntPtr hwnd, int lParam);
public clsEnum()
{
}
public void StartEnum()
{
EnumWindowsProc callbackProc = new EnumWindowsProc(EnumWin);
EnumWindows(callbackProc,0);
callbackProc = null;
ForceUpdate();//Force to check for validity
}
public void Setup(clsCollection cCollection)
{
m_cCollection = cCollection;
}
private static bool EnumWin(IntPtr Hwnd, int lParam)
{
string tcl = "WindowClass";
string gcl = GetClass(Hwnd);
string strWin = WindowText(Hwnd);
if (gcl == tcl)
{
if (strWin == "caption")
{
}
else
{
AddWin(Hwnd);
}
}
return true;
}
private static string WindowText(IntPtr Hwnd)
{
StringBuilder mtitle = new StringBuilder(256);
IntPtr mywin = Hwnd;
GetWindowText(mywin,mtitle,256);
return mtitle.ToString();
}
private static string GetClass(IntPtr Hwnd)
{
StringBuilder mclass = new StringBuilder(256);
GetClassName(Hwnd,mclass,256);
return mclass.ToString();
}
private static void AddWin(IntPtr hWnd)
{
clsWin nWin = new clsWin(hWnd);
int counter = 0;
bool bflag = false;
for(counter = 0; counter < m_cCollection.Count; counter++)
{
if (m_cCollection.Item(counter).m_hWnd == hWnd)
{
bflag = true;
nWin = m_cCollection.Item(counter);
break;
}
}
if (bflag == true)
{
//do nothing its in there, it will be force updated
}
else
{
m_cCollection.Add(nWin);
}
nWin = null;
}
private void ForceUpdate()
{
int counter = 0;
bool btest = false;
for(counter = 0; counter < m_cCollection.Count; counter++)
{
btest = false;
btest = m_cCollection.Item(counter).Update();
if (btest == false)
{
m_cCollection.Remove(counter);
}
}
}
}//end class
}//end namespace
[/CS]