none
asp.net中怎么获取鼠标的位置? RRS feed

  • 问题

  • 当我鼠标移动到某一个位置的时候,怎么捕捉到鼠标的位置,让他显示出来,请帮下忙,谢谢啦!!!

    • 已移动 孟宪会Moderator 2009年7月17日 12:44 (发件人:.NET Framework 一般性问题讨论区)
    • 已移动 孟宪会Moderator 2009年7月17日 12:46 (发件人:ASP.NET 与 AJAX)
    2009年7月17日 9:50

答案

  • 这个需要用js来写,下面的代码摘自msdn,修改了获取鼠标的方法,增加了窗体滚动条的计算。请参考:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>

    <head>
    <title>onmouseover Event Sample</title>
    <meta http-equiv="Content-Type" content="text/html; CHARSET=iso-8859-1">
    <meta name="AUTHOR" content="InetSDK">
    <meta name="MS.LOCALE" content="EN-US">
    <meta name="ROBOTS" content="noindex">
    <!-- SAMPLE_STYLE_START -->
    <link rel="stylesheet" href="samplesSDKIE4.css" type="text/css">
    <!-- SAMPLE_STYLE_END -->
    <style type="text/css">
    DIV.scratch {
     padding: 10;
     background-color: #CFCFCF;
     width: 250px;
     height: 150px;
     overflow: auto;
    }
    SPAN.notice {
     background-color: #FFFFAA;
     font-size: 8pt;
     font-family: Arial;
     width: 100px;
     height: 15px;
    }
    </style>
    <script type="text/javascript">
    function fnTrackMouse(){
     var mouseY = event.clientY + document.body.scrollTop;
     var mouseX = event.clientY + document.body.scrollLeft;
     oNotice.innerText="Coords: (" + mouseX + ", " + mouseY + ")";
    }
    </script>
    </head>

    <!--TOOLBAR_START-->
    <!--TOOLBAR_EXEMPT-->
    <!--TOOLBAR_END-->
    <body>

    <div class="body">
     <h1>onmousemove Event Sample</h1>
     <p>This sample demonstrates the use of the <b>onmousemove</b> event to monitor
     the location of the mouse cursor in a specified region of the page. When the
     mouse is moved over the gray <b>div</b>, the yellow <b>span</b> is updated with
     the mouse location values obtained from the <b>clientX</b> and <b>clientY</b>
     properties on the <b>event</b> object.</p>
     <div id="oScratch" class="scratch" onmousemove="fnTrackMouse()">
      <span id="oNotice" class="notice"></span></div>
     <!-- START_PAGE_FOOTER --><br>
     <br>
     <br>
     <p class="viewsource">[Right-click and choose View Source from the shortcut
     menu.] </p>
     <a class="copyright" href="http://www.microsoft.com/isapi/gomscom.asp?TARGET=/info/cpyright.htm">
     ?2007 Microsoft Corporation. All rights reserved. Terms of use.</a>
     <!-- END_PAGE_FOOTER --></div>

    </body>

    </html>

     

    2009年7月17日 11:23

全部回复

  • 这个需要用js来写,下面的代码摘自msdn,修改了获取鼠标的方法,增加了窗体滚动条的计算。请参考:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>

    <head>
    <title>onmouseover Event Sample</title>
    <meta http-equiv="Content-Type" content="text/html; CHARSET=iso-8859-1">
    <meta name="AUTHOR" content="InetSDK">
    <meta name="MS.LOCALE" content="EN-US">
    <meta name="ROBOTS" content="noindex">
    <!-- SAMPLE_STYLE_START -->
    <link rel="stylesheet" href="samplesSDKIE4.css" type="text/css">
    <!-- SAMPLE_STYLE_END -->
    <style type="text/css">
    DIV.scratch {
     padding: 10;
     background-color: #CFCFCF;
     width: 250px;
     height: 150px;
     overflow: auto;
    }
    SPAN.notice {
     background-color: #FFFFAA;
     font-size: 8pt;
     font-family: Arial;
     width: 100px;
     height: 15px;
    }
    </style>
    <script type="text/javascript">
    function fnTrackMouse(){
     var mouseY = event.clientY + document.body.scrollTop;
     var mouseX = event.clientY + document.body.scrollLeft;
     oNotice.innerText="Coords: (" + mouseX + ", " + mouseY + ")";
    }
    </script>
    </head>

    <!--TOOLBAR_START-->
    <!--TOOLBAR_EXEMPT-->
    <!--TOOLBAR_END-->
    <body>

    <div class="body">
     <h1>onmousemove Event Sample</h1>
     <p>This sample demonstrates the use of the <b>onmousemove</b> event to monitor
     the location of the mouse cursor in a specified region of the page. When the
     mouse is moved over the gray <b>div</b>, the yellow <b>span</b> is updated with
     the mouse location values obtained from the <b>clientX</b> and <b>clientY</b>
     properties on the <b>event</b> object.</p>
     <div id="oScratch" class="scratch" onmousemove="fnTrackMouse()">
      <span id="oNotice" class="notice"></span></div>
     <!-- START_PAGE_FOOTER --><br>
     <br>
     <br>
     <p class="viewsource">[Right-click and choose View Source from the shortcut
     menu.] </p>
     <a class="copyright" href="http://www.microsoft.com/isapi/gomscom.asp?TARGET=/info/cpyright.htm">
     ?2007 Microsoft Corporation. All rights reserved. Terms of use.</a>
     <!-- END_PAGE_FOOTER --></div>

    </body>

    </html>

     

    2009年7月17日 11:23
  • 你好!
         直接使用Cursor.Position就可以获取鼠标的位置了!
    周雪峰
    2009年7月17日 12:46
  • cursor.Position需要引入什么命名空间吗?
    2009年7月20日 1:38
  • using System.Windows.Forms;
    周雪峰
    2009年7月20日 5:05