using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
private Rectangle oldRect;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

Console.WriteLine(this.panel1.Location); //
// 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.label1 = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
// 
// panel1
// 
this.panel1.BackColor = System.Drawing.Color.Red;
this.panel1.Location = new System.Drawing.Point(104, 56);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(208, 184);
this.panel1.TabIndex = 0;
this.panel1.MouseEnter += new System.EventHandler(this.OnMouseEnter);
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 40);
this.label1.TabIndex = 1;
this.label1.Text = "注意!您的鼠标进入红色区域后,将被锁定在此区域内,按Ctrl + L解锁.";
// 
// mainMenu1
// 
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem1});
// 
// menuItem1
// 
this.menuItem1.Index = 0;
this.menuItem1.Shortcut = System.Windows.Forms.Shortcut.CtrlL;
this.menuItem1.Text = "UnLock";
this.menuItem1.Visible = false;
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 309);
this.Controls.Add(this.label1);
this.Controls.Add(this.panel1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void OnMouseEnter(object sender, System.EventArgs e)
{
Rectangle r = new Rectangle(this.panel1.Location,this.panel1.Size);
r.X += this.Location.X;
r.Y += this.Location.Y + SystemInformation.CaptionHeight;
oldRect = Cursor.Clip;
Cursor.Clip = r;
} private void menuItem1_Click(object sender, System.EventArgs e)
{
Console.WriteLine(oldRect);
Cursor.Clip = SystemInformation.WorkingArea;
this.Activate();
}
}
}

解决方案 »

  1.   

    字段oldRect 没有用到,可以不要.原理很简单,就是利用Cursor的Clip属性,把光标限定在一矩形内即可...
      

  2.   

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public int left;
    public int top;
    public int right;
    public int bottom;
    }
    [System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")] 
    public static extern int ClipCursor(ref RECT lpRect); 用以上函数即可锁定在一个区域。
      

  3.   

    调用api函数ClipCursor声明如下[DllImport("user32.dll", EntryPoint="ClipCursor")]
    public static extern int ClipCursor (
    ref int lpRect
    );