🌊

便利な検索窓を作成してみた。

に公開

大体の方針はわかっているので、LLMに作らせる。

プロンプト

C# visualstadioでウィンドウ枠無しの検索ウィンドウを作成する。 仕様 入力した文字列と Googleの検索キュー文字列を結合し、これを規定のソフトで開く。 ウィンドウ無し、タスクバーにも出さない。 エンターで検索。 または検索ボタンで検索。 速く起動すること、ソフトウェアサイズも小さいほうがありがたい

AIの回答

お任せください。C#とVisual Studioで、ご要望の「ウィンドウ枠なし」「タスクバー非表示」の高速起動・軽量なGoogle検索専用ウィンドウを作成する手順とコードの概要をご案内します。この要件を実現するには、Windows Forms (WinForms) または WPF (Windows Presentation Foundation) を使用し、フォームのプロパティを調整するのが最も一般的です。今回は、よりシンプルで高速起動・軽量なWinFormsをベースに解説します。💻 検索ウィンドウの作成手順(WinForms)

1. プロジェクトの作成

VisualStudioで新しいプロジェクトを作成します。

プロジェクトの種類として「Windowsフォーム アプリケーション (.NET Framework または .NET Core)」を選択します。プロジェクト名を例えば QuickSearch などとします。2. フォーム (Form1) のプロパティ設定デザイン画面で Form1 を選択し、プロパティウィンドウで以下の設定を行います。これが「ウィンドウ枠なし」「タスクバー非表示」を実現する重要な設定です。

プロパティ設定値説明

FormBorderStyleNone        ウィンドウ枠 (タイトルバー、境界線) を非表示にします。
ShowInTaskbarFalse         タスクバーにアイコンを表示しないようにします。
TopMostTrue                (任意)常に最前面に表示したい場合に設定します。
StartPositionCenterScreen  (任意)画面中央に表示します。

2. コントロールの配置

Form1 に以下のコントロールを配置します。

TextBox (名前: txtSearch):検索文字列を入力します。
Button (名前: btnSearch, Text: 検索):検索を実行します。

3. 検索処理の実装

btnSearch をダブルクリックするか、フォームのイベントハンドラに以下のコードを追加します。

※ 記事の最後に最終的なコード全文を掲載しています。


using System;
using System.Windows.Forms;
using System.Diagnostics; 

public partial class Form1 : Form
{
    // Google検索のクエリ文字列
    private const string GoogleSearchUrlPrefix = "https://www.google.com/search?q=";

    public Form1()
    {
        InitializeComponent();
        
        // フォームロード時にTextBoxにフォーカスを設定
        this.Load += (sender, e) => txtSearch.Focus();
        
        // Enterキー押下で検索を実行する設定
        txtSearch.KeyDown += txtSearch_KeyDown;
        
        // 検索ボタンクリックイベント
        btnSearch.Click += btnSearch_Click;

        // フォーム全体でのキー入力イベントを有効化
        this.KeyPreview = true;
        // Escキーでの非表示/終了処理
        this.KeyDown += Form1_KeyDown;
    }

