2012-10-23 10 views
5

jestem otwierając stronę w IE przy użyciu C#otworzyć stronę w IE przy użyciu C#

Kod:

Process.Start("IExplore.exe", "www.northwindtraders.com");

Ale w jaki sposób można zmienić rozmiar strony z następującymi characteritics: toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width='622', height='582'

+0

Czy potrzebujesz uruchomić rzeczywistą instancję IE, czy możesz użyć czegoś takiego jak klasa WebBrowser? http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx – JoshVarty

+0

Cóż, chcę tylko otworzyć nowe okno w IE. Ale potrzebuję otworzyć to nowe okno przeskalowane. Potrzebuję, aby ta strona została otwarta w IE –

+0

Sprawdź to również http://stackoverflow.com/questions/1431115/open-a-webpage-in-ie-using-c-sharp –

Odpowiedz

9

Dla tego poziomu drobnoziarnistej kontroli, której nie wierzę, są dostępne z linii poleceń; dodaj odniesienie (COM) do Microsoft Internet Controls wtedy możesz;

var IE = new SHDocVw.InternetExplorer(); 
object URL = "http://www.northwindtraders.com"; 

IE.ToolBar = 0; 
IE.StatusBar = false; 
IE.MenuBar = false; 
IE.Width = 622; 
IE.Height = 582; 
IE.Visible = true; 

IE.Navigate2(ref URL); 
+0

czy możesz pokazać przykłady użycia ta technika (chcę podać nazwę użytkownika i hasło) –

1

Musisz użyć właściwości StartInfo, aby ustawić niektóre parametry uruchamiania.

Sprawdź to, link, jakie opcje są dostępne.

0

Innym sposobem jest użycie wiadomości okiennych to jest wyrzucane razem z PInvoke.net

void Main() 
{ 
Process p = Process.Start( 
    @"C:\Program Files (x86)\Internet Explorer\iexplore.exe" , "www.google.com"); 

while(p.MainWindowHandle == IntPtr.Zero) 
{ 
    Thread.Sleep(100); 
    p.Refresh(); 
} 

SetWindowPos(p.MainWindowHandle , IntPtr.Zero , 100 , 100, 800 ,400 , SetWindowPosFlags.ShowWindow); 


} 




// Define other methods and classes here 
[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); 

static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); 
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); 
static readonly IntPtr HWND_TOP = new IntPtr(0); 
static readonly IntPtr HWND_BOTTOM = new IntPtr(1); 

/// <summary> 
/// Window handles (HWND) used for hWndInsertAfter 
/// </summary> 
public static class HWND 
{ 
    public static IntPtr 
    NoTopMost = new IntPtr(-2), 
    TopMost = new IntPtr(-1), 
    Top = new IntPtr(0), 
    Bottom = new IntPtr(1); 
} 

/// <summary> 
/// SetWindowPos Flags 
/// </summary> 
public static class SWP 
{ 
    public static readonly int 
    NOSIZE = 0x0001, 
    NOMOVE = 0x0002, 
    NOZORDER = 0x0004, 
    NOREDRAW = 0x0008, 
    NOACTIVATE = 0x0010, 
    DRAWFRAME = 0x0020, 
    FRAMECHANGED = 0x0020, 
    SHOWWINDOW = 0x0040, 
    HIDEWINDOW = 0x0080, 
    NOCOPYBITS = 0x0100, 
    NOOWNERZORDER = 0x0200, 
    NOREPOSITION = 0x0200, 
    NOSENDCHANGING = 0x0400, 
    DEFERERASE = 0x2000, 
    ASYNCWINDOWPOS = 0x4000; 
} 

[Flags()] 
private enum SetWindowPosFlags : uint 
{ 
    /// <summary>If the calling thread and the thread that owns the window are attached to different input queues, 
    /// the system posts the request to the thread that owns the window. This prevents the calling thread from 
    /// blocking its execution while other threads process the request.</summary> 
    /// <remarks>SWP_ASYNCWINDOWPOS</remarks> 
    AsynchronousWindowPosition = 0x4000, 
    /// <summary>Prevents generation of the WM_SYNCPAINT message.</summary> 
    /// <remarks>SWP_DEFERERASE</remarks> 
    DeferErase = 0x2000, 
    /// <summary>Draws a frame (defined in the window's class description) around the window.</summary> 
    /// <remarks>SWP_DRAWFRAME</remarks> 
    DrawFrame = 0x0020, 
    /// <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to 
    /// the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE 
    /// is sent only when the window's size is being changed.</summary> 
    /// <remarks>SWP_FRAMECHANGED</remarks> 
    FrameChanged = 0x0020, 
    /// <summary>Hides the window.</summary> 
    /// <remarks>SWP_HIDEWINDOW</remarks> 
    HideWindow = 0x0080, 
    /// <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the 
    /// top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter 
    /// parameter).</summary> 
    /// <remarks>SWP_NOACTIVATE</remarks> 
    DoNotActivate = 0x0010, 
    /// <summary>Discards the entire contents of the client area. If this flag is not specified, the valid 
    /// contents of the client area are saved and copied back into the client area after the window is sized or 
    /// repositioned.</summary> 
    /// <remarks>SWP_NOCOPYBITS</remarks> 
    DoNotCopyBits = 0x0100, 
    /// <summary>Retains the current position (ignores X and Y parameters).</summary> 
    /// <remarks>SWP_NOMOVE</remarks> 
    IgnoreMove = 0x0002, 
    /// <summary>Does not change the owner window's position in the Z order.</summary> 
    /// <remarks>SWP_NOOWNERZORDER</remarks> 
    DoNotChangeOwnerZOrder = 0x0200, 
    /// <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to 
    /// the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent 
    /// window uncovered as a result of the window being moved. When this flag is set, the application must 
    /// explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary> 
    /// <remarks>SWP_NOREDRAW</remarks> 
    DoNotRedraw = 0x0008, 
    /// <summary>Same as the SWP_NOOWNERZORDER flag.</summary> 
    /// <remarks>SWP_NOREPOSITION</remarks> 
    DoNotReposition = 0x0200, 
    /// <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary> 
    /// <remarks>SWP_NOSENDCHANGING</remarks> 
    DoNotSendChangingEvent = 0x0400, 
    /// <summary>Retains the current size (ignores the cx and cy parameters).</summary> 
    /// <remarks>SWP_NOSIZE</remarks> 
    IgnoreResize = 0x0001, 
    /// <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary> 
    /// <remarks>SWP_NOZORDER</remarks> 
    IgnoreZOrder = 0x0004, 
    /// <summary>Displays the window.</summary> 
    /// <remarks>SWP_SHOWWINDOW</remarks> 
    ShowWindow = 0x0040, 
} 
0

Można to zrobić bez odniesienia do SHDocVw, korzystając bezpośrednio z COM-Object:

const int FormWidth = 800; 
const int FormHeight = 600; 


// http://www.c-sharpcorner.com/forums/thread/51998/opening-a-browser-and-hiding-the-address-bar-help.aspx 
// http://weblog.west-wind.com/posts/2005/Apr/29/Previewing-HTML-with-InternetExplorerApplication-in-C 
// http://social.msdn.microsoft.com/Forums/vstudio/en-US/ab6969c7-0a34-4d88-9a74-b66888d3d88f/ie-automation-navigating-the-soup 

// http://superuser.com/questions/459775/how-can-i-launch-a-browser-with-no-window-frame-or-tabs-address-bar 
public void OpenBrowserWindow(string strURL) 
{ 
    // System.Diagnostics.Process.Start(strURL) 
    // For Internet Explorer you can use -k (kiosk mode): 
    // iexplore.exe -k http://www.google.com/ 
    // Internet Explorer Command-Line Options 
    // http://msdn.microsoft.com/en-us/library/ie/hh826025(v=vs.85).aspx 
    // Starts Internet Explorer in kiosk mode. The browser opens in a maximized window that does not display the address bar, 
    // the navigation buttons, or the status bar. 

    System.Drawing.Rectangle rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds; 

    // http://stackoverflow.com/questions/5049122/capture-the-screen-shot-using-net 
    int posX = rect.Width - FormWidth; 
    int posY = rect.Height - FormHeight; 
    posX = Convert.ToInt32 (posX/2.0); 
    posY = Convert.ToInt32 (posY/2.0); 

    posX = Math.Max (posX, 0); 
    posY = Math.Max (posY, 0); 



    System.Type oType = System.Type.GetTypeFromProgID ("InternetExplorer.Application"); 

    object o = System.Activator.CreateInstance (oType); 
    o.GetType().InvokeMember ("MenuBar", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { 0 }); 
    o.GetType().InvokeMember ("ToolBar", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { 0 }); 
    o.GetType().InvokeMember ("StatusBar", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { 0 }); 
    o.GetType().InvokeMember ("AddressBar", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { 0 }); 
    o.GetType().InvokeMember ("Visible", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { true }); 


    o.GetType().InvokeMember ("Top", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { posY }); 
    o.GetType().InvokeMember ("Left", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { posX }); 
    o.GetType().InvokeMember ("Width", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { FormWidth }); 
    o.GetType().InvokeMember ("Height", System.Reflection.BindingFlags.SetProperty, null, o, new object[] { FormHeight }); 

    o.GetType().InvokeMember ("Navigate", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] { strURL }); 

    try 
    { 
     object ohwnd = o.GetType().InvokeMember ("hwnd", System.Reflection.BindingFlags.GetProperty, null, o, null); 
     System.IntPtr IEHwnd = (System.IntPtr)ohwnd; 
     // NativeMethods.SetForegroundWindow (IEHwnd); 
     NativeMethods.ShowWindow(IEHwnd, NativeMethods.WindowShowStyle.ShowMaximized); 
    } catch (Exception ex) { 
    } 

} // OpenBrowserWindow 



public class NativeMethods 
{ 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    [return: System.Runtime.InteropServices.MarshalAs(
     System.Runtime.InteropServices.UnmanagedType.Bool)] 
    public static extern bool SetForegroundWindow(IntPtr hWnd); 

    [System.Runtime.InteropServices.DllImport ("user32.dll")] 
    [return: System.Runtime.InteropServices.MarshalAs(
     System.Runtime.InteropServices.UnmanagedType.Bool)] 
    public static extern bool ShowWindow(IntPtr hwnd, WindowShowStyle nCmdShow); 


    /// <summary>Enumeration of the different ways of showing a window using 
    /// ShowWindow</summary> 
    public enum WindowShowStyle : int 
    { 
     /// <summary>Hides the window and activates another window.</summary> 
     /// <remarks>See SW_HIDE</remarks> 
     Hide = 0, 
     /// <summary>Activates and displays a window. If the window is minimized 
     /// or maximized, the system restores it to its original size and 
     /// position. An application should specify this flag when displaying 
     /// the window for the first time.</summary> 
     /// <remarks>See SW_SHOWNORMAL</remarks> 
     ShowNormal = 1, 
     /// <summary>Activates the window and displays it as a minimized window.</summary> 
     /// <remarks>See SW_SHOWMINIMIZED</remarks> 
     ShowMinimized = 2, 
     /// <summary>Activates the window and displays it as a maximized window.</summary> 
     /// <remarks>See SW_SHOWMAXIMIZED</remarks> 
     ShowMaximized = 3, 
     /// <summary>Maximizes the specified window.</summary> 
     /// <remarks>See SW_MAXIMIZE</remarks> 
     Maximize = 3, 
     /// <summary>Displays a window in its most recent size and position. 
     /// This value is similar to "ShowNormal", except the window is not 
     /// actived.</summary> 
     /// <remarks>See SW_SHOWNOACTIVATE</remarks> 
     ShowNormalNoActivate = 4, 
     /// <summary>Activates the window and displays it in its current size 
     /// and position.</summary> 
     /// <remarks>See SW_SHOW</remarks> 
     Show = 5, 
     /// <summary>Minimizes the specified window and activates the next 
     /// top-level window in the Z order.</summary> 
     /// <remarks>See SW_MINIMIZE</remarks> 
     Minimize = 6, 
     /// <summary>Displays the window as a minimized window. This value is 
     /// similar to "ShowMinimized", except the window is not activated.</summary> 
     /// <remarks>See SW_SHOWMINNOACTIVE</remarks> 
     ShowMinNoActivate = 7, 
     /// <summary>Displays the window in its current size and position. This 
     /// value is similar to "Show", except the window is not activated.</summary> 
     /// <remarks>See SW_SHOWNA</remarks> 
     ShowNoActivate = 8, 
     /// <summary>Activates and displays the window. If the window is 
     /// minimized or maximized, the system restores it to its original size 
     /// and position. An application should specify this flag when restoring 
     /// a minimized window.</summary> 
     /// <remarks>See SW_RESTORE</remarks> 
     Restore = 9, 
     /// <summary>Sets the show state based on the SW_ value specified in the 
     /// STARTUPINFO structure passed to the CreateProcess function by the 
     /// program that started the application.</summary> 
     /// <remarks>See SW_SHOWDEFAULT</remarks> 
     ShowDefault = 10, 
     /// <summary>Windows 2000/XP: Minimizes a window, even if the thread 
     /// that owns the window is hung. This flag should only be used when 
     /// minimizing windows from a different thread.</summary> 
     /// <remarks>See SW_FORCEMINIMIZE</remarks> 
     ForceMinimized = 11 
    } 

} 

można również użyć VirtualScreen

System.Windows.Forms.SystemInformation.VirtualScreen.Width; 
System.Windows.Forms.SystemInformation.VirtualScreen.Height; 

zamiast PrimaryScreen

Powiązane problemy