using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace SingleXZ
{
    class FullScreenClass
    {
        public static string CodePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;//获取当前应用程序完整路径
        public const int SPI_SETWORKAREA = 47;
        public const int SPI_GETWORKAREA = 48;
        public const int SW_HIDE = 0x00;
        public const int SW_SHOW = 0x0001;
        public const int SPIF_UPDATEINIFILE = 0x01;
        public const int WM_CLOSE = 0x10;
        [DllImport("coredll.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpWindowName, string lpClassName);
        [DllImport("coredll.dll", EntryPoint = "ShowWindow")]
        private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("coredll.dll", EntryPoint = "SystemParametersInfo")]
        private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
        [DllImport("coredll.dll", EntryPoint = "SystemParametersInfo")]
        private static extern bool IsWindowVisible(IntPtr hwnd);
        [DllImport("coredll.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("coredll.dll", EntryPoint = "CreateMutex", SetLastError = true)]
        public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);

        [DllImport("coredll.dll", EntryPoint = "ReleaseMutex", SetLastError = true)]
        public static extern bool ReleaseMutex(IntPtr hMutex);

        private const int ERROR_ALREADY_EXISTS = 0183; 
        /// <summary>
        /// 设置全屏或取消全屏
        /// </summary>
        /// <param name="fullscreen">true:全屏 false:恢复</param>
        /// <param name="rectOld">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param>
        /// <returns>设置结果</returns>
        public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)
        {
            IntPtr Hwnd = FindWindow("HHTaskBar", null);
            if (Hwnd == IntPtr.Zero) return false;
            if (fullscreen)
            {
                ShowWindow(Hwnd, SW_HIDE);
                Rectangle rectFull = Screen.PrimaryScreen.Bounds;
                SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//set
            }
            else
            {
                ShowWindow(Hwnd, SW_SHOW);
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);
            }
            return true;
        }

        public static void KillMessageBox(string sCaption)
        {
            IntPtr ptr = FindWindow(null, sCaption);
            if (ptr != IntPtr.Zero)
                PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        }

        //程序只能运行一次
        public static bool IsExist()
        {
            string strAppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            IntPtr hMutex = CreateMutex(IntPtr.Zero, true, strAppName);
            if (hMutex == IntPtr.Zero)
                throw new ApplicationException("Failure creating mutex: " + Marshal.GetLastWin32Error().ToString("X"));

            if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
            {
                ReleaseMutex(hMutex);
                return true;
            }
            return false;
        }

        public static void AutoRun()
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("init", true))
            {
                key.SetValue("Launch70", CodePath);
            }
        }

        public static void DesktopLink()
        {
            //根据自己桌面路径写 有些事desktop 有些事桌面
            string PathGPRSTrue = "\\Windows\\桌面\\烟支·滤棒吸阻测试仪.lnk";
            //if (!File.Exists(PathGPRSTrue))  如果存在就不想重新写就不用注释(路径不固定这个还是注释掉好点)
            {
                //File.Copy(PathGPRS, PathGPRSTrue, true);
                try
                {
                    string text = (CodePath.Length + 2).ToString() + "#\"" + CodePath + "\"";
                    byte[] buf = new byte[text.Length];
                    buf = System.Text.Encoding.GetEncoding(936).GetBytes(text.Substring(0, text.Length));
                    FileStream fs = new FileStream(PathGPRSTrue, FileMode.OpenOrCreate);
                    fs.Write(buf, 0, buf.Length);
                    fs.Close();
                    fs = null;
                }
                catch (System.Exception ex)
                {

                }
            }
        }
    }
}

 

posted on 2014-12-04 17:39  倾天  阅读(571)  评论(0编辑  收藏  举报