private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//如果正常运行并且用户要监听鼠标的消息
if ((nCode >= 0) && (OnMouseActivity!=null)) 
{
MouseButtons button=MouseButtons.None;
int clickCount=0; switch (wParam)
{
case WM_LBUTTONDOWN: 
button=MouseButtons.Left; 
clickCount=1;
break;
case WM_LBUTTONUP: 
button=MouseButtons.Left; 
clickCount=1;
break;
case WM_LBUTTONDBLCLK: 
button=MouseButtons.Left; 
clickCount=2;
break;
case WM_RBUTTONDOWN: 
button=MouseButtons.Right; 
clickCount=1;
break;
case WM_RBUTTONUP: 
button=MouseButtons.Right; 
clickCount=1;
break;
case WM_RBUTTONDBLCLK: 
button=MouseButtons.Right; 
clickCount=2;
break;
} //从回调函数中得到鼠标的信息
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MouseEventArgs e=new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0 );
OnMouseActivity(this, e);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam); 
}
} MouseHook actHook;
void FormLoad(object sender, System.EventArgs e)
{
actHook= new MouseHook(); 
actHook.Start();
actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
this.Size = new System.Drawing.Size(488, 244);
}
protected override void OnClosing(CancelEventArgs e)
{
if (actHook != null)
actHook.Stop();
base.OnClosing (e);
}
private const int BorderWidth = 5;
public void MouseMoved(object sender, MouseEventArgs e)
{
ret = new Rectangle(this.Left, this.Top, 488, 244);
if(e.X >= this.Left && e.X <= this.Left + BorderWidth  && e.Y >= this.Top  && e.Y <= this.Top + BorderWidth)
{
System.Diagnostics.Debug.WriteLine("左上" );
}
else if(e.X >= this.Left && e.X <= this.Left + BorderWidth  && e.Y >= this.Top + this.Height - BorderWidth && e.Y <= this.Top + this.Height  )
{
System.Diagnostics.Debug.WriteLine("左下" );
}
else if(e.X >=this.Left + this.Width-BorderWidth  && e.X <= this.Left + this.Width  && e.Y >= this.Top && e.Y <= this.Top +BorderWidth)
{
System.Diagnostics.Debug.WriteLine("右上" );
}
else if(e.X >= this.Left + this.Width-BorderWidth  && e.X <= this.Left + this.Width  && e.Y >=this.Top + this.Height - BorderWidth && e.Y <= this.Top + this.Height )
{
System.Diagnostics.Debug.WriteLine("右下" );
}
else if(e.X  <= this.Left + BorderWidth && e.X >= this.Left)
{
System.Diagnostics.Debug.WriteLine("左" );
}
else if(e.X  <= this.Left + this.Width + BorderWidth && e.X >= this.Left + this.Width-BorderWidth)
{
System.Diagnostics.Debug.WriteLine("右" );
}
else if(e.Y <= this.Top + BorderWidth  && e.Y >= this.Top)
{
System.Diagnostics.Debug.WriteLine("上" );
}
else if(e.Y >= this.Top + this.Height - BorderWidth  && e.Y <= this.Top + this.Height)
{
System.Diagnostics.Debug.WriteLine("下" );
}
// if ( !ret.Contains(new Point(e.X,e.Y)))
// {
//
// System.Diagnostics.Debug.WriteLine("不是" );
//// if (this.Width != 120)
//// this.Width =120;
// }
// else
// {
// System.Diagnostics.Debug.WriteLine("是" );
//// if (this.Width != 500)
//// this.Width = 500;
// }
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new MouseHookForm());
}
}
}

