C#实现拖拽控件边控,动态改变大小;

该代码展示了如何在WindowsForms中创建一个可调整大小的RichTextBox控件,通过重写WndProc方法实现鼠标点击位置的特殊处理,支持顶部、底部、左侧和右侧的边界缩放行为。
摘要由CSDN通过智能技术生成

public partial class ResizableRichTextBox : RichTextBox
{
    public ResizableRichTextBox()
    {
        InitializeComponent();
    }

    public ResizableRichTextBox(IContainer container)
    {
        container.Add(this);

        InitializeComponent();
    }

    [DllImport("user32.dll")]
    private static extern int GetSystemMetrics(int nIndex);

    private const int WM_NCHITTEST = 0x84;
    private const int HTBOTTOM = 15;
    private const int HTBOTTOMLEFT = 16;
    private const int HTBOTTOMRIGHT = 17;
    private const int HTTOP = 12;
    private const int HTTOPLEFT = 13;
    private const int HTTOPRIGHT = 14;
    private const int HTLEFT = 10;
    private const int HTRIGHT = 11;
    private const int WM_EXPAND = WM_USER + 67;
    private const int WM_USER = 0x0400;

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        switch (m.Msg)
        {
            case WM_NCHITTEST:
                base.WndProc(ref m);
                Point vPoint = new Point((int)m.LParam & 0xFFFF,
                                         (int)m.LParam >> 16 & 0xFFFF);
                vPoint = this.PointToClient(vPoint);
                if (vPoint.X <= 5)
                {
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPLEFT;
                    else if (vPoint.Y >= this.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMLEFT;
                    else
                        m.Result = (IntPtr)HTLEFT;
                }
                else if (vPoint.X >= this.Width - 5)
                {
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPRIGHT;
                    else if (vPoint.Y >= this.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMRIGHT;
                    else
                        m.Result = (IntPtr)HTRIGHT;
                }
                else if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOP;
                else if (vPoint.Y >= this.Height - 5)
                    m.Result = (IntPtr)HTBOTTOM;
                break;
        }
    }

}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值