Esempio n. 1
0
        /// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="view"></param>
        public static void Register(TemplateEditView view)
        {
            if (view == null)
            {
                return;
            }


            if (_registedHandle == default(IntPtr))
            {
                var ps = PresentationSource.FromVisual(view);

                if (ps != null)
                {
                    HwndSource hs = ps as HwndSource;

                    if (hs != null)
                    {
                        _registedHandle = hs.Handle;
                    }
                }
            }

            if (_registedHandle != default(IntPtr))
            {
                if (_registed)
                {
                    Unregister(view);
                }

                _view = view;

                //向全局原子表申请唯一标识符
                _atomL     = Hotkey.GlobalAddAtom("L");
                _atomR     = Hotkey.GlobalAddAtom("R");
                _atomV     = Hotkey.GlobalAddAtom("V");
                _atomT     = Hotkey.GlobalAddAtom("T");
                _atomC     = Hotkey.GlobalAddAtom("C");
                _atomI     = Hotkey.GlobalAddAtom("I");
                _atomW     = Hotkey.GlobalAddAtom("W");
                _atomQ     = Hotkey.GlobalAddAtom("Q");
                _atomCtrlZ = Hotkey.GlobalAddAtom("Ctrl-Z");
                _atomCtrlY = Hotkey.GlobalAddAtom("Ctrl-Y");

                //关联热键对应的行为
                _actionWithHotkey             = new Dictionary <int, System.Action>();
                _actionWithHotkey[_atomL]     = delegate { view.RBLine.IsChecked = true; };
                _actionWithHotkey[_atomR]     = delegate { view.RBRectangle.IsChecked = true; };
                _actionWithHotkey[_atomV]     = delegate { view.RBDrag.IsChecked = true; };
                _actionWithHotkey[_atomT]     = delegate { view.RBTriangle.IsChecked = true; };
                _actionWithHotkey[_atomC]     = delegate { view.RBCircle.IsChecked = true; };
                _actionWithHotkey[_atomI]     = delegate { view.RBImage.IsChecked = true; };
                _actionWithHotkey[_atomW]     = delegate { view.RBText.IsChecked = true; };
                _actionWithHotkey[_atomQ]     = delegate { view.RBQRCode.IsChecked = true; };
                _actionWithHotkey[_atomCtrlZ] = delegate { };
                _actionWithHotkey[_atomCtrlY] = delegate { };

                //注册热键
                Hotkey.RegisterHotKey(_registedHandle, _atomL, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.L);
                Hotkey.RegisterHotKey(_registedHandle, _atomR, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.R);
                Hotkey.RegisterHotKey(_registedHandle, _atomV, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.V);
                Hotkey.RegisterHotKey(_registedHandle, _atomT, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.T);
                Hotkey.RegisterHotKey(_registedHandle, _atomC, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.C);
                Hotkey.RegisterHotKey(_registedHandle, _atomI, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.I);
                Hotkey.RegisterHotKey(_registedHandle, _atomW, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.W);
                Hotkey.RegisterHotKey(_registedHandle, _atomQ, Hotkey.KeyModifiers.None, (int)Hotkey.Keys.Q);
                Hotkey.RegisterHotKey(_registedHandle, _atomCtrlZ, Hotkey.KeyModifiers.Ctrl, (int)Hotkey.Keys.Z);
                Hotkey.RegisterHotKey(_registedHandle, _atomCtrlY, Hotkey.KeyModifiers.Ctrl, (int)Hotkey.Keys.Y);

                _registed = true;
                Debug.WriteLine("注册热键");
            }
        }