fasdfsa commited on
Commit
9292899
·
1 Parent(s): 5bdd3af

右键选中区域不清空选中状态

Browse files
Files changed (2) hide show
  1. data/0022.jpg +3 -0
  2. src/WpfEditor/TextEditorControl.cs +178 -7
data/0022.jpg ADDED

Git LFS Details

  • SHA256: 1886349b012f1518e38a3c20f7f817df534666dc255de133e292e05ff97eef01
  • Pointer size: 131 Bytes
  • Size of remote file: 372 kB
src/WpfEditor/TextEditorControl.cs CHANGED
@@ -247,6 +247,79 @@ namespace WpfEditor
247
  new PropertyMetadata(true));
248
 
249
  #endregion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
  #region 属性
252
 
@@ -328,6 +401,10 @@ namespace WpfEditor
328
  CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo));
329
  CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, OnRedo));
330
  CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, OnSelectAll));
 
 
 
 
331
  }
332
 
333
  #endregion
@@ -480,9 +557,34 @@ namespace WpfEditor
480
  }
481
  }
482
 
483
- protected override void OnMouseDown(MouseButtonEventArgs e)
484
- {
485
- base.OnMouseDown(e);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
  Focus();
487
  Point position = e.GetPosition(this);
488
  SetCursorPositionFromPoint(position);
@@ -493,16 +595,40 @@ namespace WpfEditor
493
  else
494
  {
495
  UpdateSelection();
496
- }
497
-
498
- // 图片中框选光标后面的第一个字符
499
  if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Pressed)
500
  {
501
  showCurrChar();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  }
503
 
504
  e.Handled = true;
505
- InvalidateVisual();
506
  }
507
 
508
  protected override void OnMouseMove(MouseEventArgs e)
@@ -1104,6 +1230,51 @@ namespace WpfEditor
1104
 
1105
  return true;
1106
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1107
  private void SetCursorPositionFromPoint(Point point)
1108
  {
1109
  // 计算字体度量
 
247
  new PropertyMetadata(true));
248
 
249
  #endregion
250
+
251
+ // 创建右键菜单的方法
252
+ private void CreateContextMenu()
253
+ {
254
+ ContextMenu = new ContextMenu();
255
+
256
+ // 复制菜单项
257
+ MenuItem copyMenuItem = new MenuItem
258
+ {
259
+ Header = "复制",
260
+ Command = ApplicationCommands.Copy
261
+ };
262
+ ContextMenu.Items.Add(copyMenuItem);
263
+
264
+ // 剪切菜单项
265
+ MenuItem cutMenuItem = new MenuItem
266
+ {
267
+ Header = "剪切",
268
+ Command = ApplicationCommands.Cut
269
+ };
270
+ ContextMenu.Items.Add(cutMenuItem);
271
+
272
+ // 粘贴菜单项
273
+ MenuItem pasteMenuItem = new MenuItem
274
+ {
275
+ Header = "粘贴",
276
+ Command = ApplicationCommands.Paste
277
+ };
278
+ ContextMenu.Items.Add(pasteMenuItem);
279
+
280
+ // 分隔符
281
+ ContextMenu.Items.Add(new Separator());
282
+
283
+ // 全选菜单项
284
+ MenuItem selectAllMenuItem = new MenuItem
285
+ {
286
+ Header = "全选",
287
+ Command = ApplicationCommands.SelectAll
288
+ };
289
+ ContextMenu.Items.Add(selectAllMenuItem);
290
+
291
+ // 设置菜单打开前的事件处理,用于控制菜单项的可用性
292
+ ContextMenu.Opened += OnContextMenuOpened;
293
+ }
294
+
295
+ // 右键菜单打开时的事件处理
296
+ private void OnContextMenuOpened(object sender, RoutedEventArgs e)
297
+ {
298
+ bool hasSelection = HasSelection();
299
+ bool hasClipboardText = Clipboard.ContainsText();
300
+
301
+ // 根据当前状态设置菜单项的可用性
302
+ foreach (var item in ContextMenu.Items)
303
+ {
304
+ if (item is MenuItem menuItem)
305
+ {
306
+ if (menuItem.Command == ApplicationCommands.Copy ||
307
+ menuItem.Command == ApplicationCommands.Cut)
308
+ {
309
+ menuItem.IsEnabled = hasSelection;
310
+ }
311
+ else if (menuItem.Command == ApplicationCommands.Paste)
312
+ {
313
+ menuItem.IsEnabled = !IsReadOnly && hasClipboardText;
314
+ }
315
+ else if (menuItem.Command == ApplicationCommands.SelectAll)
316
+ {
317
+ menuItem.IsEnabled = _lines.Count > 0 &&
318
+ _lines.Any(line => line.Characters.Count > 0);
319
+ }
320
+ }
321
+ }
322
+ }
323
 