解决方案 »

  1.   

    原来的窗体移动代码
    //可以调整窗体的大小和移动窗体的位置
    protected override void WndProc(ref Message m)
    {
    //System.Diagnostics.Debug.WriteLine(m.ToString());
    switch(m.Msg)
    {
    case WM_NCHITTEST:
    System.Diagnostics.Debug.WriteLine(m.Result.ToString());
    base.WndProc(ref m);
    if (DesignMode)
    {
    return;
    }
    // if (((int)m.Result == HTCLIENT ))
    // {
    // if ((Cursor.Position.X<=this.Left + BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
    // m.Result = (IntPtr)13;//左上
    // else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=this.Top +BorderWidth))
    // m.Result = (IntPtr)14;//右上
    // else if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    // m.Result = (IntPtr)16;//左下
    // else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    // m.Result = (IntPtr)17;//右下
    // else if (Cursor.Position.X<=this.Left + BorderWidth)
    // m.Result = (IntPtr)10;//左
    // else if (Cursor.Position.X>=this.Left + this.Width-BorderWidth)
    // m.Result = (IntPtr)11;//右
    // else if (Cursor.Position.Y<=this.Top + BorderWidth)
    // m.Result = (IntPtr)12;//上
    // else if (Cursor.Position.Y>=this.Top + this.Height-BorderWidth) else 
    m.Result = (IntPtr)HTCAPTION;//移动窗体   
    // }
    return;
    default:
    base.WndProc(ref m);
    break;
    }
    }
    我用什么联系起来呢?
      

  2.   

    别忘了加上这个类using System;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;namespace HookGlobal
    {

    /// <summary>
    /// 这个类可以让你得到一个在运行中程序的所有鼠标事件
    /// 并且引发一个带MouseEventArgs参数的.NET鼠标事件以便你很容易使用这些信息
    /// </summary>
    /// <res>
    /// </res>
    public class MouseHook
    {
    private const int WM_MOUSEMOVE = 0x200;
    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_RBUTTONDOWN = 0x204;
    private const int WM_MBUTTONDOWN = 0x207;
    private const int WM_LBUTTONUP = 0x202;
    private const int WM_RBUTTONUP = 0x205;
    private const int WM_MBUTTONUP = 0x208;
    private const int WM_LBUTTONDBLCLK = 0x203;
    private const int WM_RBUTTONDBLCLK = 0x206;
    private const int WM_MBUTTONDBLCLK = 0x209; //全局的事件
    public event MouseEventHandler OnMouseActivity; static int hMouseHook = 0; //鼠标钩子句柄 //鼠标常量
    public const int WH_MOUSE_LL  = 14; //mouse hook constant HookProc MouseHookProcedure; //声明鼠标钩子事件类型. //声明一个Point的封送类型
    [StructLayout(LayoutKind.Sequential)]
    public class POINT 
    {
    public int x;
    public int y;
    } //声明鼠标钩子的封送结构类型
    [StructLayout(LayoutKind.Sequential)]
    public class MouseHookStruct 
    {
    public POINT pt;
    public int hWnd;
    public int wHitTestCode;
    public int dwExtraInfo;
    } //装置钩子的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); //卸下钩子的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(int idHook);

    //下一个钩挂的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);   public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); /// <summary>
    /// 墨认的构造函数构造当前类的实例.
    /// </summary>
    public MouseHook() 
    {
    //Start();
    } //析构函数.
    ~MouseHook() 

    Stop();
    }  public void Start()
    {
    //安装鼠标钩子
    if(hMouseHook == 0)
    {
    //生成一个HookProc的实例.
    MouseHookProcedure = new HookProc(MouseHookProc); hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //如果装置失败停止钩子
    if(hMouseHook == 0 )
    {
    Stop();
    throw new Exception("SetWindowsHookEx failed.");
    }
    }
    } public void Stop()
    {
    bool retMouse =true;
    if(hMouseHook != 0)
    {
    retMouse = UnhookWindowsHookEx(hMouseHook);
    hMouseHook = 0;


    //如果卸下钩子失败
    if (!(retMouse)) throw new Exception("UnhookWindowsHookEx failed.");
    } private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
    {
    //如果正常运行并且用户要监听鼠标的消息
    if ((nCode >= 0) && (OnMouseActivity!=null)) 
    {
    MouseButtons button=MouseButtons.None;
    int clickCount=0; switch (wParam)
    {
    case WM_LBUTTONDOWN: 
    button=MouseButtons.Left; 
    clickCount=1;
    break;
    case WM_LBUTTONUP: 
    button=MouseButtons.Left; 
    clickCount=1;
    break;
    case WM_LBUTTONDBLCLK: 
    button=MouseButtons.Left; 
    clickCount=2;
    break;
    case WM_RBUTTONDOWN: 
    button=MouseButtons.Right; 
    clickCount=1;
    break;
    case WM_RBUTTONUP: 
    button=MouseButtons.Right; 
    clickCount=1;
    break;
    case WM_RBUTTONDBLCLK: 
    button=MouseButtons.Right; 
    clickCount=2;
    break;
    } //从回调函数中得到鼠标的信息
    MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
    MouseEventArgs e=new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0 );
    OnMouseActivity(this, e);
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam); 
    }
    }
    }
      

  3.   

    我改了你的代码:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Threading;
    using System.Reflection;using System.Runtime.InteropServices;
    namespace HookGlobal
    {
    /// <summary>
    /// AutoSizeForm 的摘要说明。
    /// </summary>
    public class MouseHookForm : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public MouseHookForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.panel1 = new System.Windows.Forms.Panel();
    this.SuspendLayout();
    // 
    // panel1
    // 
    this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.panel1.Location = new System.Drawing.Point(8, 8);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(272, 248);
    this.panel1.TabIndex = 1;
    // 
    // MouseHookForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 270);
    this.Controls.Add(this.panel1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "MouseHookForm";
    this.Text = "AutoSizeForm";
    this.Load += new System.EventHandler(this.FormLoad);
    this.ResumeLayout(false); }
    #endregion Rectangle ret ;
    private System.Windows.Forms.Panel panel1;
    /// <summary>
    /// 这个类可以让你得到一个在运行中程序的所有鼠标事件
    /// 并且引发一个带MouseEventArgs参数的.NET鼠标事件以便你很容易使用这些信息
    /// </summary>
    /// <res>
    /// </res>
    public class MouseHook
    {
    private const int WM_MOUSEMOVE = 0x200;
    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_RBUTTONDOWN = 0x204;
    private const int WM_MBUTTONDOWN = 0x207;
    private const int WM_LBUTTONUP = 0x202;
    private const int WM_RBUTTONUP = 0x205;
    private const int WM_MBUTTONUP = 0x208;
    private const int WM_LBUTTONDBLCLK = 0x203;
    private const int WM_RBUTTONDBLCLK = 0x206;
    private const int WM_MBUTTONDBLCLK = 0x209; //全局的事件
    public event MouseEventHandler OnMouseActivity; static int hMouseHook = 0; //鼠标钩子句柄 //鼠标常量
    public const int WH_MOUSE_LL  = 14; //mouse hook constant HookProc MouseHookProcedure; //声明鼠标钩子事件类型. //声明一个Point的封送类型
    [StructLayout(LayoutKind.Sequential)]
    public class POINT 
    {
    public int x;
    public int y;
    } //声明鼠标钩子的封送结构类型
    [StructLayout(LayoutKind.Sequential)]
    public class MouseHookStruct 
    {
    public POINT pt;
    public int hWnd;
    public int wHitTestCode;
    public int dwExtraInfo;
    } //装置钩子的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); //卸下钩子的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(int idHook);

    //下一个钩挂的函数
    [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);   public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); /// <summary>
    /// 墨认的构造函数构造当前类的实例.
    /// </summary>
    public MouseHook() 
    {
    //Start();
    } //析构函数.
    ~MouseHook() 

    Stop();
    }  public void Start()
    {
    //安装鼠标钩子
    if(hMouseHook == 0)
    {
    //生成一个HookProc的实例.
    MouseHookProcedure = new HookProc(MouseHookProc); hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //如果装置失败停止钩子
    if(hMouseHook == 0 )
    {
    Stop();
    throw new Exception("SetWindowsHookEx failed.");
    }
    }
    } public void Stop()
    {
    bool retMouse =true;
    if(hMouseHook != 0)
    {
    retMouse = UnhookWindowsHookEx(hMouseHook);
    hMouseHook = 0;


    //如果卸下钩子失败
    if (!(retMouse)) throw new Exception("UnhookWindowsHookEx failed.");
    }
    private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
    {
    //如果正常运行并且用户要监听鼠标的消息
    if ((nCode >= 0) && (OnMouseActivity!=null)) 
    {
    MouseButtons button=MouseButtons.None;
    int clickCount=0; switch (wParam)
    {
    case WM_LBUTTONDOWN: 
    button=MouseButtons.Left; 
    clickCount=1;
    break;
    case WM_LBUTTONUP: 
    button=MouseButtons.Left; 
    clickCount=1;
    break;
    case WM_LBUTTONDBLCLK: 
    button=MouseButtons.Left; 
    clickCount=2;
    break;
    case WM_RBUTTONDOWN: 
    button=MouseButtons.Right; 
    clickCount=1;
    break;
    case WM_RBUTTONUP: 
    button=MouseButtons.Right; 
    clickCount=1;
    break;
    case WM_RBUTTONDBLCLK: 
    button=MouseButtons.Right; 
    clickCount=2;
    break;
    } //从回调函数中得到鼠标的信息
    MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
    MouseEventArgs e=new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0 );
    OnMouseActivity(this, e);
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam); 
    }
    } MouseHook actHook;
    void FormLoad(object sender, System.EventArgs e)
    {
    actHook= new MouseHook(); 
    actHook.Start();
    actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
    this.Size = new System.Drawing.Size(500, 244);
    }
    protected override void OnClosing(CancelEventArgs e)
    {
    if (actHook != null)
    actHook.Stop();
    base.OnClosing (e);
    }
    private const int BorderWidth = 5; public void MouseMoved(object sender, MouseEventArgs e)
    {
    ret = new Rectangle(this.Left, this.Top, this.Width, this.Height);

    if ( !ret.Contains(new Point(e.X,e.Y)))
    {
    //鼠标在窗体内
    this.Width = 200;
    }
    else
    {
    //鼠标在窗体外
    this.Width = 500;
    }
    }
      

  4.   


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new MouseHookForm());
    }
    protected override void WndProc(ref Message m)
    {
    const int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
    const int HTCLIENT = 0x1;//工作区
    const int HTCAPTION = 0x2; //标题栏
    switch(m.Msg)
    {
    case WM_NCHITTEST:
    base.WndProc(ref m);
    if (DesignMode)
    {
    return;
    }
    if (((int)m.Result == HTCLIENT ))
    {
    if ((Cursor.Position.X<=this.Left + BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
    m.Result = (IntPtr)13;//左上
    else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=this.Top +BorderWidth))
    m.Result = (IntPtr)14;//右上
    else if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)16;//左下
    else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)17;//右下
    else if (Cursor.Position.X<=this.Left + BorderWidth)
    m.Result = (IntPtr)10;//左
    else if (Cursor.Position.X>=this.Left + this.Width-BorderWidth)
    m.Result = (IntPtr)11;//右
    else if (Cursor.Position.Y<=this.Top + BorderWidth)
    m.Result = (IntPtr)12;//上
    else if (Cursor.Position.Y>=this.Top + this.Height-BorderWidth)
    m.Result = (IntPtr)15;//下
    else 
    m.Result = (IntPtr)HTCAPTION;//移动窗体   
    }
    return;
    default:
    base.WndProc(ref m);
    break;
    }
    }
    }
    }
      

  5.   

    最简单的方法:
    新建一个项目,在窗体Load事件中添加
    private void Form1_Load(object sender, System.EventArgs e)
    {
    this.ControlBox = false;
    this.Text = string.Empty;
    }
    就把以上的问题全解决了,就这么简单的两行代码
    ^_^
      

  6.   

    这一次是彻底的解决了
    public const int WM_SYSCOMMAND=0x0112;
    public const int SC_MOVE=0xF010;
    public const int HTCAPTION=0x0002;
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    public const int WM_SETTEXT = 0xC;
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SendMessageA")]
    public static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, string lParam);
    private void Form1_Load(object sender, System.EventArgs e)
    {
    //使窗体不带标题栏,并且能够改变大小
    this.ControlBox = false;
    this.Text = string.Empty;
    //设置任务栏标题
    SendMessage( this.Handle , WM_SETTEXT , 0 , "^_^" );
    }private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    //窗体移动
    ReleaseCapture();
    SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, null);
    }
      

  7.   

    或者
    设置任务栏标题
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SetWindowText")]
    public static extern int SetWindowText (
    IntPtr hwnd,
    string lpString
    );