Winform中控件,鼠标坐标位置计算

时间:2017-06-20 13:15:55   收藏:0   阅读:337

Winform中获得鼠标位置的 (是鼠标到屏幕左上角的位置):

pt = Cursor.Position;

获得控件距屏幕位置(是控件到屏幕左上角位置)

PointToScreen(this.drawingPanel.Location

获得控件距离Form窗体位置:

this.drawingPanel.Location

这里需注意鼠标的位置是到屏幕左上角距离,而不是到Form窗体的距离,这里取部分代码截图

//滑动前鼠标距离Panel左上角位置距离                
SizeF screenMouseToPanel = (Size)pt - (Size)PointToScreen(this.drawingPanel.Location);
//滑动前鼠标距离panel左上角占比                
double mouseInPanelXPercent = screenMouseToPanel.Width / this.drawingPanel.Width;
double mouseInPanelYPercent = screenMouseToPanel.Height / this.drawingPanel.Height;


原文:http://volvo9yue.blog.51cto.com/12265053/1940067

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!