ImageSQL/Program.cs

来源:互联网 发布:数据库远程访问工具 编辑:程序博客网 时间:2024/05/16 12:50

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ImageSQL
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/ImageMSSQL", out isCreated))
            {
                if (isCreated)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        FormImageSQL frame = new FormImageSQL();
                        subKey.SetValue("Handle", frame.Handle);
                        Application.Run(frame);
                    }
                    newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        IntPtr handle = new IntPtr(Convert.ToInt32(subKey.GetValue("Handle")));
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                }
            }
            #endregion
        }
    }
}

原创粉丝点击