1

I got this code:

using System.Runtime.InteropServices;

[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String WindowName);
[DllImport("User32")]
private static extern int ShowWindow(int hWnd, int nCmdShow);
private const int SW_HIDE = 0;


int hWnd = FindWindow(null, Microsoft Excel - Book1);
if (hWnd > 0)
{
    ShowWindow(hWnd, SW_HIDE);
}

But sometimes im oppening Book1 with OpenOffice.org.. and my question is, how can i SW_HIDE different windows titles?

If Microsoft Excel - Book1 title exists

If Book1 - OpenOffice.org Calc title exists

Maybe it is possible to find windows title part "Book1"

Thank you very much!

Victor
  • 9,210
  • 3
  • 26
  • 39
b00sted 'snail'
  • 354
  • 1
  • 13
  • 27
  • 1
    You need to enumerate all windows and check their titles (as a start see [here](http://stackoverflow.com/a/7268384/21567)). What exactly are you trying to achieve by hiding the windows? Maybe there is a better approach altogether. – Christian.K Jun 05 '14 at 09:47
  • FindWindow() is rather crude, entirely useless in this case. You'd be ahead with Process.MainWindowHandle. Which window you are *actually* going to hide is still largely a random happenstance when you do this with an app that can create many windows, none of them special as the "main" window. Don't do this. – Hans Passant Jun 05 '14 at 10:10

1 Answers1

0

Use the below code to get the list of all opened windows.

[DllImport("user32.dll")]
        static extern bool EnumWindows(EnumDelegate lpfn, IntPtr lParam);

then write the delegate function as below:

public string[] win_List = new string[50];
int i = 0;
public bool lpfn(IntPtr hWnd, int lParam)
{
    StringBuilder stbrTitle = new StringBuilder(255);
    int titleLength = GetWindowText(hWnd, stbrTitle, stbrTitle.Capacity + 1);
    string strTitle = stbrTitle.ToString();
    if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
    {
        win_List[i++] = strTitle;                      
    }
    return true;
}


public string[] GetWinList()
{
    EnumDelegate del_fun = new EnumDelegate(lpfn);
    EnumWindows(del_fun, IntPtr.Zero);
    return win_List;
}
Ricky
  • 2,323
  • 6
  • 22
  • 22