WPF 开发的实用小工具(附源码)持续更新(七)根据应用首个字的首字母按键定位

接着上一篇  

 点击链接加入群聊

 在使用过程中当应用太多很难快速的找到并定位到自己所需要使用的应用,所以此次做了更新方便快速查找应用。

【update】
1、输入应用首字的首字母定位。(LeftAlt+(应用首字的首字母)

 

 

键盘按下 KeyDown 

定义ScrollViewer 所需要滚动值 double offset = 0.0;。

定义 bool isFind = false; 如果为 True 找到就不查找控件。

1、通过FindChild查找ScrollViewer方便最后定位使用。

ScrollViewer scrollViewer = ControlsHelper.FindChild<ScrollViewer>(this.AppSwitchList, "PART_ScrollViewer");

2、通过FindVisualChildren查找我所定义DataTemplate下的Border。

var elementList = ControlsHelper.FindVisualChildren<Border>(this.AppSwitchList).ToList();

3、循环 elementList 集合,判断 Tag 不等于 null 时对 offset += element.ActualHeight ,并且相等于按下键盘的Key值。

if (element.Tag != null && element.Tag.ToString().Equals(_key))
4、定位到找到的第一个与Key相等的。

scrollViewer.ScrollToVerticalOffset(offset);

var _key = Win32Api.GetCharFromKey(e.Key).ToString().ToUpper();
            if (string.IsNullOrWhiteSpace(_key))
                return;
            double offset = 0.0;
            ScrollViewer scrollViewer = ControlsHelper.FindChild<ScrollViewer>(this.AppSwitchList, "PART_ScrollViewer");
            var elementList = ControlsHelper.FindVisualChildren<Border>(this.AppSwitchList).ToList();
            bool isFind = false;
            for (int i = 0; i < elementList.Count; i++)
            {
                var element = elementList[i];
                if (isFind) break;
                if (element.Tag != null)
                {
                    offset += element.ActualHeight;
                    if (element.Tag.ToString().Equals(_key))
                    {
                        offset -= element.ActualHeight;
                        scrollViewer.ScrollToVerticalOffset(offset);
                        isFind = true;
                    }
                }

            }
            this.KeyDownText.Text = _key;
            this.KeyDownPanel.Visibility = Visibility.Visible;
View Code

获取按键转译

public enum MapType : uint
        {
            MAPVK_VK_TO_VSC = 0x0,
            MAPVK_VSC_TO_VK = 0x1,
            MAPVK_VK_TO_CHAR = 0x2,
            MAPVK_VSC_TO_VK_EX = 0x3,
        }

        [DllImport("user32.dll")]
        public static extern int ToUnicode(
            uint wVirtKey,
            uint wScanCode,
            byte[] lpKeyState,
            [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
            StringBuilder pwszBuff,
            int cchBuff,
            uint wFlags);

        [DllImport("user32.dll")]
        public static extern bool GetKeyboardState(byte[] lpKeyState);

        [DllImport("user32.dll")]
        public static extern uint MapVirtualKey(uint uCode, MapType uMapType);

        public static char GetCharFromKey(Key key)
        {
            char ch = ' ';

            int virtualKey = KeyInterop.VirtualKeyFromKey(key);
            byte[] keyboardState = new byte[256];
            GetKeyboardState(keyboardState);

            uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
            StringBuilder stringBuilder = new StringBuilder(2);

            int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
            switch (result)
            {
                case -1:
                    break;
                case 0:
                    break;
                case 1:
                    {
                        ch = stringBuilder[0];
                        break;
                    }
                default:
                    {
                        ch = stringBuilder[0];
                        break;
                    }
            }
            return ch;
        }
View Code

 抬起后隐藏

  Thread.Sleep(300);
            this.KeyDownPanel.Visibility = Visibility.Collapsed;
View Code

 

 效果图

 

 源码下载地址

 gitee

下载解压后体验

posted @ 2021-03-10 15:22  山上一边边  阅读(120)  评论(0编辑  收藏  举报