zoukankan      html  css  js  c++  java
  • WPF获取鼠标相对于屏幕的绝对位置

    最新文章:Virson's Blog

    最近在看《深入浅出WPF》,在做练习的时候自己做了一下扩展,在此记下:

    XAML代码:

     1 <Window x:Class="TestCanvas.MainWindow"
     2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4         Title="登录" Height="110px" Width="300px" WindowStartupLocation="CenterScreen" WindowStyle="None" 
     5         MaxHeight="110px" MaxWidth="300px" MinHeight="110px" MinWidth="300px" MouseDown="Mouse_downHandler"> 
     6     <Canvas>
     7         <TextBlock Text="用户名:" Canvas.Left="12px" Canvas.Top="12px"/>
     8         <TextBox x:Name="tbUserName"  Height="23px" Width="200px" BorderBrush="Black" Canvas.Left="66px" Canvas.Top="9px"/>
     9         <TextBlock Text="密码:" Canvas.Left="12px" Canvas.Top="40.72px" Height="16px" Width="36px"/>
    10         <TextBox x:Name="tbPasword"  Height="23px" Width="200px" BorderBrush="Black" Canvas.Left="66px" Canvas.Top="38px"/>
    11         <Button Content="登录" Width="80px" Height="23px" Canvas.Left="66" Canvas.Top="67px"/>
    12         <Button Content="退出" Width="80px" Height="23px" Canvas.Left="186" Canvas.Top="67px" Click="Close_clickHandler"/>
    13     </Canvas>
    14 </Window>

    CSharp代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Runtime.InteropServices;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Windows;
     8 using System.Windows.Controls;
     9 using System.Windows.Data;
    10 using System.Windows.Documents;
    11 using System.Windows.Input;
    12 using System.Windows.Media;
    13 using System.Windows.Media.Imaging;
    14 using System.Windows.Navigation;
    15 using System.Windows.Shapes;
    16 
    17 namespace TestCanvas
    18 {
    19     /// <summary>
    20     /// MainWindow.xaml 的交互逻辑
    21     /// </summary>
    22     public partial class MainWindow : Window
    23     {
    24         [StructLayout(LayoutKind.Sequential)]
    25         public struct POINT
    26         {
    27             public int X;
    28             public int Y;
    29 
    30             public POINT(int x, int y)
    31             {
    32                 this.X = x;
    33                 this.Y = y;
    34             }
    35         }
    36 
    37         [DllImport("user32.dll", CharSet = CharSet.Auto)]
    38         public static extern bool GetCursorPos(out POINT pt);
    39 
    40         public MainWindow()
    41         {
    42             InitializeComponent();
    43         }
    44 
    45         public void Close_clickHandler(Object sender, RoutedEventArgs e)
    46         {
    47             this.Close();
    48         }
    49 
    50         private void Mouse_downHandler(object sender, MouseButtonEventArgs e)
    51         {
    52             Point p = e.MouseDevice.GetPosition(this);  //获取鼠标相对位置
    53 
    54             MainWindow.POINT pit = new MainWindow.POINT();
    55             MainWindow.GetCursorPos(out pit);   //获取鼠标绝对位置
    56 
    57             StringBuilder sbRel = new StringBuilder();
    58             sbRel.Append("相对位置:(");
    59             sbRel.Append(p.X);
    60             sbRel.Append(",");
    61             sbRel.Append(p.Y);
    62             sbRel.Append(")");
    63             tbUserName.Text = sbRel.ToString();
    64 
    65             StringBuilder sbAbs = new StringBuilder();
    66             sbAbs.Append("绝对位置:(");
    67             sbAbs.Append(pit.X);
    68             sbAbs.Append(",");
    69             sbAbs.Append(pit.Y);
    70             sbAbs.Append(")");
    71             tbPasword.Text = sbAbs.ToString();
    72         }
    73     }
    74 }
  • 相关阅读:
    【加密】 RSA
    【Resource】【ResourceLoader】【ResourcePatternResolver】学习
    【拦截器】Springboot项目之拦截器使用
    【OpenSSl】linux下OpenSSL的RSA密钥生成
    【微服务架构】图解
    【md5】几种常见的数据摘要算法(MD5、CRC32、SHA1和SHA256)
    【VM VirtualBox】【Vagrant】的安装及使用
    VUE中解决echarts数据二次渲染不成功的问题xAxis
    TypeError: Cannot read property 'map' of null
    VS code 卡顿很严重
  • 原文地址:https://www.cnblogs.com/mawanglin2008/p/2865533.html
Copyright © 2011-2022 走看看