/// <summary> /// 处理控制键按下的事件,包括功能键、删除键、编辑键的功能实现 /// </summary> /// <param name="sender">事件触发者</param> /// <param name="e">事件参数</param> private void TextCtrl_KeyDown(object sender, KeyEventArgs e) { lastKeyHandled = true; if (e.KeyCode == Keys.Up) { cursorRelativePos.y--; if (!ValidateCaretPos()) { SystemSounds.Beep.Play(); } UpdateCaretPos(); ArrowKeyEndProc(); } else if (e.KeyCode == Keys.Down) { cursorRelativePos.y++; if (!ValidateCaretPos()) { SystemSounds.Beep.Play(); } UpdateCaretPos(); ArrowKeyEndProc(); } else if (e.KeyCode == Keys.Left) { CaretStep(false); } else if (e.KeyCode == Keys.Right) { CaretStep(true); } else if (e.KeyCode == Keys.Home) { cursorRelativePos.x = viewAbsolutePos.x = 0; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.End) { viewAbsolutePos.x = 0; cursorRelativePos.x = lineMaxLen; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.PageUp) { cursorRelativePos.y -= txtFieldSize.Height; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.PageDown) { cursorRelativePos.y += txtFieldSize.Height; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.C && e.Control) { CopySelection(); } else if (e.KeyCode == Keys.V && e.Control) { Paste(); } else if (e.KeyCode == Keys.F && e.Control) { RequestFind(this); } else if (e.KeyCode == Keys.S && e.Control) { SaveFile(); } else if (e.KeyCode == Keys.A && e.Control) { selectionStartPos.x = selectionStartPos.y = 0; selectionEndPos.y = texts.Length - 1; selectionEndPos.x = sizes[texts.Length - 1]; isSelecting = true; UpdateSelection(); } else if (e.KeyCode == Keys.D && e.Control) { CoreMethods.RemoveLine(cursorRelativePos.y + viewAbsolutePos.y); UpdateText(); ValidateCaretPos(); UpdateCaretPos(); } else if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) { if (isSelecting) { if (!CoreMethods.Remove(ref selectionStartPos, ref selectionEndPos)) { if (DebugMsgSend != null) { DebugMsgSend("::Remove failed!"); } } ScrollTo(selectionStartPos); ClearSelection(); UpdateText(); } else { if (e.KeyCode == Keys.Delete && !CaretStep(true, true)) { return; } CPosition tmp = cursorRelativePos + viewAbsolutePos; if (!CoreMethods.Backspace(ref tmp)) { if (DebugMsgSend != null) { DebugMsgSend("::Backspace failed!"); } } CaretStep(false, true); tmp = cursorRelativePos + viewAbsolutePos; UpdateText(); UpdateScrollBars(); cursorRelativePos = tmp - viewAbsolutePos; ValidateCaretPos(); UpdateCaretPos(); } } else { lastKeyHandled = false; } ctrlState = e.Control; }
/// <summary> /// 保存文件 /// </summary> /// <param name="path">文件路径,为空则为默认路径</param> /// <returns>保存是否成功</returns> public bool SaveFile(string path = "") { return(CoreMethods.Save(path)); }