private void MergeFile(WindowControl fileDialog, string path) { var buttonOpen = new NativeButton(fileDialog.IdentifyFromWindowText("開く(&O)")); var comboBoxMergeOrderSrc = fileDialog.GetFromWindowClass("ComboBox").OrderBy(e => { RECT rc; GetWindowRect(e.Handle, out rc); return(rc.Top); }).Last(); var comboBoxMergeOrder = new NativeComboBox(comboBoxMergeOrderSrc); comboBoxMergeOrder.EmulateSelectItem(1); var editPathSrc = fileDialog.GetFromWindowClass("Edit").OrderBy(e => { RECT rc; GetWindowRect(e.Handle, out rc); return(rc.Left); }).Last(); var editPath = new NativeEdit(editPathSrc); editPath.EmulateChangeText(path); buttonOpen.EmulateClick(); }
static void Main(string[] args) { // プロセスの取得 Process[] ps = Process.GetProcessesByName("VoiceroidEditor"); if (ps.Length == 0) { Console.Error.WriteLine("VOICEROID2を起動してください"); return; } // WindowsAppFriendをプロセスから作成する // 接続できない旨のエラーの場合、別のプロセスでテスト対象のプロセスを操作している場合がある。 // TestAssistant使いながら動作できないようなので、注意。 var app = new WindowsAppFriend(ps[0]); var mainWindow = WindowControl.FromZTop(app); // 茜ちゃんしゃべる WPFTextBox txtMessage = new WPFTextBox(mainWindow.IdentifyFromLogicalTreeIndex(0, 4, 3, 5, 3, 0, 2)); txtMessage.EmulateChangeText("アカネチャンカワイイヤッタ"); WPFButtonBase btnPlay = new WPFButtonBase(mainWindow.IdentifyFromLogicalTreeIndex(0, 4, 3, 5, 3, 0, 3, 0)); btnPlay.EmulateClick(); // ステータスバーを監視してしゃべり終わるまでまつ String sts; do { System.Threading.Thread.Sleep(500); var txtStatusItem = mainWindow.IdentifyFromVisualTreeIndex(0, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0).Dynamic();; sts = txtStatusItem.Text.ToString(); } while (!sts.Equals("テキストの読み上げは完了しました。")); // 保存ボタン押下 // ダイアログが表示されると引数なしのEmulateClickだと止まるのでAsyncオブジェクトを渡しておく var async = new Async(); WPFButtonBase btnSave = new WPFButtonBase(mainWindow.IdentifyFromLogicalTreeIndex(0, 4, 3, 5, 3, 0, 3, 5)); btnSave.EmulateClick(async); // 音声保存ダイアログ操作 var dlgSaveWav = mainWindow.WaitForNextModal(); var asyncSaveWin = new Async(); WPFButtonBase buttonOK = new WPFButtonBase(dlgSaveWav.IdentifyFromLogicalTreeIndex(0, 1, 0)); buttonOK.EmulateClick(asyncSaveWin); // ファイル名指定後の保存 var asyncSaveFile = new Async(); var dlgFileSave = dlgSaveWav.WaitForNextModal(); NativeEdit editFileName = new NativeEdit(dlgFileSave.IdentifyFromZIndex(11, 0, 4, 0, 0)); editFileName.EmulateChangeText(System.DateTime.Now.ToString("yyyymMMddhhmmss") + ".wav"); NativeButton btnSaveOk = new NativeButton(dlgFileSave.IdentifyFromDialogId(1)); btnSaveOk.EmulateClick(asyncSaveFile); // 情報ダイアログが表示されるまで待機してOKを押下 var dlgInfo = WindowControl.WaitForIdentifyFromWindowText(app, "情報"); NativeButton btn = new NativeButton(dlgInfo.IdentifyFromWindowText("OK")); btn.EmulateClick(); //非同期で実行した保存ボタン押下の処理が完全に終了するのを待つ asyncSaveFile.WaitForCompletion(); asyncSaveWin.WaitForCompletion(); async.WaitForCompletion(); // 葵ちゃんに切り替えてしゃべる // UIAutomationだと葵ちゃん切り替えが行えない。 WPFListView ListView = new WPFListView(mainWindow.IdentifyFromLogicalTreeIndex(0, 4, 3, 3, 0, 1, 0, 2)); ListView.EmulateChangeSelectedIndex(1); txtMessage.EmulateChangeText("オネエチャンカワイイヤッタ"); btnPlay.EmulateClick(); ListView.EmulateChangeSelectedIndex(0); }
public async Task Save(string msg, string fileName) { await Task.Run(async() => { try { //フォーカスしないようにする //ShowWindow(mainWindowHandle.ToInt32(), SW_HIDE); await Task.Delay(100); //再生完了待機 while (!saveButton.IsEnabled) { Console.WriteLine("saveBtn is not enabled"); await Task.Delay(100); } talkTextBox.EmulateChangeText(msg); var async = new Async(); saveButton.EmulateClick(async); //名前を付けて保存ダイアログ var saveFileWindow = uiTreeTop.WaitForNextModal(); var saveFileDialog = new NativeMessageBox(saveFileWindow); //ファイル名を入力 //右上の検索欄にも入力されてしまうが無視 var edits = saveFileDialog.Window.GetFromWindowClass("Edit"); foreach (var t in edits) { var edit = new NativeEdit(t); edit.EmulateChangeText(fileName); } saveFileDialog.EmulateButtonClick("保存(&S)"); //saveFileWindow.WaitForDestroy(); //出力状況を表示するダイアログの表示を待つ Console.WriteLine("waiting for showing progress window"); var progressWindow = uiTreeTop.WaitForNextModal(); if (progressWindow == null) { progressWindow = uiTreeTop.WaitForNextModal(); } Console.WriteLine("showed " + progressWindow.GetWindowText()); var tokenSource = new CancellationTokenSource(); var task = new TaskFactory().StartNew(() => { //完了通知ダイアログの表示を待つ Console.WriteLine("wationg for showing saving complete window"); var completeWindow = progressWindow.WaitForNextModal(); if (completeWindow != null) { Console.WriteLine("showed " + completeWindow.GetWindowText()); Console.WriteLine(DateTime.Now); try { var completeDialog = new NativeMessageBox(completeWindow); completeDialog.EmulateButtonClick("OK"); Console.WriteLine("wating for destroying"); completeWindow.WaitForDestroy(); Console.WriteLine("finish"); } catch (Exception e) { Console.WriteLine(e); } } }, tokenSource.Token); try { Console.WriteLine(DateTime.Now); if (!task.Wait(5000)) { tokenSource.Cancel(); Console.WriteLine("timeout"); Console.WriteLine(DateTime.Now); var windows = WindowControl.GetTopLevelWindows(uiTreeTop.App); foreach (var window in windows) { Console.WriteLine(window.GetWindowText()); var btnList = window.LogicalTree().ByType <Button>(); var count = btnList.Count; for (int i = 0; i < count; i++) { var btn = new WPFButtonBase(btnList[i]); var btnTxtList = btn.LogicalTree(TreeRunDirection.Descendants).ByType <TextBlock>(); if (btnTxtList.Count == 1) { var btnTxt = new WPFTextBlock(btnTxtList.Single()); Console.WriteLine(btnTxt.Text); if (btnTxt.Text.Equals("キャンセル")) { btn.EmulateClick(); } } } } var completeWindow = progressWindow.WaitForNextModal(); Console.WriteLine("showed2 " + completeWindow.GetWindowText()); Console.WriteLine("2" + DateTime.Now); var completeDialog = new NativeMessageBox(completeWindow); completeDialog.EmulateButtonClick("OK"); Console.WriteLine("wating for destroying2"); completeWindow.WaitForDestroy(); Console.WriteLine("finish2"); } } catch (AggregateException) { //タスクがキャンセルされた Console.WriteLine("task was canceled"); var completeWindow = WindowControl.FromZTop(uiTreeTop.App); Console.WriteLine("showed3 " + completeWindow.GetWindowText()); var completeDialog = new NativeMessageBox(completeWindow); completeDialog.EmulateButtonClick("OK"); Console.WriteLine("wating for destroying3"); completeWindow.WaitForDestroy(); Console.WriteLine("finish3"); } if (!async.IsCompleted) { try { Console.WriteLine("wating for async finish"); async.WaitForCompletion(); } catch (Exception e) { Console.WriteLine(e); } } } finally { //ShowWindow(mainWindowHandle.ToInt32(), SW_MINIMIZE); Console.WriteLine("complete saving"); } }); }
/// <summary> /// VOICEROID2 に入力された文字列を保存します /// </summary> private void Save(string outputFilePath) { // 「音声保存」ボタン押下 WPFButtonBase saveButton = new WPFButtonBase(_root.IdentifyFromLogicalTreeIndex(0, 4, 3, 5, 3, 0, 3, 5)); saveButton.EmulateClick(new Async()); Application.DoEvents(); // 音声保存設定ダイアログの OK ボタン押下 var saveWindow = WindowControl.GetFromWindowText(_app, "音声保存")[0]; WPFButtonBase saveOkButton = new WPFButtonBase(saveWindow.IdentifyFromLogicalTreeIndex(0, 1, 0)); saveOkButton.EmulateClick(new Async()); Application.DoEvents(); // 「名前を付けて保存」ダイアログで出力ファイル名を入力して OK ボタン押下 WindowControl saveDialog = WindowControl.WaitForIdentifyFromWindowText(_app, "名前を付けて保存"); NativeButton saveDialogButton = new NativeButton(saveDialog.IdentifyFromDialogId(1)); NativeEdit saveFileNameTextBox = new NativeEdit(saveDialog.IdentifyFromZIndex(11, 0, 4, 0, 0)); saveFileNameTextBox.EmulateChangeText(outputFilePath); Application.DoEvents(); saveDialogButton.EmulateClick(new Async()); Application.DoEvents(); // 上書き確認ダイアログが出てきたら、 OK ボタンを押下 // 出てくる前提で作っているけど、なぜか出ない場合でも問題なく動いているっぽい。 while (true) { try { WindowControl saveConfirmDialog = WindowControl.IdentifyFromWindowText(_app, "名前を付けて保存"); NativeButton saveConfirmDialogButton = new NativeButton(saveConfirmDialog.IdentifyFromDialogId(6)); saveConfirmDialogButton.EmulateClick(new Async()); Application.DoEvents(); } catch (WindowIdentifyException e) { // do nothing } // 保存成功通知ダイアログの OK ボタンを押下 try { WindowControl infoDialog = WindowControl.IdentifyFromWindowText(_app, "情報"); NativeButton infoDialogButton = new NativeButton(infoDialog.IdentifyFromDialogId(2)); infoDialogButton.EmulateClick(new Async()); Application.DoEvents(); break; } catch (WindowIdentifyException e) { // do nothing } Thread.Sleep(100); } // おまじない // 再生の時もこれやっているので、何か意味があるのだろう。 _isPlaying = false; _isRunning = false; _timer.Start(); }