0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nvim - windows - C#: スクロールバーを非表示にするコマンドの作成

0
Last updated at Posted at 2023-05-09

windows上でnvimやvimを触っているときに出てくるスクロールバーを消したい

ウィンドウサイズをいじったりするとWindowsのスクロールバーがひょっこりと顔を出す。

特に水平方向のスクロールバーはコマンドが見えなくなるのでやっかいです。

ということで、スクロールバーを削除するコマンドをC#で作成してnvimから呼び出そうという作戦です。

環境

OS: windows 10
コンパイラ: Microsoft (R) Visual C# Compiler version 4.8.4084.0
nvim: v0.8.2

今回C#を選択した理由は、ほとんどのwindows環境に標準でインストールされているため、環境構築の必要がないからです。
同様の理由で、Visual Studioを使用せずに手動コンパイル?します。

実装

1.書く

忘れたとき用にヘルプも書いときました。

hidesb.cs
using System;
using System.Runtime.InteropServices;

class HideSB
{
    // Windows API
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow);

    //定数定義
    const int SB_HORZ = 0;
    const int SB_VERT = 1;
    const int SB_BOTH = 3;
    const int SW_HIDE = 0;

    static void Main(string[] args)
    {
        //コマンドライン引数が1つのときだけ続行
        if (args.Length != 1)
        {
            Console.WriteLine("\r\nError: Argument count mismatch.");
            ShowHelp();
            return;
        }

        //コマンドライン引数の値によって処理を分岐
        string option = args[0];
        int target;

        if (option == "-h")
        {
            //水平スクロールバー
            target = SB_HORZ;
        }
        else if (option == "-v")
        {
            //垂直スクロールバー
            target = SB_VERT;
        }
        else if (option == "-b")
        {
            //水平、垂直スクロールバー
            target = SB_BOTH;
        }
        else if (option == "--help")
        {
            //helpを表示して終了
            ShowHelp();
            return;
        }
        else
        {
            //予期しない引数の場合は中断
            Console.WriteLine("\r\nError: invalid argument.");
            ShowHelp();
            return;
        }

        //現在のウィンドウのハンドラを取得
        IntPtr hWnd = GetForegroundWindow();

        //対象のスクロールバーを非表示にする
        ShowScrollBar(hWnd, target, SW_HIDE);
    }

    static void ShowHelp()
    {
        Console.WriteLine("\r\n================= help =================");
        Console.WriteLine("Usage");
        Console.WriteLine("  hidesb [option]");
        Console.WriteLine("Available auguments");
        Console.WriteLine("  -h      hide Horizontal Scroll Bar.");
        Console.WriteLine("  -v      hide Vertical Scroll Bar.");
        Console.WriteLine("  -b      hide Both Scroll Bar.");
        Console.WriteLine("  --help  this help.");
        Console.WriteLine("========================================");
    }
}

2.コンパイル

今回はVisual Studioを使わないので、コマンドを叩いてコンパイルすることにします。
バッチを作っておくと楽です。

cmd
> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe hidesb.csまでのパス

csc.exeが上のパスになかったら、このコマンドで探してみてください。

cmd
> dir C:\ /b /s | findstr csc.exe$

3.パスを通す

完成した実行ファイルのパスを通して、雑に実行できるようにします。
vimで呼び出すだけであれば、手順4のコマンドを絶対パスで書いても大丈夫です。

4.init.vimにコマンド追加

自分は基本nvimなのでinit.vimです。一応wls上のvimでも動作確認済みです。
お好みでコマンド以外の方法で呼び出しても大丈夫です。

init.vim
command! HIDESB  :call system("hidesb -b")
command! HIDEHSB :call system("hidesb -h")
command! HIDESVB :call system("hidesb -v")

おわり

見ていただきありがとうございました。
自分はこの方法でスクロールバーを消しましたが、もっと良い方法があったら教えていただけると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?