• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

wincec#创建桌面快捷方式自动启动只运行一次全屏显示

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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)
                {

                }
            }
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#集合ArrayList:可以存储任何类型的数组,的基本用法发布时间:2022-07-14
下一篇:
C++编译的基本知识发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap