我已经使用了一个互斥体来运行一个单一的实例程序,现在我希望窗口变得最大化,如果当用户重新打开应用程序时,这个窗口最小化。
static class Program { [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { bool Ok = true; string ProductName = Application.ProductName; Mutex m = new Mutex(true,ProductName,out Ok); if (!Ok) { System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(ProductName); SetForegroundWindow(p[0].MainWindowHandle); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
在Adobe AIR和.NET之间发送“Windows消息”是否有体面的方式,还是需要使用套接字?
编写.NET以外的Windows客户端应用程序的最佳方法是什么?
有没有触摸()的.NET /窗口? 文件被locking时可以使用吗?
在VB.NET中获取CD驱动器号
如何从C#读取PowerShell脚本stdout和stderr
以编程方式更改Internet Explorer设置?
.NET:屏幕保护程序configuration对话框所有者和预览焦点
当ReadOnly = false时,性能计数器显示不同的值
键盘挂钩改变键的行为
我的dotnet应用程序不能在Windows Server 2003上运行
您正在寻找ShowWindow函数和SW_MAXIMIZE标志。
在C#中,P / Invoke声明如下所示:
[DllImport("user32.dll",CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool ShowWindow(IntPtr hWnd,int nCmdshow); private const int SW_MAXIMIZE = 3;
if (!Ok) { Process[] p = Process.GetProcessesByName(ProductName); SetForegroundWindow(p[0].MainWindowHandle); ShowWindow(p[0].MainWindowHandle,SW_MAXIMIZE); }
如果您真的想在最大化之前测试窗口是否最小化,可以使用旧式的IsIconic函数 :
[DllImport("user32.dll",CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsIconic(IntPtr hWnd); // [...] if (!Ok) { Process[] p = Process.GetProcessesByName(ProductName); IntPtr hwndMain= p[0].MainWindowHandle; SetForegroundWindow(hwndMain); if (IsIconic(hwndMain)) { ShowWindow(hwndMain,SW_MAXIMIZE); } }
如果您只想激活窗口(而不是最大化),请使用SW_SHOW值( 5 )而不是SW_MAXIMIZE 。 这将在最小化之前将其恢复到之前的状态。
Program.cs中
static class Program { private static volatile bool _exitProcess; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { bool creatednew; var showMeEventHandle = new EventWaitHandle(false,EventResetMode.AutoReset,"MyApp.ShowMe",out creatednew); if (creatednew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form1(); new Thread(() => { while (!_exitProcess) { showMeEventHandle.WaitOne(-1); if (!_exitProcess) { if (form.Invokerequired) { form.BeginInvoke((MethodInvoker)form.BringFormToFront); } else { form.BringFormToFront(); } } } }).Start(); Application.Run(form); } _exitProcess = true; showMeEventHandle.Set(); showMeEventHandle.Close(); } }
ExtMethods.cs
public static class ExtMethods { public static void BringFormToFront(this Form form) { form.WindowState = FormWindowState.normal; form.ShowInTaskbar = true; form.Show(); form.Activate(); } }
Form1.cs的
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void notifyIcon1_MouseDoubleClick(object sender,MouseEventArgs e) { this.BringFormToFront(); } private void button1_Click(object sender,EventArgs e) { WindowState = FormWindowState.Minimized; ShowInTaskbar = false; Hide(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。