-->

Set Height, Width and screen location of windows e

2019-09-15 17:45发布

问题:

Using c# I'm trying to set the size and position of a Windows Explorer window I'm starting up with my program. I've looked a little bit into SetWindowPos but I don't think that works too good for Windows Explorer. I've come across this bit of code and I think it will do what I need but I don't know how to use it.

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
            string filename;
            ArrayList windows = new ArrayList();
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if (filename.Equals("explorer"))
                {
                    ie.WindowSetHeight += Ie_WindowSetHeight;
                    Console.WriteLine(ie.HWND.ToString());
                }
                Console.ReadLine();
            }

Correct me if I'm wrong, but this code snipped loops through all of the windows in SHDocVw.ShellWindows and prints the HWND of all of the "explorer", which is to say all of the windows explorer windows to the console. Upon looking into the different parts of ie. I come across "WindowSetHeight","WindowSetWidth","WindowSetTop" and 'WindowSetResizeable". Just judging by the names these sound like exactly what I would like to use, but when I tried to set the value of them it says they need to be followed by either += or -= and I can't make sense of where to go from there. Any help would be greatly appreciated

回答1:

To set the explorer window's height, width and location you need to use the Left, Top, Width and Height properties on the ie object in your code.

The WindowSetHeight that you are attempting to use is an event, it will fire when the window height is set see the MSDN reference.

Here is a code example that opens up an explorer window to the root of drive C and then sets the position and size of the window (and any other explorer window that happens to be open).

Process.Start( @"c:\");

foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
    if (Path.GetFileNameWithoutExtension(window.FullName).ToLowerInvariant() == "explorer")
    {
        window.Left = 150;
        window.Top = 200;
        window.Width = 800;
        window.Height = 600;
    }
}

Also you need to make sure you add a reference to both ShDocVW.dll and Shell32.dll which can both be found in C:\Windows\System32.