c# 窗体上控件如何放大缩小和移动

using   System;
using   System.Collections;
using   System.Text;
using   System.Windows.Forms;
using   System.Drawing;

namespace   testmovecontrol
{
class   MoveableControl:Button
{
internal   static   int   WM_NCHITTEST   =   0x84;   //移动鼠标,按住或释放鼠标时发生的系统消息
internal   static   int   WM_NCACTIVATE   =   0x86;//窗体的激活状态发生改变的消息

internal   static   IntPtr   HTCLIENT   =   (IntPtr)0x1;//工作区
internal   static   IntPtr   HTSYSMENU   =   (IntPtr)3;//系统菜单
internal   static   IntPtr   HTCAPTION   =   (IntPtr)0x2;   //标题栏

internal   static   IntPtr   HTLEFT   =   (IntPtr)10;//向左
internal   static   IntPtr   HTRIGHT   =   (IntPtr)11;//向右
internal   static   IntPtr   HTTOP   =   (IntPtr)12;//向上
internal   static   IntPtr   HTTOPLEFT   =   (IntPtr)13;//向左上
internal   static   IntPtr   HTTOPRIGHT   =   (IntPtr)14;//向右上
internal   static   IntPtr   HTBOTTOM   =   (IntPtr)15;//向下
internal   static   IntPtr   HTBOTTOMLEFT   =   (IntPtr)16;//向左下
internal   static   IntPtr   HTBOTTOMRIGHT   =   (IntPtr)17;//向右下

private   int   m_BorderWidth   =   4;
private   bool   m_Sizeable   =   true;
protected   override   void   WndProc(ref   Message   m)
{
if   (m.Msg   ==   WM_NCHITTEST)
{
base.WndProc(ref   m);
if   (DesignMode)
{
return;
}
if   (m.Result   ==   HTCLIENT)
{
m.HWnd   =   this.Handle;

System.Drawing.Rectangle   rect   =   this.RectangleToScreen(this.ClientRectangle);
Point   C_Pos   =   Cursor.Position;
if   ((C_Pos.X   <=   rect.Left   +   m_BorderWidth)   &&   (C_Pos.Y   <=   rect.Top   +   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTTOPLEFT;//左上
else   if   ((C_Pos.X   > =   rect.Left   +   rect.Width   -   m_BorderWidth)   &&   (C_Pos.Y   <=   rect.Top   +   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTTOPRIGHT;//右上
else   if   ((C_Pos.X   <=   rect.Left   +   m_BorderWidth)   &&   (C_Pos.Y   > =   rect.Top   +   rect.Height   -   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTBOTTOMLEFT;//左下
else   if   ((C_Pos.X   > =   rect.Left   +   rect.Width   -   m_BorderWidth)   &&   (C_Pos.Y   > =   rect.Top   +   rect.Height   -   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTBOTTOMRIGHT;//右下
else   if   ((C_Pos.X   <=   rect.Left   +   m_BorderWidth   -   1)   &&   this.m_Sizeable)
m.Result   =   HTLEFT;//左
else   if   ((C_Pos.X   > =   rect.Left   +   rect.Width   -   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTRIGHT;//右
else   if   ((C_Pos.Y   <=   rect.Top   +   m_BorderWidth   -   1)   &&   this.m_Sizeable)
m.Result   =   HTTOP;//上
else   if   ((C_Pos.Y   > =   rect.Top   +   rect.Height   -   m_BorderWidth)   &&   this.m_Sizeable)
m.Result   =   HTBOTTOM;//下
else
{
m.Result   =   HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref   m);
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值