I have multi-part RAR files in the same folder and I want to check if all parts of the RAR exist. I am using SharpCompress v0.24, here is my code:
using (var archive = RarArchive.Open("D:/Ontology tool.part1.rar"))
{
foreach (RarVolume vol in archive.Volumes)
{
MessageBox.Show(vol.IsMultiVolume + " \n"+ vol.IsFirstVolume+"\n"+ vol.IsSolidArchive);
}
}
But I cannot get the volume full file name.
I then use SevenZipSharp v 0.64 and 7z.dll version 19, here is my code:
using (var extractor = new SevenZipExtractor("D:/Ontology tool.part1.rar"))
{
MessageBox.Show(extractor.VolumeFileNames[0]+"");
}
But then I get the error:
Invalid archive open/read error! Is it encrypted and a wrong
password was provided? If your archive is an exotic one, it is
possible that SevenZipSharp has no signature for its format and thus
decided it is TAR by mistake
Note that the WinRAR program seems it make changes of RAR file formats because I create a RAR file with version 5.71 and when I try to open it with old WinRAR version it not opened ok, I mean files are not in the correct format.
The same applies to SevenZipSharp. If I open an old RAR file I created since 2014 it opens ok, but when I open a RAR file created with WinRAR v 5.71 it raises this error.
So now how can I get all parts files names of multi part RAR file?
Thanks for your help.
I end up using winrar.exe , send command to test file like this:
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
private enum WindowShowStyle : uint
{
/// <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
}
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);
const int SW_RESTORE = 9;
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr Handle, int x, int y, int w, int h, bool repaint);
static readonly int GWL_STYLE = -16;
static readonly int WS_VISIBLE = 0x10000000;
....
private string GetMissingCompressedFile(string FilePath, string WinRARPath)
{
string MissingCompressedFile = "";
Process p = new Process();
p.StartInfo.FileName = WinRARPath;
p.EnableRaisingEvents = true;
// -ibck -inul
p.StartInfo.Arguments = " t " + " \"" + FilePath + "\"";
//p.StartInfo.UseShellExecute = false;
// p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardError = true;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
IntPtr PanelHandler=new IntPtr();
WinRARPanel_Ref.Invoke((MethodInvoker)delegate
{
PanelHandler = WinRARPanel_Ref.Handle;
});
p.Start();
IntPtr h = p.MainWindowHandle;
while (true)
{
if (p.HasExited)
break;
if (p.MainWindowHandle != IntPtr.Zero)
break;
Thread.Sleep(100); // Don't hog the CPU
p.Refresh(); // You need this since `MainWindowHandle` is cached
}
if (!p.HasExited)
{
SetParent(p.MainWindowHandle, PanelHandler);
//SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
//MoveWindow(p.MainWindowHandle, 0, 0, WinRARPanel_Ref.Width, WinRARPanel_Ref.Height, true);
}
while (!p.HasExited)
{
var _windowHandle = FindWindow(null, "Next volume is required");
var _parent = GetParent(_windowHandle);
if (_parent == p.MainWindowHandle)
{
if (!p.HasExited)
{
SetParent(_windowHandle, WinRARPanel_Ref.Handle);
}
Thread.Sleep(1000);
if (!p.HasExited)
{
MainForm_Ref.Invoke((MethodInvoker)delegate
{
Clipboard.Clear();
ShowWindow(h, SW_RESTORE);
SetForegroundWindow(h);
SendKeys.SendWait("^(c)");
string ClipboardText = Clipboard.GetText();
if (!string.IsNullOrEmpty(ClipboardText))
{
try
{
string n = new FileInfo(ClipboardText).Name;
MissingCompressedFile = ClipboardText;
if (!p.HasExited)
{
p.Kill();
}
}
catch
{
if (ClipboardText.Contains("You need to start extraction from a previous volume to unpack"))
{
MissingCompressedFile = "";
p.Kill();
}
}
Clipboard.Clear();
}
});
}
}
else
{
if (!p.HasExited)
{
MainForm_Ref.Invoke((MethodInvoker)delegate
{
Clipboard.Clear();
if (!p.HasExited)
{
ShowWindow(h, SW_RESTORE);
SetForegroundWindow(h);
}
SendKeys.SendWait("^(c)");
string ClipboardText = Clipboard.GetText();
if (!string.IsNullOrEmpty(ClipboardText))
{
try
{
string n = new FileInfo(ClipboardText).Name;
MissingCompressedFile = ClipboardText;
if (!p.HasExited)
{
p.Kill();
}
}
catch
{
if (ClipboardText.Contains("You need to start extraction from a previous volume to unpack"))
{
MissingCompressedFile = "";
p.Kill();
}
}
Clipboard.Clear();
}
});
}
}
}
if (!p.HasExited)
{
p.Kill();
}
p = null;
return MissingCompressedFile;
}
Related
I'm doing a mobile application, all is almost as WindowsForms but when I close a MessageBox that provoque the multiple dialogs close with DialogResult in DialogResult.OK I find a problem, the main form it hides.
The structure is as follows:
program.cs
Application.Run(new From1());
Form1
Dialog1 frmDialog1 = new Dialog1());
frmDialog1.ShowDialog(this);
Dialog1(Windows Parent)
Dialog2 frmDialog2 = new Dialog2());
frmDialog2.ShowDialog(this);
if(frmDialog2.DialogResult == DialogResult.OK)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
Dialog2(Windows Parent)
Dialog3 frmDialog3 = new Dialog3());
frmDialog3.ShowDialog(this);
if(frmDialog3.DialogResult == DialogResult.OK)
{
this.DialogResult = DialogResult.OK;
this.ParentForm.Hide();
this.Close();
}
Dialog3(Windows Parent)
if (MessageBox.Show("Do you want to save?", "Save?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
this.DialogResult = DialogResult.OK;
this.ParentForm.Hide();
this.Close();
}
**Note: I'm using [DllImport("coredll.dll")] to show full screen
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[Flags()]
public enum FullScreenFlags : int
{
SwHide = 0,
ShowTaskbar = 0x1,
HideTaskbar = 0x2,
ShowSipButton = 0x4,
HideSipButton = 0x8,
SwRestore = 9,
ShowStartIcon = 0x10,
HideStartIcon = 0x20
}
static class FullScreen
{
#region Win32 API Calls
/// <summary>
/// The GetCapture function retrieves a handle to the window
/// </summary>
/// <returns></returns>
[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();
/// <summary>
/// The SetCapture function sets the mouse capture to
/// the specified window belonging to the current thread
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("coredll.dll")]
private static extern IntPtr SetCapture(IntPtr hWnd);
/// <summary>
/// This function can be used to take over certain areas of the screen
/// It is used to modify the taskbar, Input Panel button,
/// or Start menu icon.
/// </summary>
/// <param name="hwnd"></param>
/// <param name="state"></param>
/// <returns></returns>
[DllImport("aygshell.dll", SetLastError = true)]
private static extern bool SHFullScreen(IntPtr hwnd, int state);
/// <summary>
/// The function retrieves the handle to the top-level
/// window whose class name and window name match
/// the specified strings. This function does not search child windows.
/// </summary>
/// <param name="lpClass"></param>
/// <param name="lpWindow"></param>
/// <returns></returns>
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr FindWindowW(string lpClass, string
lpWindow);
/// <summary>
/// changes the position and dimensions of the specified window
/// </summary>
/// <param name="hwnd"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="l"></param>
/// <param name="repaint"></param>
/// <returns></returns>
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr MoveWindow(IntPtr hwnd, int x, int y, int
w, int l, int repaint);
#endregion
#region General Methods
/// <summary>
/// obtain the window handle of a .Net window or control
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static IntPtr GetHWnd(Control c)
{
IntPtr hOldWnd = GetCapture();
c.Capture = true;
IntPtr hWnd = GetCapture();
c.Capture = false;
SetCapture(hOldWnd);
return hWnd;
}
/// <summary>
/// Set Full Screen Mode
/// </summary>
/// <param name="form"></param>
public static void StartFullScreen(Form form)
{
//if not Pocket Pc platform
if (!Platform.PlatformDetection.IsPocketPC())
{
//Set Full Screen For Windows CE Device
//Normalize windows state
form.WindowState = FormWindowState.Normal;
IntPtr iptr = form.Handle;
SHFullScreen(iptr, (int)FullScreenFlags.HideStartIcon);
//detect taskbar height
int taskbarHeight = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
// move the viewing window north taskbar height to get rid of the command
//bar
MoveWindow(iptr, 0, -taskbarHeight, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height + taskbarHeight, 1);
// move the task bar south taskbar height so that its not visible anylonger
IntPtr iptrTB = FindWindowW("HHTaskBar", null);
MoveWindow(iptrTB, 0, Screen.PrimaryScreen.Bounds.Height, Screen.PrimaryScreen.Bounds.Width, taskbarHeight, 1);
}
else //pocket pc platform
{
//Set Full Screen For Pocket Pc Device
form.Menu = null;
form.ControlBox = false;
form.FormBorderStyle = FormBorderStyle.None;
form.WindowState = FormWindowState.Maximized;
form.Text = string.Empty;
}
}
/// <summary>
/// Stop Full Screen Mode
/// </summary>
/// <param name="form"></param>
public static void StopFullScreen(Form form)
{
//if windows ce return window and taskbar to his original place
if (!Platform.PlatformDetection.IsPocketPC())
{
IntPtr iptr = form.Handle;
SHFullScreen(iptr, (int)FullScreenFlags.ShowStartIcon);
//detect taskbar height
int taskbarHeight = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
MoveWindow(iptr, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height - taskbarHeight, 1);
IntPtr iptrTB = FindWindowW("HHTaskBar", null);
MoveWindow(iptrTB, 0, Screen.PrimaryScreen.Bounds.Height - taskbarHeight, Screen.PrimaryScreen.Bounds.Width, taskbarHeight, 1);
}
}
#endregion
}
**Other comment:
I´m calling this.ParentFormHide(); because I desire that the parent it is hide before then close it. This for prevent then it show wrong, as like the follows:
This is shown when is closing multiple dialogs in cascade
If you know a way to prevent this animation on closing multiple dialogs, please share to me. At present to prevent this, I'm hiding the previous dialog before to close it.
Thanks in advance
When I tried from with transparent background, it's not completely transparent.
I tried two code blocks for this issue. First i tried like this code:
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
it look like this picture;
Then i found some different codes and tried ike this;
public Form1()
{
InitializeComponent();
this.TransparencyKey = Color.White;
this.BackColor = Color.White;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
And this looks like this picture;
You can see logo with a white border. I want to show only .png Logo completely transparent. What should i do? How can do this?
Here is my Logo image as .png;
You can use Layered Windows:
Using a layered window can significantly improve performance and
visual effects for a window that has a complex shape, animates its
shape, or wishes to use alpha blending effects. The system
automatically composes and repaints layered windows and the windows of
underlying applications. As a result, layered windows are rendered
smoothly, without the flickering typical of complex window regions. In
addition, layered windows can be partially translucent, that is,
alpha-blended.
Create layered window in Windows Forms
Here is some code from msdn code gallery which demonstrates creating Layered Windows in Windows Forms. It allows you to create a shaped splash screen and let you to move it by mouse.
Add PerPixelAlphaForm to the project and then it's enough to inherit from this form and call its SelectBitmap and pass your png to the method to create a layered window.
PerPixelAlphaForm.cs
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
#endregion
namespace CSWinFormLayeredWindow
{
public partial class PerPixelAlphaForm : Form
{
public PerPixelAlphaForm()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Load += PerPixelAlphaForm_Load;
}
void PerPixelAlphaForm_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
protected override CreateParams CreateParams
{
get
{
// Add the layered extended style (WS_EX_LAYERED) to this window.
CreateParams createParams = base.CreateParams;
if(!DesignMode)
createParams.ExStyle |= WS_EX_LAYERED;
return createParams;
}
}
/// <summary>
/// Let Windows drag this window for us (thinks its hitting the title
/// bar of the window)
/// </summary>
/// <param name="message"></param>
protected override void WndProc(ref Message message)
{
if (message.Msg == WM_NCHITTEST)
{
// Tell Windows that the user is on the title bar (caption)
message.Result = (IntPtr)HTCAPTION;
}
else
{
base.WndProc(ref message);
}
}
/// <summary>
///
/// </summary>
/// <param name="bitmap"></param>
public void SelectBitmap(Bitmap bitmap)
{
SelectBitmap(bitmap, 255);
}
/// <summary>
///
/// </summary>
/// <param name="bitmap">
///
/// </param>
/// <param name="opacity">
/// Specifies an alpha transparency value to be used on the entire source
/// bitmap. The SourceConstantAlpha value is combined with any per-pixel
/// alpha values in the source bitmap. The value ranges from 0 to 255. If
/// you set SourceConstantAlpha to 0, it is assumed that your image is
/// transparent. When you only want to use per-pixel alpha values, set
/// the SourceConstantAlpha value to 255 (opaque).
/// </param>
public void SelectBitmap(Bitmap bitmap, int opacity)
{
// Does this bitmap contain an alpha channel?
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
{
throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
}
// Get device contexts
IntPtr screenDc = GetDC(IntPtr.Zero);
IntPtr memDc = CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr hOldBitmap = IntPtr.Zero;
try
{
// Get handle to the new bitmap and select it into the current
// device context.
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
hOldBitmap = SelectObject(memDc, hBitmap);
// Set parameters for layered window update.
Size newSize = new Size(bitmap.Width, bitmap.Height);
Point sourceLocation = new Point(0, 0);
Point newLocation = new Point(this.Left, this.Top);
BLENDFUNCTION blend = new BLENDFUNCTION();
blend.BlendOp = AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = (byte)opacity;
blend.AlphaFormat = AC_SRC_ALPHA;
// Update the window.
UpdateLayeredWindow(
this.Handle, // Handle to the layered window
screenDc, // Handle to the screen DC
ref newLocation, // New screen position of the layered window
ref newSize, // New size of the layered window
memDc, // Handle to the layered window surface DC
ref sourceLocation, // Location of the layer in the DC
0, // Color key of the layered window
ref blend, // Transparency of the layered window
ULW_ALPHA // Use blend as the blend function
);
}
finally
{
// Release device context.
ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
SelectObject(memDc, hOldBitmap);
DeleteObject(hBitmap);
}
DeleteDC(memDc);
}
}
#region Native Methods and Structures
const Int32 WS_EX_LAYERED = 0x80000;
const Int32 HTCAPTION = 0x02;
const Int32 WM_NCHITTEST = 0x84;
const Int32 ULW_ALPHA = 0x02;
const byte AC_SRC_OVER = 0x00;
const byte AC_SRC_ALPHA = 0x01;
[StructLayout(LayoutKind.Sequential)]
struct Point
{
public Int32 x;
public Int32 y;
public Point(Int32 x, Int32 y)
{ this.x = x; this.y = y; }
}
[StructLayout(LayoutKind.Sequential)]
struct Size
{
public Int32 cx;
public Int32 cy;
public Size(Int32 cx, Int32 cy)
{ this.cx = cx; this.cy = cy; }
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ARGB
{
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc,
Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteObject(IntPtr hObject);
#endregion
}
}
SplashScreen.cs
public partial class Form4 : CSWinFormLayeredWindow.PerPixelAlphaForm
{
public Form4()
{
InitializeComponent();
this.SelectBitmap(Properties.Resources.splash);
}
}
Note
The original answer was based on turning double buffer off and overriding OnPaintBackground and drawing the image without calling base method. The answer had a known issue; while the form was moveless it was working well but if form was moving or the window behind the form was changed the window was not updating. You can see previous code in revisions. The current edit which is completely based on an MSDN code doesn't have any known issue.
You can choose the level of transparency for an image via changing the alpha coefficient which is between 0 and 255. An image can be a backgrounf image too,no prob
private static Image ToGrayscale(Image s,int alpha)
{
Bitmap tImage = new Bitmap(s);
for (int x = 0; x < tImage.Width; x++)
{
for (int y = 0; y < tImage.Height; y++)
{
Color tCol = tImage.GetPixel(x, y);
Color newColor = Color.FromArgb(alpha, tCol.R, tCol.G, tCol.B);
tImage.SetPixel(x, y, newColor);
}
}
return tImage;
}
I'm opening a webpage in IE using c#
Code:
Process.Start("IExplore.exe", "www.northwindtraders.com");
But, how can I resize the page with the following characteritics: toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width='622', height='582'
For that level of fine grained control which I do not believe is available from the command line add a reference (COM) to Microsoft Internet Controls and then you can:
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);
These are the features for opening Internet Explorer from Command Shell:
http://msdn.microsoft.com/en-us/library/hh826025(v=vs.85).aspx
You need to use the StartInfo property to set some startup parameters.
Check this link for what options are available.
You can do it without a reference to SHDocVw, by using the COM-object directly:
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
}
}
You can also use VirtualScreen
System.Windows.Forms.SystemInformation.VirtualScreen.Width;
System.Windows.Forms.SystemInformation.VirtualScreen.Height;
instead of PrimaryScreen
Another way is to use window messages this is thrown together from 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,
}
I am trying to move winamps main window, with this code:
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
static void resize()
{
Process w = new Process();
w.StartInfo = new ProcessStartInfo("winamp.exe");
w.Start();
Thread.Sleep(5000);
IntPtr hWnd = GetForegroundWindow();
RECT rect;
GetWindowRect(hWnd, out rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
MoveWindow(hWnd, 0, 0, width, height, true);
}
This code snippet works with all processes that I tested, except Winamp. When I use the mainWindowHandle of the process it moves another window.
Anyone an idea how to get it working with Winamp?
With the following code I can confirm changing WinAmp's main window size does not work via the Win32 API's.
Changing other winamp window sizes via the Win32 API's did work, but is not a solution:
public partial class Form1 : Form
{
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
private System.IntPtr hWnd;
private void button1_Click(object sender, EventArgs e)
{
Process p = Process.Start(#"C:\Program Files\Winamp\winamp.exe");
try
{
do
{
p.Refresh();
}
while (p.MainWindowHandle.ToInt32() == 0);
hWnd = new IntPtr(p.MainWindowHandle.ToInt32());
}
catch (Exception ex)
{
//Do some stuff...
throw;
}
}
private void button2_Click(object sender, EventArgs e)
{
//Make sure we have a handle to the shelled exe
if (hWnd == new IntPtr(0)) return;
ResizeExternalExeChildWindows(hWnd);
}
private void ResizeExternalExeChildWindows(IntPtr parent)
{
List<IntPtr> childWindows = GetChildWindows(hWnd);
foreach (IntPtr childWindow in childWindows)
{
RECT rect;
GetWindowRect(childWindow, out rect);
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
MoveWindow(hWnd, 0, 0, width, height, true);
}
}
// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
// You can modify this to check to see if you want to cancel the operation, then return a null here
return true;
}
/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
}
}
Cause
Because WinAmp is skinnable it supports resizing via config files (not via external apps using win32 API's).
Solution
Open the file C:\Users[username]\AppData\Roaming\Winamp\studio.xnf and edit the following values:
<entry name="Bento_nomax_h" value="492" />
<entry name="Bento_nomax_w" value="633" />
<entry name="Bento_nomax_x" value="27" />
<entry name="Bento_nomax_y" value="16" />
I am using the Bento Skin. If you open the WinAmp.ini file you can detect the skin being used:
skin=Bento
The solution to set the initial size for WinAmp's main window is to edit the config file before shelling WinAmp with Process.Start.
I have tried your code, it is not working always, and it is not re-sizing windows, it was moving it only,
Actually I found a problem in the 2000 ms sleep, which you can change to a while loop checking if the handle is zero then proceed with the code,
while (p.MainWindowHandle == 0)
{
}
IntPtr hWnd = p.MainWindowHandle;
this may help if the winamp takes too much time to build its own window
This worked for me using the latest version, please give it a try:
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[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);
public static class HWND
{
public static IntPtr
NoTopMost = new IntPtr(-2),
TopMost = new IntPtr(-1),
Top = new IntPtr(0),
Bottom = new IntPtr(1);
}
[Flags]
public enum SetWindowPosFlags : uint
{
SWP_ASYNCWINDOWPOS = 0x4000,
SWP_DEFERERASE = 0x2000,
SWP_DRAWFRAME = 0x0020,
SWP_FRAMECHANGED = 0x0020,
SWP_HIDEWINDOW = 0x0080,
SWP_NOACTIVATE = 0x0010,
SWP_NOCOPYBITS = 0x0100,
SWP_NOMOVE = 0x0002,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOREDRAW = 0x0008,
SWP_NOREPOSITION = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_NOSIZE = 0x0001,
SWP_NOZORDER = 0x0004,
SWP_SHOWWINDOW = 0x0040,
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
static void Resize()
{
IntPtr winampMainWindow = IntPtr.Zero;
while (true)
{
winampMainWindow = FindWindow("BaseWindow_RootWnd", "Main Window");
if (winampMainWindow != IntPtr.Zero)
{
RECT rect = new RECT();
GetWindowRect(winampMainWindow, ref rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
SetWindowPos(winampMainWindow,
HWND.Top,
0,
0,
width,
height,
SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOSENDCHANGING | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOCOPYBITS);
break;
}
Thread.Sleep(1000);
}
}
[STAThread]
static void Main()
{
Process.Start("winamp.exe");
Resize();
}
Absolute nightmare!
However, right clicking in the "song window" DID open up the docking menu - I think the undock-(something) worked. One of these options allowed me to move the god-damned thing. It was a fluke, but something opened up there that worked.
How to get the client size of a Form when it's maximized without maximize it?
From example, I want to create a Bitmap with the same size of the maximized Form's client size, how can I do that?
Try
Screen.FromControl(this).GetWorkingArea();
to calculate the size (without taskbar)
then substract the difference between the Forms ClientSize / Size.
Hope that works, haven't tested it.
Update:
A bit hacky, but I tried it and it works.
var frm = new Form();
frm.Opacity = 100;
frm.WindowState = FormWindowState.Maximized;
frm.Show();
while (!frm.IsHandleCreated)
System.Threading.Thread.Sleep(1);
var result = frm.ClientSize;
frm.Close();
return result;
Update2:
This is a nicer solution.
I disable painting of the Form, maximize it, get the client area, set it back to normal and return the result. Works well, without flicker or something.
private static Size GetMaximizedClientSize(Form form)
{
var original = form.WindowState;
try
{
BeginUpdate(form);
form.WindowState = FormWindowState.Maximized;
return form.ClientSize;
}
finally
{
form.WindowState = original;
EndUpdate(form);
}
}
[DllImport("User32.dll")]
private extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private enum Message : int
{
WM_SETREDRAW = 0x000B, // int 11
}
/// <summary>
/// Calls user32.dll SendMessage(handle, WM_SETREDRAW, 0, null) native function to disable painting
/// </summary>
/// <param name="c"></param>
public static void BeginUpdate(Control c)
{
SendMessage(c.Handle, (int)Message.WM_SETREDRAW, new IntPtr(0), IntPtr.Zero);
}
/// <summary>
/// Calls user32.dll SendMessage(handle, WM_SETREDRAW, 1, null) native function to enable painting
/// </summary>
/// <param name="c"></param>
public static void EndUpdate(Control c)
{
SendMessage(c.Handle, (int)Message.WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);
}
Try the following link,
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.workingarea%28VS.71%29.aspx