我正在尝试最小化窗口,标题位于string p
(由用户在运行时指定)。窗口保证是主窗口,因为用户只能从主窗口中选择。我已经尝试过showCmd,flags和两者的混合,但每次,无论窗口是否最小化,我都会看到指出"Minimizing"
的对话框。我该如何解决?
private void button1_Click(object sender, EventArgs e)
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains(p))
{
IntPtr handle = proc.Handle;
Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
Program.GetWindowPlacement(handle, ref wp);
if ((wp.showCmd & 2) == 2)
{
wp.showCmd = 9;
MessageBox.Show("Restoring");
}
else
{
wp.showCmd = 2;
MessageBox.Show("Minimizing");
}
wp.length = Marshal.SizeOf(wp);
Program.SetWindowPlacement(handle, ref wp);
break;
}
}
}
我用作p -
string p;
PictureBox i;
bool windowShowing = false;
bool minimized = false;
public TaskbarItem(string window)
{
InitializeComponent();
p = window;
button1.Text = window;
}
要获取窗口:
class WindowEnumerator
{
public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
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;
}
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
public static LinkedList<IntPtr> EnumMethod2(bool includeChild)
{
LinkedList<IntPtr> pss = new LinkedList<IntPtr>();
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
string s = p.MainWindowTitle;
if (s.Equals("") || s.Equals("Mod_Taskbar"))
{
continue;
}
LinkedListNode<IntPtr> node = new LinkedListNode<IntPtr>(p.MainWindowHandle);
pss.AddLast(node);
if (includeChild){
List<IntPtr> l = GetChildWindows(p.MainWindowHandle);
foreach (IntPtr i in l)
{
StringBuilder stb = new StringBuilder(255);
WindowEnumerator.GetWindowText(i, stb, 255);
if (stb.ToString().StartsWith("Address:") || stb.ToString().EndsWith("View") || stb.ToString().EndsWith("Control") ||
stb.ToString().EndsWith("Bar") || stb.ToString().EndsWith("bar") || stb.ToString().Contains("Host"))
{
continue;
}
if (Vars.excludes.Contains(stb.ToString()))
{
continue;
}
pss.AddAfter(node, i);
}
}
}
return pss;
}
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
list.Add(handle);
return true;
}
}
和
private void LoadWindows(object sender, EventArgs e)
{
panel1.Controls.Clear();
foreach (IntPtr p in WindowEnumerator.EnumMethod2(false))
{
StringBuilder stb = new StringBuilder(255);
WindowEnumerator.GetWindowText(p, stb, 255);
TaskbarItem t = new TaskbarItem(stb.ToString());
t.Dock = DockStyle.Left;
panel1.Controls.Add(t);
}
}
答案 0 :(得分:4)
您是否检查过wp.showCmd的值是多少?
尝试使用此代码:
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains(p))
{
IntPtr handle = proc.Handle;
Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
Program.GetWindowPlacement(handle, ref wp);
if ((wp.showCmd & 2) == 2)
{
wp.showCmd = 9;
MessageBox.Show("Restoring");
}
else
{
wp.showCmd = 2;
MessageBox.Show("Minimizing");
}
wp.length = Marshal.SizeOf(wp);
Program.SetWindowPlacement(handle, ref wp);
break;
}
}
答案 1 :(得分:0)
this.WindowState = FormWindowState.Minimized;