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

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

pt = Cursor.Position;

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

PointToScreen(this.drawingPanel.Location

得到控件距离Form窗体位置:orm

this.drawingPanel.Location

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

//滑动前鼠标距离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;
相关文章
相关标签/搜索