324
  #region 属性
325
 
 
401
  CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo));
402
  CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, OnRedo));
403
  CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, OnSelectAll));
404
+
405
+ // 创建右键菜单
406
+ CreateContextMenu();
407
+
408
  }
409
 
410
  #endregion
 
557
  }
558
  }
559
 
560
+ //protected override void OnMouseDown(MouseButtonEventArgs e)
561
+ //{
562
+ // base.OnMouseDown(e);
563
+ // Focus();
564
+ // Point position = e.GetPosition(this);
565
+ // SetCursorPositionFromPoint(position);
566
+ // if (Keyboard.Modifiers != ModifierKeys.Shift)
567
+ // {
568
+ // //ClearSelection();
569
+ // }
570
+ // else
571
+ // {
572
+ // UpdateSelection();
573
+ // }
574
+
575
+ // // 图片中框选光标后面的第一个字符
576
+ // if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Pressed)
577
+ // {
578
+ // showCurrChar();
579
+ // }
580
+
581
+ // e.Handled = true;
582
+ // InvalidateVisual();
583
+ //}
584
+
585
+ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
586
+ {
587
+ base.OnMouseLeftButtonDown(e);
588
  Focus();
589
  Point position = e.GetPosition(this);
590
  SetCursorPositionFromPoint(position);
 
595
  else
596
  {
597
  UpdateSelection();
598
+ }
599
+
600
+ // 图片中框选光标后面的第一个字符
601
  if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Pressed)
602
  {
603
  showCurrChar();
604
+ }
605
+
606
+ e.Handled = true;
607
+ InvalidateVisual();
608
+ }
609
+
610
+ // 重写右键按下事件
611
+ protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
612
+ {
613
+ base.OnMouseRightButtonDown(e);
614
+
615
+ // 设置焦点
616
+ Focus();
617
+
618
+ // 获取鼠标位置并设置光标位置
619
+ Point position = e.GetPosition(this);
620
+
621
+ // 如果右键点击的位置不在当前选择区域内,则移动光标到点击位置
622
+ // 但不清除选择区域,这样在选中文本上右键点击时不会丢失选择
623
+ if (!IsPositionInSelection(position))
624
+ {
625
+ //SetCursorPositionFromPoint(position);
626
+ // 移除以下行,不再清除选择区域
627
+ // ClearSelection();
628
+ InvalidateVisual();
629
  }
630
 
631
  e.Handled = true;
 
632
  }
633
 
634
  protected override void OnMouseMove(MouseEventArgs e)
 
1230
 
1231
  return true;
1232
  }
1233
+
1234
+ // 检查位置是否在选择区域内的辅助方法
1235
+ private bool IsPositionInSelection(Point position)
1236
+ {
1237
+ if (!HasSelection())
1238
+ return false;
1239
+
1240
+ // 计算字体度量
1241
+ var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
1242
+ var formattedText = new FormattedText("M", System.Globalization.CultureInfo.CurrentCulture,
1243
+ FlowDirection.LeftToRight, typeface, FontSize, Brushes.Black, VisualTreeHelper.GetDpi(this).PixelsPerDip);
1244
+ double charWidth = formattedText.Width;
1245
+ double lineHeight = formattedText.Height;
1246
+
1247
+ // 计算点击位置对应的行列
1248
+ int clickLine = Math.Max(0, Math.Min((int)(position.Y / lineHeight) + _scrollOffset, _lines.Count - 1));
1249
+ int clickColumn = Math.Max(0, (int)((position.X - 50) / charWidth)); // 50是行号区域宽度
1250
+
1251
+ // 获取选择区域的起始和结束位置
1252
+ int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
1253
+ int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
1254
+ int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
1255
+ int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
1256
+
1257
+ // 检查点击位置是否在选择区域内
1258
+ if (clickLine < startLine || clickLine > endLine)
1259
+ return false;
1260
+
1261
+ if (clickLine == startLine && clickLine == endLine)
1262
+ {
1263
+ return clickColumn >= startColumn && clickColumn <= endColumn;
1264
+ }
1265
+ else if (clickLine == startLine)
1266
+ {
1267
+ return clickColumn >= startColumn;
1268
+ }
1269
+ else if (clickLine == endLine)
1270
+ {
1271
+ return clickColumn <= endColumn;
1272
+ }
1273
+ else
1274
+ {
1275
+ return true; // 在中间行
1276
+ }
1277
+ }
1278
  private void SetCursorPositionFromPoint(Point point)
1279
  {
1280
  // 计算字体度量