WPF C# 常用操作

来源:互联网 发布:电脑内存条检测软件 编辑:程序博客网 时间:2024/05/10 16:16

获取鼠标位置

private void button2_Click(object sender, RoutedEventArgs e)//获取位置
        {
            POINT p = new POINT();

            Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
            Point ppp = (e.Source as FrameworkElement).PointToScreen(pp);//WPF方法

            if (GetCursorPos(out p))//API方法
            {
                MessageBox.Show(string.Format("GetCursorPos {0},{1}  GetPosition {2},{3}\r\n {4},{5}", p.X, p.Y, pp.X, pp.Y, ppp.X, ppp.Y));
            }
        }

        /// <summary>   
        /// 设置鼠标的坐标   
        /// </summary>   
        /// <param name="x">横坐标</param>   
        /// <param name="y">纵坐标</param>   
        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        /// <summary>   
        /// 获取鼠标的坐标   
        /// </summary>   
        /// <param name="lpPoint">传址参数,坐标point类型</param>   
        /// <returns>获取成功返回真</returns>   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);



屏幕大小

 double width = SystemParameters.PrimaryScreenWidth;

  double height = SystemParameters.PrimaryScreenHeight;


字符串全半角转换

 ///  <summary> 
        /// 转全角的函数(SBC case) 
        ///  </summary> 
        ///  <param name="input">任意字符串 </param> 
        ///  <returns>全角字符串 </returns> 
        /// <remarks> 
        ///全角空格为12288,半角空格为32 
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 
        /// </remarks>         
        public static string ToSBC(string input)
        {
            //半角转全角: 
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }


        ///  <summary> 
        /// 转半角的函数(DBC case) 
        ///  </summary> 
        ///  <param name="input">任意字符串 </param> 
        ///  <returns>半角字符串 </returns> 
        /// <remarks> 
        ///全角空格为12288,半角空格为32 
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 
        /// </remarks> 
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }