@@ -147,6 +147,125 @@ public WpfCursor CreateColoredPencilCursor(string tipColorHex)
147147 }
148148 }
149149
150+ /// <summary>
151+ /// Creates a crosshair cursor with color indicator for the Line tool
152+ /// </summary>
153+ /// <param name="colorHex">Hex color for the line (e.g., "#FF0000")</param>
154+ /// <returns>Custom cursor</returns>
155+ public WpfCursor CreateLineCursor ( string colorHex )
156+ {
157+ lock ( _cursorLock )
158+ {
159+ if ( _disposed )
160+ {
161+ _logger . LogWarning ( "CreateLineCursor called on disposed CursorHelper" ) ;
162+ return WpfCursors . Cross ;
163+ }
164+
165+ try
166+ {
167+ _logger . LogDebug ( "Creating line cursor with color {Color}" , colorHex ) ;
168+
169+ // Destroy previous cursor handle to prevent leaks
170+ if ( _currentCursorHandle != nint . Zero )
171+ {
172+ try
173+ {
174+ DestroyCursor ( _currentCursorHandle ) ;
175+ _logger . LogDebug ( "Destroyed previous cursor handle" ) ;
176+ }
177+ catch ( Exception ex )
178+ {
179+ _logger . LogError ( ex , "Failed to destroy previous cursor handle" ) ;
180+ }
181+ _currentCursorHandle = nint . Zero ;
182+ }
183+
184+ // Create a bitmap for the cursor (32x32 pixels)
185+ int size = 32 ;
186+ using ( Bitmap bitmap = new Bitmap ( size , size ) )
187+ using ( Graphics g = Graphics . FromImage ( bitmap ) )
188+ {
189+ g . SmoothingMode = SmoothingMode . AntiAlias ;
190+ g . Clear ( Color . Transparent ) ;
191+
192+ // Parse the line color
193+ Color lineColor = ColorTranslator . FromHtml ( colorHex ) ;
194+
195+ // Draw two circles with a line connecting them
196+ // Left circle is at the hotspot (where the mouse clicks)
197+ int circleRadius = 4 ;
198+ int circleSpacing = 20 ; // Increased spacing for better visibility
199+
200+ // Position left circle at the hotspot (6 pixels from left edge for visual balance)
201+ Point leftCircleCenter = new Point ( 6 , size / 2 ) ;
202+ Point rightCircleCenter = new Point ( 6 + circleSpacing , size / 2 ) ;
203+
204+ // Draw connecting line with the active color
205+ using ( Pen linePen = new Pen ( lineColor , 2 ) )
206+ {
207+ g . DrawLine ( linePen , leftCircleCenter , rightCircleCenter ) ;
208+ }
209+
210+ // Draw left circle (outline) - this is where the line starts
211+ using ( Pen circlePen = new Pen ( Color . White , 2 ) )
212+ {
213+ g . DrawEllipse ( circlePen ,
214+ leftCircleCenter . X - circleRadius ,
215+ leftCircleCenter . Y - circleRadius ,
216+ circleRadius * 2 ,
217+ circleRadius * 2 ) ;
218+ }
219+ using ( Pen circleOutline = new Pen ( Color . Black , 1 ) )
220+ {
221+ g . DrawEllipse ( circleOutline ,
222+ leftCircleCenter . X - circleRadius - 1 ,
223+ leftCircleCenter . Y - circleRadius - 1 ,
224+ circleRadius * 2 + 2 ,
225+ circleRadius * 2 + 2 ) ;
226+ }
227+
228+ // Draw right circle (outline)
229+ using ( Pen circlePen = new Pen ( Color . White , 2 ) )
230+ {
231+ g . DrawEllipse ( circlePen ,
232+ rightCircleCenter . X - circleRadius ,
233+ rightCircleCenter . Y - circleRadius ,
234+ circleRadius * 2 ,
235+ circleRadius * 2 ) ;
236+ }
237+ using ( Pen circleOutline = new Pen ( Color . Black , 1 ) )
238+ {
239+ g . DrawEllipse ( circleOutline ,
240+ rightCircleCenter . X - circleRadius - 1 ,
241+ rightCircleCenter . Y - circleRadius - 1 ,
242+ circleRadius * 2 + 2 ,
243+ circleRadius * 2 + 2 ) ;
244+ }
245+
246+ // Convert bitmap to cursor with hotspot at the left circle center
247+ nint hCursor = CreateCursorFromBitmap ( bitmap , leftCircleCenter . X , leftCircleCenter . Y ) ;
248+
249+ if ( hCursor != nint . Zero )
250+ {
251+ _currentCursorHandle = hCursor ;
252+ _logger . LogDebug ( "Successfully created line cursor (handle: {Handle})" , hCursor ) ;
253+
254+ return System . Windows . Interop . CursorInteropHelper . Create ( new SafeCursorHandle ( hCursor ) ) ;
255+ }
256+ }
257+
258+ _logger . LogWarning ( "Failed to create line cursor, returning default" ) ;
259+ return WpfCursors . Cross ;
260+ }
261+ catch ( Exception ex )
262+ {
263+ _logger . LogError ( ex , "Error creating line cursor, using default" ) ;
264+ return WpfCursors . Cross ;
265+ }
266+ }
267+ }
268+
150269 private nint CreateCursorFromBitmap ( Bitmap bitmap , int hotspotX , int hotspotY )
151270 {
152271 nint hIcon = nint . Zero ;
0 commit comments