C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.89786 の関連記事表示

<< 0 >>
■89786  Re[1]: buttonのドラッグリサイズ C#プログラム
□投稿者/ 魔界の仮面弁士 -(2018/12/26(Wed) 10:25:04)
    No89782 (Rty1143 さん) に返信
    > C# windows form application で button をフォーム上に配置しておりますが、
    > このbuttonの端をマウスで摘んでドラッグでリサイズする方法を調べています。
    > 分かる方いませんか?
    
    これでどうでしょう。Load イベントなどで
    
     private void Form1_Load(object sender, EventArgs e)
     {
      Resizer.Attach(button1);
      Resizer.Attach(button2);
      Resizer.Attach(button3);
     }
    
    のように呼ぶことで、ドラッガブルなコントロールになります。
    
    //-----
    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing;
    
    public static class Resizer
    {
        public static void Attach(Control target)
        {
            Attach(target, 5);
        }
    
        public static void Attach(Control target, int bandWidth)
        {
            if (bandWidth <= 0) { throw new ArgumentOutOfRangeException(nameof(bandWidth), bandWidth, "1以上の値を指定してください。"); }
            target.MouseDown += (sender, e) =>
            {
                int cmd = GetResizeCommand(target.Size, e.Location, bandWidth);
                if (cmd != 0)
                {
                    SetCapture(target.Handle);
                    ReleaseCapture();
                    SendMessage(target.Handle, WM_SYSCOMMAND, SC_SIZE | cmd, 0);
                }
            };
            target.MouseMove += (sender, e) =>
            {
                switch (GetResizeCommand(target.Size, e.Location, bandWidth))
                {
                    case 1:
                    case 2:
                        target.Cursor = Cursors.SizeWE;
                        break;
                    case 3:
                    case 6:
                        target.Cursor = Cursors.SizeNS;
                        break;
                    case 4:
                    case 8:
                        target.Cursor = Cursors.SizeNWSE;
                        break;
                    case 5:
                    case 7:
                        target.Cursor = Cursors.SizeNESW;
                        break;
                    default:
                        target.Cursor = Cursors.Default;
                        break;
                }
            };
        }
    
        private static int GetResizeCommand(Size size, Point point, int bandWidth)
        {
            int WMSZ = 0;
            if (point.X < bandWidth) { WMSZ += 1; }
            if (point.Y < bandWidth) { WMSZ += 3; }
            if (size.Width - bandWidth < point.X) { WMSZ += 2; }
            if (size.Height - bandWidth < point.Y) { WMSZ += 6; }
            return WMSZ;
        }
    
        #region P/Invoke
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
        [DllImport("user32")]
        private static extern bool SetCapture(IntPtr hWnd);
        [DllImport("user32")]
        private static extern bool ReleaseCapture();
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MOVE = 0xF010;
        private const int SC_SIZE = 0xF000;
        #endregion
    }
    
    //-----
    
    なお、上記のサンプル最低限のものなので、たとえば
     ・リサイズ可能な最小・最大サイズを設定できない
     ・同じコントロールに何度もアタッチできてしまう
     ・解除処理(デタッチ)を実装していない
    といった実装課題があります。
記事No.89782 のレス /過去ログ154より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -