抱歉,如果这个问题太简单了,但我在帮助文件或在线搜索中没有找到关于如何做到这一点的任何信息。我正在打开一个新的浏览器窗口来测试基于Web的应用程序的登录/注销功能,但我想以最大化的模式打开IE窗口。我可以设置大小为:
$ie.height = 1024
$ie.width - 768
但是是否有关键字或任何东西可以自动最大化它,还是我需要先查询屏幕大小,然后从该查询中填充值?
/matt
但是是否有关键字或任何东西可以自动最大化它,还是我需要先查询屏幕大小,然后从该查询中填充值?
/matt
Function maxIE
{
param($ie)
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.Width = $screen.width
$ie.Height =$screen.height
$ie.Top = 0
$ie.Left = 0
}
cls
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
maxIE $ie
while ($ie.busy) {sleep -milliseconds 50}
$ie.navigate("http://www.google.com")
(new-object -com wscript.shell).run("url",3)
我没有找到任何特定的答案来解决问题,但是我找到了一种组合方法。调用它们的顺序很重要,否则它不起作用。
完整示例:
$ie = New-Object -Com "InternetExplorer.Application"
$urls = @("http://www.google.com","http://www.yahoo.com")
$ie.Visible = $true
CLS
write-output "Loading pages now..."
#Maximize IE window
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.height = $screen.height
#Open webpages
foreach ($link in $urls) {
$ie.Navigate2($link, 0x1000)
}
#close first blank tab
$sa = New-Object -ComObject Shell.Application
$tab = $sa.windows() | Where {$_.Name -match 'Internet Explorer' -and $_.LocationName -eq ''}
$tab.quit()
如果您在PowerShell 2.0上安装了PowerShell社区扩展1.2(PSCX),我已经验证过这个可以工作:
Pscx\Start-Process IExplore.exe; Start-Sleep 3; $hwnd = Get-ForegroundWindow
$sig = @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 3)
这有点棘手,因为它使用了一个等待(start-sleep)3秒钟的方法来等待IE打开,然后使用PSCX cmdlet获取前台窗口的句柄。 如果您只运行了一个IExplore实例,则可以使用此方法获取该句柄:
@(Get-Process IExplore)[0].MainWindowHandle
需要PowerShell 2.0才能支持Add-Type,从而允许我们调用Win32 API。
顺便说一句,通过快速的Bing搜索,似乎让IE启动最大化是一个相当普遍的问题。例如,使用Start-Process可以指定-WindowStyle Maximized,但IE不会遵守这个设置。
如果有人需要帮助,我最终只是调用了最大化的iexplore,然后连接到它。你需要睡眠,因为它调用太快了,无法连接。我相信有更好的方法,但我想不出来。
start iexplore -WindowStyle maximized
Start-Sleep -seconds 1
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
$ie.navigate("URL")
#We will use the Win32 API function ShowWindowAsync, and spawn an IE Window Maximized.
#Parameters can be used for ShowWindowAsync
$Hide = 0
$Normal = 1
$Minimized = 2
$Maximized = 3
$ShowNoActivateRecentPosition = 4
$Show = 5
$MinimizeActivateNext = 6
$MinimizeNoActivate = 7
$ShowNoActivate = 8
$Restore = 9
$ShowDefault = 10
$ForceMinimize = 11
#Specify an interwebs address :)
$URL="http://www.google.com/"
#Create internetexplorer.application object
$IE=new-object -com internetexplorer.application
#Set some parameters for the internetexplorer.application object
$IE.TheaterMode = $False
$IE.AddressBar = $True
$IE.StatusBar = $False
$IE.MenuBar = $True
$IE.FullScreen = $False
$IE.visible = $True
#Navigate to the URL
$IE.navigate2($URL)
#the C#-style signature of an API function
$code = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
#add signature as new type to PowerShell (for this session)
$type = Add-Type -MemberDefinition $code -Name myAPI -PassThru
#Magic:
$type::ShowWindowAsync($IE.HWND[0], $Maximized)