/// <summary>
        /// <see cref="GetEditStyle(ITypeDescriptorContext)"/> メソッドで指定された
        /// エディタースタイルを使用して、指定したオブジェクトの値を編集する
        /// </summary>
        /// <param name="context">
        /// 追加のコンテキスト情報を取得するために使用する <see cref="ITypeDescriptorContext"/> オブジェクト
        /// </param>
        /// <param name="provider">
        /// エディターがサービスを取得するために使用する <see cref="IServiceProvider"/> オブジェクト
        /// </param>
        /// <param name="value">
        /// 編集対象のオブジェクト
        /// </param>
        /// <returns>
        /// オブジェクトの新しい値
        /// オブジェクトの値が変更されない場合は、引数の<paramref name="value"/>オブジェクトをそのまま返す
        /// </returns>
        public override object EditValue(
            ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            // 編集対象のオブジェクトの型チェック
            if (!(value is SizePoint sizePointValue))
            {
                // 対象の型でない場合は編集を行わず、引数の値をそのまま返す
                return(value);
            }

            // IWindowsFormsEditorServiceを使用してドロップダウンのUIのプロパティウィンドウを表示する
            if (!(provider?.GetService(
                      typeof(IWindowsFormsEditorService)) is IWindowsFormsEditorService editorService))
            {
                // IWindowsFormsEditorServiceが取得できない場合は編集を行わず、引数の値をそのまま返す
                return(value);
            }

            // エディターのユーザインターフェースを生成しドロップダウンで表示する
            SizePoint       setSizePoint = null;
            SizePointEditor editorUI;

            using (editorUI = new SizePointEditor(sizePointValue, true))
            {
                editorService.DropDownControl(editorUI);
                setSizePoint = editorUI.SettingSizePoint;
            }

            // エディタで設定した値を返却
            // 設定値がNULLの場合は編集を行わず、引数の値をそのまま返す
            return(setSizePoint ?? value);
        }
Esempio n. 2
0
        /// <summary>
        /// デフォルトコンストラクタ
        /// </summary>
        /// <exception cref="SettingException">
        /// 下記の場合に発生
        /// ・引数で指定された名称のプロパティが設定情報(<see cref="Properties.Settings"/>)に存在しない場合
        /// ・デフォルト値を使用する場合において、デフォルト値属性が存在しない場合
        /// ・デフォルト値を使用する場合において、デフォルト値が指定のEnum型に変換できない場合
        /// </exception>
        public Setting()
        {
            // アプリケーション起動の際の起動モード
            StartStartupMode = GetSettingPropertyValue <StartupMode>(
                nameof(Properties.Settings.Default.StartStartupMode));

            // アプリケーション起動の際の入力モード
            StartInputMode = GetSettingPropertyValue <InputMode>(
                nameof(Properties.Settings.Default.StartInputMode));

            // 音声認識で使用するChromeウィンドウのサイズ
            ChromeSizePoint = new SizePoint(Properties.Settings.Default.ChromeSizePoint);

            // キー入力 1~7 の入力キーの設定
            InputKey1 = new InputKey(Properties.Settings.Default.InputKey1);
            InputKey2 = new InputKey(Properties.Settings.Default.InputKey2);
            InputKey3 = new InputKey(Properties.Settings.Default.InputKey3);
            InputKey4 = new InputKey(Properties.Settings.Default.InputKey4);
            InputKey5 = new InputKey(Properties.Settings.Default.InputKey5);
            InputKey6 = new InputKey(Properties.Settings.Default.InputKey6);
            InputKey7 = new InputKey(Properties.Settings.Default.InputKey7);

            // 起動する対象のパス 1~6 の設定
            StartTargetPath1 = Properties.Settings.Default.StartTargetPath1;
            StartTargetPath2 = Properties.Settings.Default.StartTargetPath2;
            StartTargetPath3 = Properties.Settings.Default.StartTargetPath3;
            StartTargetPath4 = Properties.Settings.Default.StartTargetPath4;
            StartTargetPath5 = Properties.Settings.Default.StartTargetPath5;
            StartTargetPath6 = Properties.Settings.Default.StartTargetPath6;
        }
Esempio n. 3
0
        /// <summary>
        /// 音声認識で使用するChromeウィンドウのサイズ、位置の設定を既定値にリセットする
        /// </summary>
        public void ResetChromeSizePoint()
        {
            string name         = nameof(Properties.Settings.Default.ChromeSizePoint);
            string defaultValue = DefaultUserSettings[name];

            ChromeSizePoint = new SizePoint(defaultValue);
        }
Esempio n. 4
0
        /// <summary>
        /// アクティブウィンドウのキャプチャを行う
        /// </summary>
        /// <param name="targetWindowSizePoint">
        /// キャプチャ対象となるアクティブウィンドウのサイズ位置情報を返却
        /// </param>
        /// <exception cref="PlatformInvokeException">
        /// Win32Apiの処理において、処理の呼び出しに失敗した場合に発生
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// Win32Apiの処理において、処理に失敗した場合に発生
        /// </exception>
        /// <exception cref="Win32Exception">
        /// キャプチャデータ取得に用いる <see cref="Graphics.CopyFromScreen(Point, Point, Size, CopyPixelOperation)"/>
        /// において、Win32Apiの操作に失敗した場合に発生
        /// </exception>
        /// <exception cref="Exception">
        /// キャプチャデータを格納する <see cref="Bitmap"/> オブジェクトが生成できない場合に発生
        /// </exception>
        /// <returns>
        /// アクティブウィンドウをキャプチャした画像データ
        /// (キャプチャできない場合は NULL を返却)
        /// </returns>
        private static Bitmap ActiveWindowCapture(out SizePoint targetWindowSizePoint)
        {
            // アクティブウィンドウのサイズと位置を取得
            SizePoint windowSizePoint = null;
            IntPtr    windowHandle    = WindowOperate.GetForegroundWindowHandle();

            if (windowHandle != IntPtr.Zero)
            {
                // ウィンドウハンドルが取得できた場合、
                // ウィンドウサイズ位置、クライアント領域のサイズ位置を取得
                HandleRef handle = new HandleRef(0, windowHandle);
                SizePoint window = WindowOperate.GetWindowRect(handle);
                SizePoint client = WindowOperate.GetClientRect(handle);

                // ウィンドウ全体とクライアント領域のサイズ差から、
                // 左右下のバーの幅を取得し全体のサイズ位置を補正する
                if (window != null && client != null)
                {
                    float barWidth = (window.SizeWidth - client.SizeWidth) / 2;
                    windowSizePoint = new SizePoint(
                        sizeWidth: window.SizeWidth - (int)(barWidth * 2),
                        sizeHeight: window.SizeHeight - (int)Math.Floor(barWidth + 1),
                        positionX: window.PositionX + (int)Math.Floor(barWidth),
                        positionY: window.PositionY + 1);
                }
            }

            // ウィンドウサイズが取得できない場合はディスクトップのサイズ位置を取得する
            if (windowSizePoint == null)
            {
                HandleRef handle = new HandleRef(0, WindowOperate.GetDesktopWindow());
                windowSizePoint = WindowOperate.GetWindowRect(handle);
            }

            // 上記ウィンドウの存在するScreenを取得
            Screen screen;

            if (windowSizePoint != null)
            {
                screen = Screen.FromPoint(windowSizePoint.Point);
            }
            else
            {
                // ウィンドウサイズが取得できない場合は
                // プライマリウィンドウの情報からディスクトップのウィンドウサイズを設定する
                screen          = Screen.PrimaryScreen;
                windowSizePoint = new SizePoint(screen.Bounds.Size, screen.Bounds.Location);
            }

            // ディスクトップの対象領域のキャプチャを取得
            Bitmap captureImage = DesktopWindowCapture(
                windowSizePoint, screen, out SizePoint displayedSizePoint);

            // ウィンドウのサイズ位置 及び、画面情報、画像を返却
            targetWindowSizePoint = displayedSizePoint;
            return(captureImage);
        }
 /// <summary>
 /// Chromeによる音声認識を開始する
 /// </summary>
 /// <param name="htmlFilePath">
 /// 音声認識に使用する既定の「音声認識用.html」ファイルのパス
 /// </param>
 /// <param name="postRegex">
 /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するURLの正規表現
 /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
 /// </param>
 /// <param name="postParamName">
 /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するPOSTデータのパラメータ名
 /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
 /// </param>
 /// <param name="sizePoint">
 /// 音声認識用に起動するChromeの初期のサイズと位置
 /// (NULLを指定した場合はデフォルトのサイズと位置で起動する)
 /// </param>
 /// <param name="responceFunctions">
 /// 音声認識した文字に対する処理の指定(複数指定可、優先順位は上から)
 /// ・引数1  string:音声認識した文字列を渡す
 /// ・戻り値 byte[]:処理を実行した結果返却するデータ
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// 引数の <paramref name="htmlFilePath"/> 又は <paramref name="postRegex"/> がNULLの場合に発生
 /// </exception>
 /// <exception cref="ArgumentException">
 /// 下記の場合に発生する
 /// ・引数の <paramref name="htmlFilePath"/> が下記の場合
 ///  長さ 0 の文字列
 ///  空白のみで構成される
 ///  <see cref="Path.InvalidPathChars"/> で定義される 1 つ以上の正しくない文字を含む
 /// ・引数の <paramref name="postRegex"/> が正規表現として不正な値の場合
 /// </exception>
 /// <exception cref="SpeechRecognitionException">
 /// Chromeが使用不可能な場合に発生
 /// (<see cref="ChromePath"/> の指定が間違ってる 又は、Chromeがインストールされていない場合)
 /// </exception>
 /// <exception cref="IOException">
 /// 下記の場合に発生
 /// ・引数の <paramref name="htmlFilePath"/> がシステム定義の最大長を超えている場合
 ///  [<see cref="PathTooLongException"/>]
 ///  (たとえば、Windowsでは、パスは 248文字未満、ファイル名は 260 文字未満である必要がある)
 /// ・引数の <paramref name="htmlFilePath"/> が存在しないディレクトリを示している場合
 ///  [<see cref="DirectoryNotFoundException"/>]
 /// ・引数の <paramref name="htmlFilePath"/> で指定されたファイルが存在しない場合
 ///  [<see cref="FileNotFoundException"/>]
 /// ・I/O エラーが発生した場合
 ///  [<see cref="IOException"/>]
 /// </exception>
 /// <exception cref="UnauthorizedAccessException">
 /// 引数の <paramref name="htmlFilePath"/> がファイルを指定しないない(ディレクトリを指定)場合、
 /// 又は、呼び出し元に必要なアクセス許可がない場合に発生
 /// </exception>
 /// <exception cref="Win32Exception">
 /// 下記の要因で。指定ファイルを開いているときにエラーが発生した場合に発生
 /// ・指定ファイルへの完全パスの長さと起動パラメータの長さの合計が、2080 文字を超えている場合
 /// ・指定ファイルへのアクセスが拒否された場合
 /// </exception>
 /// <exception cref="PlatformInvokeException">
 /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生
 /// </exception>
 /// <exception cref="Win32OperateException">
 /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生
 /// </exception>
 public static void Start(
     string htmlFilePath,
     string postRegex,
     string postParamName,
     SizePoint sizePoint,
     params Func <string, byte[]>[] responceFunctions)
 {
     // 開始
     Start(htmlFilePath, postRegex, postParamName, sizePoint, null, responceFunctions);
 }
Esempio n. 6
0
        /// <summary>
        /// ディスクトップをキャプチャする
        /// </summary>
        /// <param name="windowSizePoint">
        /// 対象のウィンドウのサイズ位置情報
        /// </param>
        /// <param name="targetScreen">
        /// 対象のウィンドウが存在する画面
        /// </param>
        /// <param name="displayedSizePoint">
        /// 対象のウィンドウにおいて画面に表示している領域の情報を返却
        /// </param>
        /// <exception cref="Win32Exception">
        /// キャプチャデータ取得に用いる <see cref="Graphics.CopyFromScreen(Point, Point, Size, CopyPixelOperation)"/>
        /// において、Win32Apiの操作に失敗した場合に発生
        /// </exception>
        /// <exception cref="Exception">
        /// キャプチャデータを格納する <see cref="Bitmap"/> オブジェクトが生成できない場合に発生
        /// </exception>
        /// <returns>キャプチャした画像データ(キャプチャできない場合は NULL を返却)</returns>
        private static Bitmap DesktopWindowCapture(
            SizePoint windowSizePoint, Screen targetScreen, out SizePoint displayedSizePoint)
        {
            // キャプチャ用のBitmapの変数を宣言
            Bitmap desktopImage = null;

            try
            {
                // 画面に表示されている領域の画面サイズを取得
                SizePoint displayed = GetDisplayedSizePoint(windowSizePoint, targetScreen);
                if (displayed.SizeWidth <= 0 || displayed.SizeHeight <= 0)
                {
                    // 表示領域が存在しない場合は対象スクリーン全体を対象とする
                    displayed = new SizePoint(targetScreen.Bounds.Size, targetScreen.Bounds.Location);
                }

                // キャプチャ用のBitmapを生成
                desktopImage = new Bitmap(displayed.Size.Width, displayed.Size.Height);

                // 画面のキャプチャを取得
                using (Graphics graphics = Graphics.FromImage(desktopImage))
                {
                    // 背景を黒にする
                    graphics.CopyFromScreen(
                        upperLeftSource: new Point(0, 0),
                        upperLeftDestination: new Point(0, 0),
                        blockRegionSize: displayed.Size,
                        copyPixelOperation: CopyPixelOperation.Blackness);

                    // 画面のキャプチャを描画
                    graphics.CopyFromScreen(
                        upperLeftSource: displayed.Point,
                        upperLeftDestination: new Point(0, 0),
                        blockRegionSize: displayed.Size,
                        copyPixelOperation: CopyPixelOperation.SourceCopy);
                }

                // キャプチャした画像データを返却
                displayedSizePoint = displayed;
                return(desktopImage);
            }
            catch (Exception)
            {
                // 例外発生時、キャプチャ用のBitmapオブジェクトを破棄する
                desktopImage?.Dispose();

                // 発生した例外はそのままスロー
                throw;
            }
        }
        /// <summary>
        /// Chromeによる音声認識を開始する
        /// </summary>
        /// <param name="chromePath">
        /// ChromeのEXEファイルの絶対パス
        /// 指定した値は <see cref="ChromePath"/> プロパティに設定する
        /// </param>
        /// <param name="htmlFilePath">
        /// 音声認識に使用する既定の「音声認識用.html」ファイルのパス
        /// </param>
        /// <param name="postRegex">
        /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するURLの正規表現
        /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
        /// </param>
        /// <param name="postParamName">
        /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するPOSTデータのパラメータ名
        /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
        /// </param>
        /// <param name="sizePoint">
        /// 音声認識用に起動するChromeの初期のサイズと位置
        /// (NULLを指定した場合はデフォルトのサイズと位置で起動する)
        /// </param>
        /// <param name="localHttpServerPort">
        /// 音声認識に使用するローカルHTTPサーバのポート番号を指定する場合に設定するポート番号
        /// 指定しない場合(NULLの場合)、または指定したポート番号が使用できない場合は乱数からポート番号を取得し使用する
        /// </param>
        /// <param name="responceFunctions">
        /// 音声認識した文字に対する処理の指定(複数指定可、優先順位は上から)
        /// ・引数1  string:音声認識した文字列を渡す
        /// ・戻り値 byte[]:処理を実行した結果返却するデータ
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// 引数の <paramref name="htmlFilePath"/> 又は <paramref name="postRegex"/> がNULLの場合に発生
        /// </exception>
        /// <exception cref="ArgumentException">
        /// 下記の場合に発生する
        /// ・引数の <paramref name="htmlFilePath"/> が下記の場合
        ///  長さ 0 の文字列
        ///  空白のみで構成される
        ///  <see cref="Path.InvalidPathChars"/> で定義される 1 つ以上の正しくない文字を含む
        /// ・引数の <paramref name="postRegex"/> が正規表現として不正な値の場合
        /// </exception>
        /// <exception cref="SpeechRecognitionException">
        /// Chromeが使用不可能な場合に発生
        /// (<see cref="ChromePath"/> の指定が間違ってる 又は、Chromeがインストールされていない場合)
        /// </exception>
        /// <exception cref="IOException">
        /// 下記の場合に発生
        /// ・引数の <paramref name="htmlFilePath"/> がシステム定義の最大長を超えている場合
        ///  [<see cref="PathTooLongException"/>]
        ///  (たとえば、Windowsでは、パスは 248文字未満、ファイル名は 260 文字未満である必要がある)
        /// ・引数の <paramref name="htmlFilePath"/> が存在しないディレクトリを示している場合
        ///  [<see cref="DirectoryNotFoundException"/>]
        /// ・引数の <paramref name="htmlFilePath"/> で指定されたファイルが存在しない場合
        ///  [<see cref="FileNotFoundException"/>]
        /// ・I/O エラーが発生した場合
        ///  [<see cref="IOException"/>]
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        /// 引数の <paramref name="htmlFilePath"/> がファイルを指定しないない(ディレクトリを指定)場合、
        /// 又は、呼び出し元に必要なアクセス許可がない場合に発生
        /// </exception>
        /// <exception cref="Win32Exception">
        /// 下記の要因で。指定ファイルを開いているときにエラーが発生した場合に発生
        /// ・指定ファイルへの完全パスの長さと起動パラメータの長さの合計が、2080 文字を超えている場合
        /// ・指定ファイルへのアクセスが拒否された場合
        /// </exception>
        /// <exception cref="PlatformInvokeException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生
        /// </exception>
        public static void Start(
            string chromePath,
            string htmlFilePath,
            string postRegex,
            string postParamName,
            SizePoint sizePoint,
            int?localHttpServerPort,
            params Func <string, byte[]>[] responceFunctions)
        {
            // プロパティにChromeのEXEファイルのパスを設定する
            ChromePath = chromePath;

            // 開始
            Start(htmlFilePath, postRegex, postParamName, sizePoint, localHttpServerPort, responceFunctions);
        }
