码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-12-04 19:50:13      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   os   sp   for   on   div   

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 bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)
        //{
        //    IntPtr Hwnd = FindWindow("HHTaskBar", null);
        //    if (Hwnd == IntPtr.Zero) return false;
        //    if (fullscreen)
        //    {
        //        ShowWindow(Hwnd, SW_HIDE);
        //    }
        //    else
        //    {
        //        ShowWindow(Hwnd, SW_SHOW);
        //    }
        //    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()
        {
            string PathGPRSTrue = @"/Windows/Desktop/app.exe.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.CreateNew);
                    fs.Write(buf, 0, buf.Length);
                    fs.Close();
                    fs = null;
                }
                catch (System.Exception ex)
                {

                }
            }
        }
    }
}

  

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

标签:des   blog   io   ar   os   sp   for   on   div   

原文地址:http://www.cnblogs.com/FuYan/p/4143444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!