22using System . Windows ;
33using System . Windows . Input ;
44using System . Windows . Media ;
5- using Application = System . Windows . Application ;
6- using Point = System . Windows . Point ;
7-
85
96namespace ShowWrite
107{
@@ -13,15 +10,14 @@ namespace ShowWrite
1310 /// </summary>
1411 public class PanZoomManager
1512 {
16-
1713 private readonly ScaleTransform _zoomTransform ;
1814 private readonly TranslateTransform _panTransform ;
1915 private readonly UIElement _targetElement ;
2016 private readonly DrawingManager _drawingManager ;
2117
2218 private double _currentZoom = 1.0 ;
2319 private bool _isPanning = false ;
24- private System . Windows . Point _lastMousePos ; // 明确命名空间
20+ private System . Windows . Point _lastMousePos ; // 明确指定为 WPF 的 Point
2521
2622 // 笔迹缩放补偿相关
2723 private double _originalPenWidth = 2.0 ;
@@ -107,20 +103,32 @@ public void SetOriginalPenWidth(double width)
107103 /// <summary>
108104 /// 鼠标按下事件处理
109105 /// </summary>
110- public void HandleMouseDown ( MouseButtonEventArgs e , DrawingManager . ToolMode currentMode )
106+ public void HandleMouseDown ( System . Windows . Input . MouseButtonEventArgs e , DrawingManager . ToolMode currentMode )
111107 {
112108 if ( ! CheckEnabled ( ) ) return ;
113109
114- if ( currentMode == DrawingManager . ToolMode . Move && e . ChangedButton == MouseButton . Left )
110+ if ( currentMode == DrawingManager . ToolMode . Move && e . ChangedButton == System . Windows . Input . MouseButton . Left )
115111 {
116112 try
117113 {
118114 _isPanning = true ;
119- _lastMousePos = e . GetPosition ( System . Windows . Application . Current . MainWindow ) ;
115+
116+ // 修复:使用 _targetElement 的父窗口来获取位置
117+ var window = Window . GetWindow ( _targetElement ) ;
118+ if ( window != null )
119+ {
120+ _lastMousePos = e . GetPosition ( window ) ;
121+ window . Cursor = System . Windows . Input . Cursors . SizeAll ;
122+ }
123+ else
124+ {
125+ // 备用方案:如果无法获取窗口,则使用 _targetElement
126+ _lastMousePos = e . GetPosition ( _targetElement ) ;
127+ }
128+
120129 _targetElement . CaptureMouse ( ) ;
121130 e . Handled = true ;
122131
123- System . Windows . Application . Current . MainWindow . Cursor = System . Windows . Input . Cursors . SizeAll ;
124132 Console . WriteLine ( $ "开始拖拽: 起始点=({ _lastMousePos . X : F1} , { _lastMousePos . Y : F1} )") ;
125133 }
126134 catch ( Exception ex )
@@ -142,7 +150,19 @@ public void HandleMouseMove(System.Windows.Input.MouseEventArgs e, DrawingManage
142150 {
143151 try
144152 {
145- var currentPos = e . GetPosition ( System . Windows . Application . Current . MainWindow ) ;
153+ // 修复:使用相同的窗口获取方式
154+ var window = Window . GetWindow ( _targetElement ) ;
155+ System . Windows . Point currentPos ;
156+
157+ if ( window != null )
158+ {
159+ currentPos = e . GetPosition ( window ) ;
160+ }
161+ else
162+ {
163+ currentPos = e . GetPosition ( _targetElement ) ;
164+ }
165+
146166 _panTransform . X += currentPos . X - _lastMousePos . X ;
147167 _panTransform . Y += currentPos . Y - _lastMousePos . Y ;
148168 _lastMousePos = currentPos ;
@@ -158,17 +178,24 @@ public void HandleMouseMove(System.Windows.Input.MouseEventArgs e, DrawingManage
158178 /// <summary>
159179 /// 鼠标释放事件处理
160180 /// </summary>
161- public void HandleMouseUp ( MouseButtonEventArgs e , DrawingManager . ToolMode currentMode )
181+ public void HandleMouseUp ( System . Windows . Input . MouseButtonEventArgs e , DrawingManager . ToolMode currentMode )
162182 {
163183 if ( ! CheckEnabled ( ) ) return ;
164184
165- if ( _isPanning && e . ChangedButton == MouseButton . Left )
185+ if ( _isPanning && e . ChangedButton == System . Windows . Input . MouseButton . Left )
166186 {
167187 try
168188 {
169189 _isPanning = false ;
170190 _targetElement . ReleaseMouseCapture ( ) ;
171- Application . Current . MainWindow . Cursor = System . Windows . Input . Cursors . Arrow ;
191+
192+ // 修复:恢复光标
193+ var window = Window . GetWindow ( _targetElement ) ;
194+ if ( window != null )
195+ {
196+ window . Cursor = System . Windows . Input . Cursors . Arrow ;
197+ }
198+
172199 e . Handled = true ;
173200 Console . WriteLine ( $ "结束拖拽: 最终平移=({ _panTransform . X : F1} , { _panTransform . Y : F1} )") ;
174201 }
@@ -182,18 +209,18 @@ public void HandleMouseUp(MouseButtonEventArgs e, DrawingManager.ToolMode curren
182209 /// <summary>
183210 /// 鼠标滚轮事件处理
184211 /// </summary>
185- public void HandleMouseWheel ( MouseWheelEventArgs e , DrawingManager . ToolMode currentMode , UIElement zoomContainer )
212+ public void HandleMouseWheel ( System . Windows . Input . MouseWheelEventArgs e , DrawingManager . ToolMode currentMode , UIElement zoomContainer )
186213 {
187214 if ( ! CheckEnabled ( ) ) return ;
188215
189216 if ( currentMode == DrawingManager . ToolMode . Move )
190217 {
191- Point mousePos = e . GetPosition ( zoomContainer ) ;
218+ System . Windows . Point mousePos = e . GetPosition ( zoomContainer ) ;
192219 double zoomFactor = e . Delta > 0 ? 1.1 : 0.9 ;
193220 double newZoom = _currentZoom * zoomFactor ;
194221 newZoom = Math . Max ( 0.1 , Math . Min ( 10 , newZoom ) ) ;
195222
196- Point relative = new Point (
223+ System . Windows . Point relative = new System . Windows . Point (
197224 ( mousePos . X - _panTransform . X ) / _currentZoom ,
198225 ( mousePos . Y - _panTransform . Y ) / _currentZoom ) ;
199226
@@ -212,25 +239,30 @@ public void HandleMouseWheel(MouseWheelEventArgs e, DrawingManager.ToolMode curr
212239 /// <summary>
213240 /// 手势操作开始
214241 /// </summary>
215- public void HandleManipulationStarting ( ManipulationStartingEventArgs e , DrawingManager . ToolMode currentMode )
242+ public void HandleManipulationStarting ( System . Windows . Input . ManipulationStartingEventArgs e , DrawingManager . ToolMode currentMode )
216243 {
217244 if ( ! CheckEnabled ( ) ) return ;
218245
219246 if ( currentMode == DrawingManager . ToolMode . Move )
220247 {
221- e . ManipulationContainer = Application . Current . MainWindow ;
222- e . Mode = ManipulationModes . Scale | ManipulationModes . Translate ;
248+ // 修复:使用目标元素的窗口作为操作容器
249+ var window = Window . GetWindow ( _targetElement ) ;
250+ if ( window != null )
251+ {
252+ e . ManipulationContainer = window ;
253+ }
254+ e . Mode = System . Windows . Input . ManipulationModes . Scale | System . Windows . Input . ManipulationModes . Translate ;
223255 }
224256 else
225257 {
226- e . Mode = ManipulationModes . None ;
258+ e . Mode = System . Windows . Input . ManipulationModes . None ;
227259 }
228260 }
229261
230262 /// <summary>
231263 /// 手势操作处理
232264 /// </summary>
233- public void HandleManipulationDelta ( ManipulationDeltaEventArgs e , DrawingManager . ToolMode currentMode , UIElement container )
265+ public void HandleManipulationDelta ( System . Windows . Input . ManipulationDeltaEventArgs e , DrawingManager . ToolMode currentMode , UIElement container )
234266 {
235267 if ( ! CheckEnabled ( ) ) return ;
236268 if ( currentMode != DrawingManager . ToolMode . Move ) return ;
@@ -240,10 +272,22 @@ public void HandleManipulationDelta(ManipulationDeltaEventArgs e, DrawingManager
240272 // 处理缩放
241273 if ( delta . Scale . X != 1.0 || delta . Scale . Y != 1.0 )
242274 {
243- Point center = e . ManipulationOrigin ;
244- Point relativeCenter = container . TranslatePoint ( center , Application . Current . MainWindow ) ;
275+ System . Windows . Point center = e . ManipulationOrigin ;
276+
277+ // 修复:使用目标元素的窗口作为参考
278+ var window = Window . GetWindow ( _targetElement ) ;
279+ System . Windows . Point relativeCenter ;
280+
281+ if ( window != null )
282+ {
283+ relativeCenter = container . TranslatePoint ( center , window ) ;
284+ }
285+ else
286+ {
287+ relativeCenter = center ;
288+ }
245289
246- Point relative = new Point (
290+ System . Windows . Point relative = new System . Windows . Point (
247291 ( relativeCenter . X - _panTransform . X ) / _currentZoom ,
248292 ( relativeCenter . Y - _panTransform . Y ) / _currentZoom ) ;
249293
0 commit comments