Esempio n. 8
0
        /// <summary>
        /// 引数(<paramref name="windowHandle"/>)のウインドウハンドルを持つウィンドウの
        /// サイズ、位置を変更する
        /// </summary>
        /// <param name="windowHandle">変更対象のウィンドウのハンドル</param>
        /// <param name="sizePoint">設定するウィンドウのサイズと位置</param>
        /// <exception cref="ArgumentNullException">
        /// 引数の <paramref name="sizePoint"/> がNULLの場合に発生
        /// </exception>
        /// <exception cref="PlatformInvokeException">
        /// Win32Apiの下記の処理の呼び出しに失敗した場合に発生
        /// ・「DLL:user32.dll、メソッド:IsWindow」
        /// ・「DLL:user32.dll、メソッド:SetWindowSizeLocation」
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// Win32Apiの下記の処理に失敗した場合に発生
        /// ・「DLL:user32.dll、メソッド:IsWindow」
        /// ・「DLL:user32.dll、メソッド:SetWindowSizeLocation」
        /// </exception>
        public static void SetWindowSizeLocation(HandleRef windowHandle, SizePoint sizePoint)
        {
            // 対象のウィンドウが存在しない場合は処理を終了する
            if (!IsWindow(windowHandle))
            {
                return;
            }

            // NULLチェック
            if (sizePoint == null)
            {
                throw new ArgumentNullException(nameof(sizePoint));
            }

            // ウィンドウを閉じるコマンドメッセージをの定義
            // Zオーダーの配置順序の変更なしを指定
            IntPtr order  = IntPtr.Zero;
            uint   option = (uint)Win32Api.SetWindowPosParameter.OptionFlag.SWP_NOZORDER;

            // Win32Apiの実行処理
            // Win32ApiのWindou共通の呼び出し機能を用いて、ウィンドウのサイズ、位置を変更する
            Win32ApiResult Function()
            {
                // 実行
                bool win32Result = Win32Api.SetWindowPos(
                    windowHandle: windowHandle,
                    windowHandleOrder: order,
                    pointX: sizePoint.PositionX,
                    pointY: sizePoint.PositionY,
                    sizeWidth: sizePoint.SizeWidth,
                    sizeHeight: sizePoint.SizeHeight,
                    optionFlag: option);
                int win32ErrorCode = Marshal.GetLastWin32Error();

                return(new Win32ApiResult(win32Result, win32ErrorCode));
            }

            // 実行
            string         dllName    = "user32.dll";
            string         methodName = nameof(Win32Api.SetWindowPos);
            Win32ApiResult result     = Win32ApiCommon.Run(Function, dllName, methodName);

            // 正常終了したかチェック
            if (!result.Result && result.ErrorCode != (int)ErrorCode.NO_ERROR)
            {
                throw Win32ApiCommon.GetWin32OperateException(dllName, methodName, result.ErrorCode);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// コンストラクタ
        /// 引数の値でこのユーザコントロールを初期化する
        /// </summary>
        /// <param name="currentSizePoint">設定対象とするFormコントロールの現在のサイズと位置を指定</param>
        /// <param name="isDropDown">ドロップダウンで表示するかのフラグ</param>
        public SizePointEditor(SizePoint currentSizePoint, bool?isDropDown)
        {
            // デザイナで生成された設定を行う
            InitializeComponent();

            // 現在の設定値を保持
            // (NULLの場合はSizePointオブジェクトの初期値を使用)
            CurrentSizePoint = currentSizePoint ?? new SizePoint();

            // ドロップダウンで表示しているかのフラグを保持
            // (NULLの場合はFalse:ドロップダウンでの表示ではないとする)
            this.isDropDown = isDropDown ?? false;

            // 現在の設定値を元に各コントロールに値を設定
            SetCurrentSizePoint(CurrentSizePoint);
        }
Esempio n. 10
0
        /// <summary>
        /// 引数(<paramref name="windowHandle"/>)のウインドウハンドルを持つウィンドウにおいて、
        /// そのウィンドウのクライアント領域の上下左右の座標情報を取得する
        /// (クライアント領域の座標は相対座標のため X:0, Y:0 となる)
        /// </summary>
        /// <param name="windowHandle">取得対象のウィンドウのハンドル</param>
        /// <exception cref="PlatformInvokeException">
        /// Win32Apiの下記の処理の呼び出しに失敗した場合に発生
        /// ・「DLL:user32.dll、メソッド:IsWindow」
        /// ・「DLL:user32.dll、メソッド:IsWindowVisible」
        /// ・「DLL:user32.dll、メソッド:IsIconic」
        /// ・「DLL:user32.dll、メソッド:GetClientRect」
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// Win32Apiの下記の処理に失敗した場合に発生
        /// ・「DLL:user32.dll、メソッド:IsWindow」
        /// ・「DLL:user32.dll、メソッド:IsWindowVisible」
        /// ・「DLL:user32.dll、メソッド:IsIconic」
        /// ・「DLL:user32.dll、メソッド:GetClientRect」
        /// </exception>
        /// <returns>
        /// ウィンドウのクライアント領域のサイズ位置情報
        /// ウィンドウが存在しない、非表示、最小化状態の場合はNULLを返却
        /// </returns>
        public static SizePoint GetClientRect(HandleRef windowHandle)
        {
            // ウィンドウが存在しない、非表示、最小化状態の場合は処理をせずに終了する
            if (!IsWindow(windowHandle) ||
                !IsWindowVisible(windowHandle) ||
                IsIconic(windowHandle))
            {
                return(null);
            }

            // Win32Apiの実行処理
            // Win32ApiのWindou共通の呼び出し機能を用いて、上下左右の座標情報の取得処理を呼び出す
            Win32ApiResult Function()
            {
                bool win32Result    = Win32Api.GetClientRect(windowHandle, out Win32Api.RECT rect);
                int  win32ErrorCode = Marshal.GetLastWin32Error();

                int       width     = rect.Right - rect.Left;
                int       height    = rect.Bottom - rect.Top;
                int       positionX = rect.Left;
                int       positionY = rect.Top;
                SizePoint sizePoint = new SizePoint(width, height, positionX, positionY);

                return(new Win32ApiResult(sizePoint, win32Result, win32ErrorCode));
            }

            // 実行
            string         dllName    = "user32.dll";
            string         methodName = nameof(Win32Api.GetClientRect);
            Win32ApiResult result     = Win32ApiCommon.Run(Function, dllName, methodName);

            // 正常終了したかチェック
            if (!result.Result && result.ErrorCode != (int)ErrorCode.NO_ERROR)
            {
                throw Win32ApiCommon.GetWin32OperateException(dllName, methodName, result.ErrorCode);
            }

            // 取得したウィンドウのサイズ位置情報を返却
            return((SizePoint)result.ReturnValue);
        }
        /// <summary>
        /// この機能を使用して起動したChromeを閉じる
        /// </summary>
        /// <exception cref="PlatformInvokeException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生
        /// </exception>
        /// <returns>
        /// 閉じたChromeのサイズ位置の情報
        /// サイズ位置の情報が取得できない場合はNULLを返却
        /// </returns>
        public static SizePoint Close()
        {
            // 閉じたChromeのサイズ位置の情報の変数を宣言
            SizePoint sizePoint = null;

            // Chromeの音声認識が起動しているかチェック
            if (ChromeProcessInfoKey.HasValue)
            {
                // 閉じるChromeのウィンドウサイズ位置情報を取得
                foreach (ProcessInfo info in StartProcess.StartProcessList)
                {
                    // 起動したChromeのウィンドウハンドルを取得しサイズ位置情報を取得する
                    if (info.StartNumId == ChromeProcessInfoKey.Value && info.HasWindowInfo)
                    {
                        sizePoint = WindowOperate.GetWindowRect(info.WindowInfo.WindowHandle);
                    }

                    // サイズ位置情報が取得できた場合、ループを抜ける
                    if (sizePoint != null)
                    {
                        break;
                    }
                }

                // 起動している場合、対象のChromeを閉じる
                StartProcess.CloseWindow(ChromeProcessInfoKey.Value);
                ChromeProcessInfoKey = null;
            }

            // ローカルHTTPサーバが起動しているかチェック
            if (Server != null)
            {
                // 起動している場合は停止する
                Server.Stop();
                Server = null;
            }

            // 取得したサイズ位置情報を返却
            return(sizePoint);
        }
        /// <summary>
        /// Chromeによる音声認識を開始する
        /// <see cref="ChromePath"/> プロパティが未設定の場合はChromeのEXEファイルパスを自動的に検索し使用する
        /// </summary>
        /// <param name="htmlFilePath">
        /// 音声認識に使用する既定の「音声認識用.html」ファイルのパス
        /// </param>
        /// <param name="postRegex">
        /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するURLの正規表現
        /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
        /// </param>
        /// <param name="postParamName">
        /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するPOSTデータのパラメータ名
        /// (「音声認識用.html」と整合性をとる必要があるパラメータ)
        /// </param>
        /// <param name="sizePoint">
        /// 音声認識用に起動するChromeの初期のサイズと位置
        /// (NULLを指定した場合はデフォルトのサイズと位置で起動する)
        /// </param>
        /// <param name="localHttpServerPort">
        /// 音声認識に使用するローカルHTTPサーバのポート番号を指定する場合に設定するポート番号
        /// 指定しない場合(NULLの場合)、または指定したポート番号が使用できない場合は乱数からポート番号を取得し使用する
        /// </param>
        /// <param name="responceFunctions">
        /// 音声認識した文字に対する処理の指定(複数指定可、優先順位は上から)
        /// ・引数1  string:音声認識した文字列を渡す
        /// ・戻り値 byte[]:処理を実行した結果返却するデータ
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// 引数の <paramref name="htmlFilePath"/> 又は <paramref name="postRegex"/> がNULLの場合に発生
        /// </exception>
        /// <exception cref="ArgumentException">
        /// 下記の場合に発生する
        /// ・引数の <paramref name="htmlFilePath"/> が下記の場合
        ///  長さ 0 の文字列
        ///  空白のみで構成される
        ///  <see cref="Path.InvalidPathChars"/> で定義される 1 つ以上の正しくない文字を含む
        /// ・引数の <paramref name="postRegex"/> が正規表現として不正な値の場合
        /// </exception>
        /// <exception cref="SpeechRecognitionException">
        /// Chromeが使用不可能な場合に発生
        /// (<see cref="ChromePath"/> の指定が間違ってる 又は、Chromeがインストールされていない場合)
        /// </exception>
        /// <exception cref="IOException">
        /// 下記の場合に発生
        /// ・引数の <paramref name="htmlFilePath"/> がシステム定義の最大長を超えている場合
        ///  [<see cref="PathTooLongException"/>]
        ///  (たとえば、Windowsでは、パスは 248文字未満、ファイル名は 260 文字未満である必要がある)
        /// ・引数の <paramref name="htmlFilePath"/> が存在しないディレクトリを示している場合
        ///  [<see cref="DirectoryNotFoundException"/>]
        /// ・引数の <paramref name="htmlFilePath"/> で指定されたファイルが存在しない場合
        ///  [<see cref="FileNotFoundException"/>]
        /// ・I/O エラーが発生した場合
        ///  [<see cref="IOException"/>]
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        /// 引数の <paramref name="htmlFilePath"/> がファイルを指定しないない(ディレクトリを指定)場合、
        /// 又は、呼び出し元に必要なアクセス許可がない場合に発生
        /// </exception>
        /// <exception cref="Win32Exception">
        /// 下記の要因で。指定ファイルを開いているときにエラーが発生した場合に発生
        /// ・指定ファイルへの完全パスの長さと起動パラメータの長さの合計が、2080 文字を超えている場合
        /// ・指定ファイルへのアクセスが拒否された場合
        /// </exception>
        /// <exception cref="PlatformInvokeException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生
        /// </exception>
        /// <exception cref="Win32OperateException">
        /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生
        /// </exception>
        public static void Start(
            string htmlFilePath,
            string postRegex,
            string postParamName,
            SizePoint sizePoint,
            int?localHttpServerPort,
            params Func <string, byte[]>[] responceFunctions)
        {
            // NULLチェック
            if (htmlFilePath == null)
            {
                throw new ArgumentNullException(nameof(htmlFilePath));
            }
            else if (postRegex == null)
            {
                throw new ArgumentNullException(nameof(postRegex));
            }

            // Chromeが使用可能かチェックする
            switch (CanUseChrome)
            {
            case ChromeState.CanUse:
                // OK:処理を継続する
                break;

            case ChromeState.NoInstall:
                // NG:メッセージを表示して処理を中止する
                throw new SpeechRecognitionException(ErrorMessage.ChromeSpeechRecognitionMessageNoInstall);

            case ChromeState.ChromePathIsWrong:
            default:
                // NG:メッセージを表示して処理を中止する
                throw new SpeechRecognitionException(
                          ErrorMessage.ChromeSpeechRecognitionMessageChromePathIsWrong);
            }

            // 開始前の既にローカルHTTPサーバが起動している場合は落とす
            if (Server != null)
            {
                Server.Stop();
                Server = null;
            }

            // 開始前の既に起動している音声認識用のChromeが存在する場合は落とす
            if (ChromeProcessInfoKey.HasValue)
            {
                StartProcess.CloseWindow(ChromeProcessInfoKey.Value);
                ChromeProcessInfoKey = null;
            }

            // HTTPサーバのクラスを生成
            HttpResponseData faviconResponse = FaviconData == null
                    ? LocalHttpServerCommon.EmptyDataResponse
                    : LocalHttpServerCommon.GetIconDataResponse(FaviconData);

            Server = new LocalHttpServer(
                LocalHttpServerCommon.GetHttpFileDataResponse(htmlFilePath),
                faviconResponse,
                GetResponceProcesses(responceFunctions, postRegex, postParamName));

            // HTTPサーバのクラスをリソース解放対象として登録する
            // (非同期での動作を止めるために登録)
            EntryPoint.DisposeClass.Add(Server);

            // HTTPサーバを起動する
            Server.Start(localHttpServerPort);

            // 起動パラメータを設定
            string startUpParam = string.Format(
                CultureInfo.InvariantCulture, ChromeStartUpParamFormat, Server.Url.ToString());

            // Chromeを起動する
            long?key = StartProcess.Start(ChromePath, startUpParam, sizePoint);

            // 起動キーをプロパティに保持する
            ChromeProcessInfoKey = key;
        }
Esempio n. 13
0
        /// <summary>
        /// 対象のウィンドウにおいて画面に表示されている領域のサイズ位置を取得する
        /// </summary>
        /// <param name="windowSizePoint">対象のウィンドウのサイズ位置情報</param>
        /// <param name="targetScreen">対象のウィンドウが存在する画面</param>
        /// <returns>画面に表示されている領域のサイズ位置</returns>
        private static SizePoint GetDisplayedSizePoint(SizePoint windowSizePoint, Screen targetScreen)
        {
            // ウインドウと画面の四隅の情報を取得
            Rectangle windowRect = new Rectangle(
                windowSizePoint.Point, windowSizePoint.Size);
            Rectangle screenRect = new Rectangle(
                targetScreen.WorkingArea.Location, targetScreen.WorkingArea.Size);

            // ウィンドウの左上の座標を取得
            Point windowLocation = windowSizePoint.Point;

            // 画面からはみ出した領域を計算
            // 幅
            int overWidth;

            if (windowRect.Left < screenRect.Left)
            {
                // 左にはみ出している場合、はみ出した分を取得
                overWidth = screenRect.Left - windowRect.Left;

                // 左にはみ出した分、左上の座標を補正する
                windowLocation.X += overWidth;
            }
            else if (windowRect.Right > screenRect.Right)
            {
                // 右にはみ出している場合、はみ出した分を取得
                overWidth = windowRect.Right - screenRect.Right;
            }
            else
            {
                // 上記以外の場合、はみ出していないため 0 を設定
                overWidth = 0;
            }

            // 高さ
            int overHeight;

            if (windowRect.Top < screenRect.Top)
            {
                // 上にはみ出している場合、はみ出した分を取得
                overHeight = screenRect.Top - windowRect.Top;

                // 上にはみ出した分、左上の座標を補正する
                windowLocation.Y += overHeight;
            }
            else if (windowRect.Bottom > screenRect.Bottom)
            {
                // 下にはみ出している場合、はみ出した分を取得
                overHeight = windowRect.Bottom - screenRect.Bottom;
            }
            else
            {
                // 上記以外の場合、はみ出していないため 0 を設定
                overHeight = 0;
            }

            // 画面に表示されている領域のサイズ位置を設定
            SizePoint displayedSizePoint = new SizePoint(
                sizeWidth: windowSizePoint.SizeWidth - overWidth,
                sizeHeight: windowSizePoint.SizeHeight - overHeight,
                positionX: windowLocation.X,
                positionY: windowLocation.Y);

            // 生成したサイズ位置情報を返す
            return(displayedSizePoint);
        }
Esempio n. 14
0
 /// <summary>
 /// コンストラクタ
 /// 引数の値でこのユーザコントロールを初期化する
 /// </summary>
 /// <param name="currentSizePoint">設定対象とするFormコントロールの現在のサイズと位置を指定</param>
 public SizePointEditor(SizePoint currentSizePoint)
     : this(currentSizePoint, null)
 {
     // 下記のコンストラクタを呼び出しており、そこで処理を行うためここでは処理をしない
 }