首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何只运行一个winform啊

2012-05-30 
怎么只运行一个winform啊??比如我打开了一个winform,最小化到托盘了.然后我又打开了一个刚才那个winform,

怎么只运行一个winform啊??
比如我打开了一个winform,最小化到托盘了.

然后我又打开了一个刚才那个winform,

怎么让原来最小化到托盘的那个程序显示出来

上代码哦!!

[解决办法]

C# code
using System;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Diagnostics;using System.Reflection;using System.Threading;namespace TEST{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new FrmMain());            Process instance = RunningInstance();            if (instance == null)            {                Application.Run(new FrmMain());            }            else            {                HandleRunningInstance(instance);            }         }        #region 进程名        public static Process RunningInstance()        {            Process current = Process.GetCurrentProcess();            Process[] processes = Process.GetProcessesByName(current.ProcessName);            foreach (Process process in processes)            {                if (process.Id != current.Id)                {                    if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") == current.MainModule.FileName)                    {                        return process;                    }                }            }            return null;        }        public static void HandleRunningInstance(Process instance)        {            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);            SetForegroundWindow(instance.MainWindowHandle);        }        [DllImport("User32.dll ")]        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);        [DllImport("User32.dll ")]        private static extern bool            SetForegroundWindow(IntPtr hWnd);        private const int WS_SHOWNORMAL = 1;        #endregion    }}
[解决办法]
C# code
   private void button1_Click(object sender, EventArgs e)        {            Form2 frm = Form2.CreateInstance();            if (frm.WindowState == FormWindowState.Minimized)            {                frm.WindowState = FormWindowState.Normal;            }            else            {                frm.Show();            }        } public partial class Form2 : Form    {        private static readonly object m_SyncObject = new object();        private static Form2 frm;        private Form2()        {            InitializeComponent();        }        public static Form2 CreateInstance()        {            if (frm == null)            {                lock (m_SyncObject)                {                    if (frm == null)                    {                        frm = new Form2();                    }                }            }            return frm;        }    } 

热点排行