zoukankan      html  css  js  c++  java
  • C# .NET中如何使用GetCursorPos函数

    转自:http://blog.csdn.net/wl58796351/article/details/8122003

    C# .NET中如何使用GetCursorPos函数 例程

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace CursorPosition
    {
        public partial class frmMain : Form
        {
            // We need to use unmanaged code
            [DllImport("user32.dll")]
            // GetCursorPos() makes everything possible
            static extern bool GetCursorPos(ref Point lpPoint);
            // Variable we will need to count the traveled pixels
            static protected long totalPixels = 0;
            static protected int currX;
            static protected int currY;
            static protected int diffX;
            static protected int diffY;

            public frmMain()
            {
                InitializeComponent();
            }

            private void tmrDef_Tick(object sender, EventArgs e)
            {
                // New point that will be updated by the function with the current coordinates
                Point defPnt = new Point();
                // Call the function and pass the Point, defPnt
                GetCursorPos(ref defPnt);
                // Now after calling the function, defPnt contains the coordinates which we can read
                lblCoordX.Text = "X = " + defPnt.X.ToString();
                lblCoordY.Text = "Y = " + defPnt.Y.ToString();
                // If the cursor has moved at all
                if (diffX != defPnt.X | diffY != defPnt.Y)
                {
                    // Calculate the distance of movement (in both vertical and horizontal movement)
                    diffX = (defPnt.X - currX);
                    diffY = (defPnt.Y - currY);
                    // The difference will be negative if the cursor was moved left or up
                    // and if it is so, make the number positive
                    if (diffX < 0)
                    {
                        diffX *= -1;
                    }
                    if (diffY < 0)
                    {
                        diffY *= -1;
                    }
                    // Add to the "pixels traveled" counter
                    totalPixels += diffX + diffY;
                    // And display inside a label
                    lblTravel.Text = "You have traveled " + totalPixels + " pixels";
                }
                // We need this to see the difference of pixels between two mouse movements
                currX = defPnt.X;
                currY = defPnt.Y;
            }

            private void btnReset_Click(object sender, EventArgs e)
            {
                totalPixels = 0;
            }
        }
    }

  • 相关阅读:
    BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )
    BZOJ 2038: [2009国家集训队]小Z的袜子(hose) ( 莫队 )
    BZOJ 3555: [Ctsc2014]企鹅QQ( hash )
    BZOJ 2226: [Spoj 5971] LCMSum( 数论 )
    BZOJ 3505: [Cqoi2014]数三角形( 组合数 )
    BZOJ 2510: 弱题( 矩阵快速幂 )
    BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )
    BZOJ 1090: [SCOI2003]字符串折叠( 区间dp )
    HDU 2295 Radar dancing links 重复覆盖
    ZOJ 3209 Treasure Map dancing links
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/2832409.html
Copyright © 2011-2022 走看看