    // フォーム全体でのキー入力処理 (Escキー検出用)
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            // Escキーが押されたら、フォームを非表示にして閉じたように見せます。
            // 完全にアプリケーションを終了させたい場合は this.Close(); を使用してください。
            // 高速再起動のためにアプリを起動したまま非表示にする場合はこちら。
            this.Hide(); 
            // 完全に終了させる場合: this.Close(); 
        }
    }

    // 検索ボタンがクリックされたときの処理
    private void btnSearch_Click(object sender, EventArgs e)
    {
        ExecuteSearch();
    }

    // TextBoxでキーが押されたときの処理 (Enterキー検出用)
    private void txtSearch_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            // Enterキーが押されたら検索を実行
            ExecuteSearch();
            
            // Enterキーの標準動作(ビープ音など)を抑制
            e.SuppressKeyPress = true; //間違い keypressでの実装要
            e.Handled = true;//間違い keypressでの実装要
        }
    }

    // 検索実行ロジック
    private void ExecuteSearch()
    {
        string searchText = txtSearch.Text.Trim();

        if (string.IsNullOrEmpty(searchText))
        {
            return;
        }

        string searchUrl = GoogleSearchUrlPrefix + searchText;

        try
        {
            // 規定のブラウザでURLを開く
            Process.Start(searchUrl);

            // 検索実行後、TextBoxの内容をクリアして、次の検索に備えます
            txtSearch.Clear();
            txtSearch.Focus();
            
            // ★重要: ここで this.Close() は実行しません。

        }
        catch (Exception ex)
        {
            MessageBox.Show($"検索中にエラーが発生しました: {ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

このままだとエラー

private void ExecuteSearch()
{
    string searchText = txtSearch.Text.Trim();

    if (string.IsNullOrEmpty(searchText))
    {
        return;
    }

    string searchUrl = GoogleSearchUrlPrefix + searchText;

    try
    {

    //こう書きなおす必要がある。
        var startInfo = new ProcessStartInfo()
        {
            FileName = searchUrl,
            UseShellExecute = true,
            CreateNoWindow = true,
        };
        Process.Start(startInfo);
    
        // 検索実行後、TextBoxの内容をクリアして、次の検索に備えます
        txtSearch.Clear();
        txtSearch.Focus();

        // ★重要: ここで this.Close() は実行しません。

    }
    catch (Exception ex)
    {
        MessageBox.Show($"検索中にエラーが発生しました: {ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

さらにウィンドウがないため、移動処理などを自前で用意する必要あり。

https://dobon.net/vb/dotnet/form/moveform.html#google_vignette
DOBON.NET
タイトルバーのないフォームを移動できるようにする
より、
以下を拝借し、

//マウスのクリック位置を記憶
private Point mousePoint;

//Form1のMouseDownイベントハンドラ
//マウスのボタンが押されたとき
private void Form1_MouseDown_1(object sender,
    System.Windows.Forms.MouseEventArgs e)
{
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
        //位置を記憶する
        mousePoint = new Point(e.X, e.Y);
    }
}

//Form1のMouseMoveイベントハンドラ
//マウスが動いたとき
private void Form1_MouseMove_1(object sender,
    System.Windows.Forms.MouseEventArgs e)
{
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
        this.Left += e.X - mousePoint.X;
        this.Top += e.Y - mousePoint.Y;
        //または、つぎのようにする
        //this.Location = new Point(
        //    this.Location.X + e.X - mousePoint.X,
        //    this.Location.Y + e.Y - mousePoint.Y);
    }
}

最終成果物


using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace search_var
{


    public partial class Form1 : Form
    {
        // Google検索のクエリ文字列
        private const string GoogleSearchUrlPrefix = "https://www.google.com/search?q=";

        public Form1()
        {
            InitializeComponent();

            // フォームロード時にTextBoxにフォーカスを設定
            this.Load += (sender, e) => txtSearch.Focus();

            // Enterキー押下で検索を実行する設定
            txtSearch.KeyDown += txtSearch_KeyDown;

            // 検索ボタンクリックイベント
            btnSearch.Click += btnSearch_Click;

            // フォーム全体でのキー入力イベントを有効化
            this.KeyPreview = true;
            // Escキーでの非表示/終了処理
            this.KeyDown += Form1_KeyDown;
        }

        // フォーム全体でのキー入力処理 (Escキー検出用)
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                // Escキーが押されたら、フォームを非表示にして閉じたように見せます。
                // 完全にアプリケーションを終了させたい場合は this.Close(); を使用してください。
                // 高速再起動のためにアプリを起動したまま非表示にする場合はこちら。
                this.Hide();
                // 完全に終了させる場合: this.Close(); 
            }
        }

        // 検索ボタンがクリックされたときの処理
        private void btnSearch_Click(object sender, EventArgs e)
        {
            ExecuteSearch();
        }

        // TextBoxでキーが押されたときの処理 (Enterキー検出用)
        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // Enterキーが押されたら検索を実行
                ExecuteSearch();

                // Enterキーの標準動作(ビープ音など)を抑制
                e.SuppressKeyPress = true;
                e.Handled = true;
            }
        }

        // 検索実行ロジック
        private void ExecuteSearch()
        {
            string searchText = txtSearch.Text.Trim();

            if (string.IsNullOrEmpty(searchText))
            {
                return;
            }

            string searchUrl = GoogleSearchUrlPrefix + searchText;

            try
            {


                var startInfo = new ProcessStartInfo()
                {
                    FileName = searchUrl,
                    UseShellExecute = true,
                    CreateNoWindow = true,
                };
                Process.Start(startInfo);

                // 検索実行後、TextBoxの内容をクリアして、次の検索に備えます
                txtSearch.Clear();
                txtSearch.Focus();

                // ★重要: ここで this.Close() は実行しません。

            }
            catch (Exception ex)
            {
                MessageBox.Show($"検索中にエラーが発生しました: {ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //マウスのクリック位置を記憶
        private Point mousePoint;

        //Form1のMouseDownイベントハンドラ
        //マウスのボタンが押されたとき
        private void Form1_MouseDown_1(object sender,
            System.Windows.Forms.MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                //位置を記憶する
                mousePoint = new Point(e.X, e.Y);
            }
        }

        //Form1のMouseMoveイベントハンドラ
        //マウスが動いたとき
        private void Form1_MouseMove_1(object sender,
            System.Windows.Forms.MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                this.Left += e.X - mousePoint.X;
                this.Top += e.Y - mousePoint.Y;
                //または、つぎのようにする
                //this.Location = new Point(
                //    this.Location.X + e.X - mousePoint.X,
                //    this.Location.Y + e.Y - mousePoint.Y);
            }
        }

    }
}


追記
このままだとエンターを押すとビープ音が鳴ります。

https://dobon.net/vb/dotnet/control/tbsuppressbeep.html#google_vignette

より、

Keypressイベントに以下を追加するとうまくいくようです。

//イベントを処理済にする
if (e.KeyChar == (char)Keys.Enter)
{
    e.Handled = true;
}

以下読みたい方はどうぞ。

if (searchText.StartsWith("http"))
{
    searchUrl = searchText;
}
else {
    searchUrl = GoogleSearchUrlPrefix + searchText;

}

こうゆうこともできる

Discussion