简体   繁体   English

如何添加隐藏和显示 Visual Studio 应用程序的快捷方式?

[英]How to add a shortcut that hides and shows a Visual Studio Application?

I am very new to C# and Visual Studio btw.顺便说一句,我对 C# 和 Visual Studio 很陌生。 What I am trying to do is that whenever I click a keyboard key my windows application disappears, and if I click the same key again it comes back.我想要做的是,每当我单击键盘键时,我的 Windows 应用程序就会消失,如果我再次单击同一个键,它就会返回。 This OR that when I click the key it moves in the back of the programs that I have opened behind it, and if I press it again it moves infront.当我单击该键时,它会移动到我在它后面打开的程序的后面,如果我再次按下它,它会移动到前面。

I make a code example, which could show or hide the app when you click F10.我做了一个代码示例,当你点击 F10 时,它可以显示或隐藏应用程序。

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        KeyboardHook kh;
        bool b=true;
        private void Form1_Load(object sender, EventArgs e)
        {
            kh = new KeyboardHook();

            kh.SetHook();

            kh.OnKeyDownEvent += kh_OnKeyDownEvent;

        }

        void kh_OnKeyDownEvent(object sender, KeyEventArgs e)
        {

            if (e.KeyData == (Keys.F10) && !b) { this.Show(); }
            if (e.KeyData == (Keys.F10 ) && b) { this.Hide(); }
            b=!b;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            kh.UnHook();
        }
    }
    public class Win32Api

    {

        #region 

        public const int WM_KEYDOWN = 0x100;

        public const int WM_KEYUP = 0x101;

        public const int WM_SYSKEYDOWN = 0x104;

        public const int WM_SYSKEYUP = 0x105;

        public const int WH_KEYBOARD_LL = 13;



        [StructLayout(LayoutKind.Sequential)] 

        public class KeyboardHookStruct

        {

            public int vkCode; 

            public int scanCode; 

            public int flags;

            public int time;

            public int dwExtraInfo;

        }

        #endregion

        #region Api

        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);



        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);



        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern bool UnhookWindowsHookEx(int idHook);



        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

        [DllImport("user32")]

        public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);

        [DllImport("user32")]

        public static extern int GetKeyboardState(byte[] pbKeyState);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern IntPtr GetModuleHandle(string lpModuleName);

        #endregion
    }
    public class KeyboardHook

    {

        int hHook;

        Win32Api.HookProc KeyboardHookDelegate;

        public event KeyEventHandler OnKeyDownEvent;

        public event KeyEventHandler OnKeyUpEvent;

        public event KeyPressEventHandler OnKeyPressEvent;

        public KeyboardHook() { }

        public void SetHook()

        {

            KeyboardHookDelegate = new Win32Api.HookProc(KeyboardHookProc);

            Process cProcess = Process.GetCurrentProcess();

            ProcessModule cModule = cProcess.MainModule;

            var mh = Win32Api.GetModuleHandle(cModule.ModuleName);

            hHook = Win32Api.SetWindowsHookEx(Win32Api.WH_KEYBOARD_LL, KeyboardHookDelegate, mh, 0);

        }

        public void UnHook()

        {

            Win32Api.UnhookWindowsHookEx(hHook);

        }

        private List<Keys> preKeysList = new List<Keys>();

        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {

            if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))

            {

                Win32Api.KeyboardHookStruct KeyDataFromHook = (Win32Api.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.KeyboardHookStruct));

                Keys keyData = (Keys)KeyDataFromHook.vkCode;


                if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == Win32Api.WM_KEYDOWN || wParam == Win32Api.WM_SYSKEYDOWN))

                {

                    if (IsCtrlAltShiftKeys(keyData) && preKeysList.IndexOf(keyData) == -1)

                    {

                        preKeysList.Add(keyData);

                    }

                }

                if (OnKeyDownEvent != null && (wParam == Win32Api.WM_KEYDOWN || wParam == Win32Api.WM_SYSKEYDOWN))

                {

                    KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));



                    OnKeyDownEvent(this, e);

                }

                if (OnKeyPressEvent != null && wParam == Win32Api.WM_KEYDOWN)

                {

                    byte[] keyState = new byte[256];

                    Win32Api.GetKeyboardState(keyState);

                    byte[] inBuffer = new byte[2];

                    if (Win32Api.ToAscii(KeyDataFromHook.vkCode, KeyDataFromHook.scanCode, keyState, inBuffer, KeyDataFromHook.flags) == 1)

                    {

                        KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);

                        OnKeyPressEvent(this, e);

                    }

                }

                if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == Win32Api.WM_KEYUP || wParam == Win32Api.WM_SYSKEYUP))

                {

                    if (IsCtrlAltShiftKeys(keyData))

                    {

                        for (int i = preKeysList.Count - 1; i >= 0; i--)

                        {

                            if (preKeysList[i] == keyData) { preKeysList.RemoveAt(i); }

                        }

                    }

                }


                if (OnKeyUpEvent != null && (wParam == Win32Api.WM_KEYUP || wParam == Win32Api.WM_SYSKEYUP))

                {

                    KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));

                    OnKeyUpEvent(this, e);

                }

            }

            return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);

        }

        private Keys GetDownKeys(Keys key)

        {

            Keys rtnKey = Keys.None;

            foreach (Keys i in preKeysList)

            {

                if (i == Keys.LControlKey || i == Keys.RControlKey) { rtnKey = rtnKey | Keys.Control; }

                if (i == Keys.LMenu || i == Keys.RMenu) { rtnKey = rtnKey | Keys.Alt; }

                if (i == Keys.LShiftKey || i == Keys.RShiftKey) { rtnKey = rtnKey | Keys.Shift; }

            }

            return rtnKey | key;

        }

        private Boolean IsCtrlAltShiftKeys(Keys key)

        {

            if (key == Keys.LControlKey || key == Keys.RControlKey || key == Keys.LMenu || key == Keys.RMenu || key == Keys.LShiftKey || key == Keys.RShiftKey) { return true; }

            return false;

        }

    }

Result:结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在普通项目Visual Studio 2008上添加快捷方式do文件 - How to add a shortcut do file on normal project Visual Studio 2008 Visual Studio 2013中是否有快捷方式显示所选元素的定义弹出窗口? - Is there a shortcut in Visual Studio 2013 that shows the definition pop up of the selected element? 如何在 Visual Studio 中编辑“for + tab”快捷方式的结果? - How to edit result of “for + tab” shortcut in Visual Studio? 如何添加添加一些MSI应用程序到Visual Studio InstallShield Redistributables? - How to add add some msi application to visual studio InstallShield Redistributables? Visual Studio /**/ 注释快捷方式? - Visual Studio /**/ comment shortcut? Visual Studio中字符串的快捷方式 - Shortcut to string in Visual Studio 在 Visual Studio 2019 中添加带有 css 类的 div 的键盘快捷键 - Keyboard shortcut to add a div with a css class in Visual Studio 2019 如何将应用程序安装文件添加到Visual Studio 2010安装程序? - How to add application setup file to visual studio 2010 setup? 如何在 Visual Studio Code 中向 wpf 应用程序添加图标 - How to add icon to wpf Application in Visual Studio Code 如何在Visual Studio中开发的C#应用​​程序中添加空白行? - How to add a blank lines in C# application developed in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM