C# 如何最小化/最大化打开的应用程序
我有一份公开申请表。为了得到这个列表,我使用了以下代码C# 如何最小化/最大化打开的应用程序,c#,C#,我有一份公开申请表。为了得到这个列表,我使用了以下代码 internal static class NativeMethods { public static readonly Int32 GWL_STYLE = -16; public static readonly UInt64 WS_VISIBLE = 0x10000000L; public static readonly UInt64 WS_BORDER = 0x00800000L; public sta
internal static class NativeMethods
{
public static readonly Int32 GWL_STYLE = -16;
public static readonly UInt64 WS_VISIBLE = 0x10000000L;
public static readonly UInt64 WS_BORDER = 0x00800000L;
public static readonly UInt64 DESIRED_WS = WS_BORDER | WS_VISIBLE;
public delegate Boolean EnumWindowsCallback(IntPtr hwnd, Int32 lParam);
public static List<WindowWrapper> GetAllWindows()
{
List<WindowWrapper> windows = new List<WindowWrapper>();
StringBuilder buffer = new StringBuilder(100);
EnumWindows(delegate(IntPtr hwnd, Int32 lParam)
{
if ((GetWindowLongA(hwnd, GWL_STYLE) & DESIRED_WS) == DESIRED_WS)
{
GetWindowText(hwnd, buffer, buffer.Capacity);
WindowWrapper wnd = new WindowWrapper();
wnd.handle = hwnd;
wnd.title = buffer.ToString();
windows.Add(wnd);
}
return true;
}, 0);
return windows;
}
[DllImport("user32.dll")]
static extern Int32 EnumWindows(EnumWindowsCallback lpEnumFunc, Int32 lParam);
[DllImport("user32.dll")]
public static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);
[DllImport("user32.dll")]
static extern UInt64 GetWindowLongA(IntPtr hWnd, Int32 nIndex);
}
public class WindowWrapper : IWin32Window
{
internal IntPtr handle;
internal String title;
public IntPtr Handle
{
get { return handle; }
}
public String Title
{
get { return title; }
}
}
foreach (var wnd in NativeMethods.GetAllWindows())
{
string caption = wnd.title;
string handle = wnd.Handle
// Add this caption and handle to list
}
现在,用户将从列表中选择任何打开的窗口,我的任务是读取所选窗口的标题,获取进程句柄,最大化/最小化或关闭窗口。如何执行此操作。对于ncmdShow,您可以使用本机方法ShowWindow和SW_最大化、SW_最小化 看看
您可以使用ShowWindowAsync
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
ShowWindowAsync(wnd.Handle, SW_SHOWMINIMIZED );
而且它更好用
var openWindows = Process.GetProcesses().Where(process=> String.IsNullOrEmpty(process.MainWindowTitle)==false);
打开窗户
我在过程中测试了MainWindowTitle,它有助于在给定标题的窗口上搜索
var handles = Process.GetProcesses().Where(x => x.MainWindowTitle == "Untitled - Notepad").Select(y=>y.Handle).ToList();
您可以使用
findwindowbycaption
获取句柄,然后使用showwindow
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
// more here: http://www.pinvoke.net/default.aspx/user32.showwindow
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
然后在代码中使用以下内容:
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "The window title");
ShowWindow(hwnd, SW_MAXIMIZE);
虽然您似乎已经通过使用
EnumWindows
获得了窗口句柄,但在这种情况下,您只需要:
ShowWindow(windows[i].handle, SW_MAXIMIZE);
i
是窗口的索引
要关闭窗口,您将使用:
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
在守则中:
DestroyWindow(hwnd) //or DestroyWindow(windows[i].handle)
这是非托管版本的system.windows.forms.form.close()
或者您可以使用:
Process [] proc Process.GetProcessesByName("process name");
proc[0].Kill();
static uint WM_CLOSE = 0x0010;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
或者您可以使用:
Process [] proc Process.GetProcessesByName("process name");
proc[0].Kill();
static uint WM_CLOSE = 0x0010;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
代码:
PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
你能告诉我怎么用吗?你的答案是对的。如果我们只知道它的标题,我们能得到句柄或窗口吗你的WindowWrapper中有句柄,存储它们会更有效,因为窗口标题可以由后台运行的应用程序本身更改。实际上,我正在创建一个应用程序,用户将存储他/她希望在指定时间最大化/最小化/关闭的窗口标题,直到该窗口打开。我的代码将从数据库中读取标题。同样的事情可能会发生在另一台打开相同窗口的机器上。现在,另一台机器的句柄代码与本地机器的句柄代码不同,所以我需要通过它的标题来获取进程的句柄代码,有一个Windows API函数FindWindow可以做到这一点,只需将ClassName设置为NULL并使用WindowsName的标题,如果我们只知道它的标题,您知道获取窗口句柄的代码吗?这是唯一适用于我的WPF应用程序的方法,谢谢!DestroyWindow/CloseWindow不关闭窗口,为什么?如果窗口句柄正确,它应该关闭窗口。如果窗口句柄等于
IntPtr.Zero
则为无效句柄。如果销毁父窗口,所有子窗口也将关闭。没有句柄不是IntPtr.Zero。最小化和最大化工作正常只有关闭不工作,还有其他方法吗?我尝试了PostMessage和PostQuitMessage两种方法,但没有关闭,而是恢复了。正如Tyler所说,我在您提供的链接上得到了答案,感谢您提供了大量可能的重复