zoukankan      html  css  js  c++  java
  • wpf 与winform获取鼠标坐标的差异

    1. winform中以Panel为例的MouseDown事件为:
      private void panelposition_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
      {
            //分别用e.X,e.Y来获取鼠标的x,y坐标(e.X,e.Y分别为整数)
              int x=e.X;
              int y=e.Y;
      }
    2. wpf中以Image为例的MouseDown事件为:
      private void imageposition_MouseDown(object sender, MouseButtonEventArgs e)
      {
              //采用e.GetPosition(IInputElement relativeTo)来获取鼠标的x,y坐标
              //以下部分来自于metadata中:
              //--------------------------------------------------------------
              // Summary:
              //     Returns the position of the mouse pointer relative to the specified element.
              //
              // Parameters:
              //   relativeTo:
              //     The element to use as the frame of reference for calculating the position
              //     of the mouse pointer.
              //
              // Returns:
              //     The x- and y-coordinates of the mouse pointer position relative to the specified
              //     object.
              public Point GetPosition(IInputElement relativeTo);
              //--------------------------------------------------------------
              //其中relativeTo为选择的对象,即在图片上面就是图片对象,窗体上面就是window对象。

                    //e.GetPosition(img).X为double类型,需转换后使用。      
                    int x = Convert.ToInt32(e.GetPosition(img).X);
                    int y = Convert.ToInt32(e.GetPosition(img).Y);

            }

  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/peasana/p/2349523.html
Copyright © 2011-2022 走看看