private void StartProcess(string title) { try { windowHandler = new WindowHandler(); filePath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Substring(8)), title); if (File.Exists(filePath)) { File.Delete(filePath); } Process vscp = new Process(); vscp.StartInfo.FileName = vsCodePath; vscp.StartInfo.Arguments = "-n \"" + filePath + "\""; vscp.Start(); // 等待visual studio code 启动, 不然标题不会显示文件名 do { Thread.Sleep(500); } while (!windowHandler.SetWindow( "Chrome_WidgetWin_1", title + " - Visual Studio Code")); IsInitialed = true; } catch (Exception err) { MessageBox.Show("Error while start visual studio code : \n" + err.ToString()); } }
/// <summary> /// 关闭编辑器. /// </summary> public void Close() { if (windowHandler != null && !windowHandler.IsWindowClosed()) { windowHandler.ActiveWindow(); SendKeys.SendWait("^(s)"); Thread.Sleep(500); SendKeys.SendWait("^(w)"); Thread.Sleep(500); windowHandler.CloseWindow(); windowHandler = null; File.Delete(filePath); } }
/// <summary> /// 设置编辑器的代码格式 /// </summary> /// <param name="codeStyle">代码格式</param> public void ChangeLanguageMode(string codeStyle) { bool isVisible = windowHandler.IsWindowVisible(); if (!isVisible) { windowHandler.ActiveWindow(); } // 避免输入法影响 bool isCapsPressed = WindowHandler.IsCapsLockPressed(); if (!isCapsPressed) { WindowHandler.SendCapsLock(); } SendKeys.SendWait("^(k)m"); Thread.Sleep(350); SendKeys.SendWait($"{codeStyle.ToUpper()}"); Thread.Sleep(150); SendKeys.SendWait("\n"); Thread.Sleep(150); // 发送esc 键, 防止出现不识别的语言导致命令窗一直存在 SendKeys.SendWait("{ESC}"); string nowTitle = windowHandler.GetWindowTitle(); if (nowTitle.Contains("settings.json")) { SendKeys.SendWait("^(w)"); MessageBox.Show(codeStyle + " configuration not exist!"); } if (!isCapsPressed) { WindowHandler.SendCapsLock(); } if (!isVisible) { windowHandler.DeactiveWindow(); } }