一个窗体最小化了,如何用代码让它恢复到原来的状态呢?
------------------------------------我已经实现了,让一个窗口以单例模式打开,现在有一个问题。
如果用户将窗口最小化了(注意,没有关闭,是最小化),可是用户不知道这个窗口是最小化,以后没有打开,所以再次
执行打开操作,可是这个窗口无法恢复到最小化前的状态。所以我想实现,让这个最小化的窗口恢复到刚才的显示状态,如何用代码实现呢?
------------------------------------我已经实现了,让一个窗口以单例模式打开,现在有一个问题。
如果用户将窗口最小化了(注意,没有关闭,是最小化),可是用户不知道这个窗口是最小化,以后没有打开,所以再次
执行打开操作,可是这个窗口无法恢复到最小化前的状态。所以我想实现,让这个最小化的窗口恢复到刚才的显示状态,如何用代码实现呢?
加上:
frm.Activate();//激活
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices; namespace WindowsFormsApplication2
{
static class Program
{
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern int SetForegroundWindow(IntPtr hwnd); /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
//防止程序启动多次,只有一个运行的实例
if (p.Length > 1)
{
//将之前启动的窗体显示出来。
SetForegroundWindow(p[1].MainWindowHandle);
return;//退出软件
}
//正常情况下的窗体显示
Application.Run(new Form1());
}
}
}
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); ShowWindowAsync(childHwnd, 1);
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
static class Program
{
[DllImport("user32.dll", EntryPoint="ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
//防止程序启动多次,只有一个运行的实例
if (p.Length > 1)
{
//将之前启动的窗体显示出来。
ShowWindow(p[1].MainWindowHandle, 1);
return;//退出软件
}
//正常情况下的窗体显示
Application.Run(new Form1());
} }
}
http://www.cnblogs.com/dancingbit/archive/2010/08/11/1797685.html
无论窗口是何种状态,都能正确地恢复。