1
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?

[.NET] 実行中にFormBorderStyle=Noneに切り替えても簡易的にサイズ変更できるようにしてみた。

Last updated at Posted at 2022-11-25

Windowsのタイトルバーを表示させない方法はいろいろありますが、実行中にタイトルバーを消したり出したり切り替えたい場合(そんなことあるのか?)、

  • CreateParamsをオーバーライドするスマートな方法は文字通りForm作成時にしか参照されないので実行中に切り替えるのには使えない
  • ControlBox=FalseでText=String.Emptyはタスクバーも名無しになるのが不自然(元からタスクバーに表示させないつもりならこれで)
  • 仕方がないのでFormBorderStyle=Noneを使うのですが、そのままではサイズ変更ができない困りもの

なので疑似的にFormBorderStyle=Noneでもサイズ変更できるようにしてみました。あくまでも簡易的にね。その割にちょっと長いけど。

Form1.cs
private bool inFormSizeChange;
private struct RectangleBool { public bool left, right, top, bottom; }
private RectangleBool pinch;
private const int mdt = 8;    // マウスの検出幅(内側)
                              // 大きくしすぎるとカーソルがDefaultに戻りづらい(Formのサイズに依る)

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        mousePoint = new Point(e.X, e.Y);
        pinch.left = e.X < mdt;
        pinch.right = e.X > _this.Width - mdt;
        pinch.top = e.Y < mdt;
        pinch.bottom = e.Y > this.Height - mdt;
        inFormSizeChange = pinch.left || pinch.right || pinch.top || pinch.bottom;
        if (!inFormSizeChange) Cursor = Cursors.SizeAll;    // どの枠もつままれてなければウィンドウの移動
    }
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (form2.HideTitleCheckBox.Checked)    // タイトルバーを隠す設定になっていたら
    {
        int minWidth = 128;    // ウィンドウサイズの最小限界
        int minHeight = 64;    // ここらは適当に設定

        if (e.Button == MouseButtons.Left)
        {
            if (this.Cursor == Cursors.SizeWE)    // マウスカーソルが左右矢印に変わってたら
            {
                if (pinch.left)  // 左側をつまんでいる
                {
                    this.Width -= e.X - mousePoint.X;
                    if (this.Width < minWidth) this.Width = minWidth;
                    else this.Left += e.X - mousePoint.X;    // 左側を動かしているならフォームの原点自体も移動
                }
                else if (pinch.right)   // じゃなかったら右を動かしてる
                {
                    this.Width = e.X;
                    if (this.Width < minWidth) this.Width = minWidth;
                    // 右側を動かしている場合は原点の移動は無い
                }
            }
            else if (this.Cursor == Cursors.SizeNS)    // マウスカーソルが上下矢印に変わってたら
            {
                if (pinch.top)
                {
                    this.Height -= e.Y - mousePoint.Y;
                    if (this.Height < minHeight) this.Height = minHeight;
                    else this.Top += e.Y - mousePoint.Y;
                }
                else if (pinch.bottom)
                {
                    this.Height = e.Y;
                    if (this.Height < minHeight) this.Height = minHeight;
                }
            }
            else    // ウィンドウ自体の移動
            {
                Left += e.X - mousePoint.X;
                Top += e.Y - mousePoint.Y;
            }
        }
        if (e.X < mdt || e.X > this.Width - mdt)
            this.Cursor = Cursors.SizeWE;
        else if (e.Y < mdt || e.Y > this.Height - mdt)
            this.Cursor = Cursors.SizeNS;
        else if (Cursor != Cursors.SizeAll && e.Button == MouseButtons.None)    // これが無いと移動中にカーソルが範囲外に出ると戻っちゃう
            this.Cursor = Cursors.Default;
    }
}

private void Form1_MouseHover(object sender, EventArgs e)
{
    inFormSizeChange = false;    // ボタンの押されていないカーソルがフォームにもどってきたらサイズ変更終了
    this.Cursor = Cursors.Default;    //変えたカーソルを戻す
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    if (Cursor != Cursors.Default)  //サイズ変更・移動を終了
    {
        Cursor = Cursors.Default;
        pinch.left = pinch.right = pinch.top = pinch.bottom = false;
    }
}

ただやるべきことちまちまと書いてるだけです。
フォームの内側を摘まむ必要があったり、マウスカーソルがフォームの外に出てしまうとイベントで捕まえられなくなってしまうので、ちょっとマウス捌きにコツが要りますが(特に右方向、下方向)、あと四隅のサイズ変更は面倒なのでやってません。なんとなく趣味の範囲ではこれで実用になってます。
車輪の再発明? なにか別のもっといい方法があったら教えてください。

1
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
1
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?