2011-06-26 74 views
0

有人可以給我一個示例代碼來將光標限制在窗體上。我發現這(ClipCursor API,這表示可以使用它來完成)。我有一個C#Windows窗體應用程序和使用VS 2008如何限制C#Windows窗體中的光標移動?

我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

    } 


    private void Form1_CursorChanged(object sender, EventArgs e) 
    { 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
      MoveCursor(); 

    } 

    private void Form1_Resize(object sender, EventArgs e) 
    { 
     MoveCursor(); 
    } 

    private void Form1_LocationChanged(object sender, EventArgs e) 
    { 
     MoveCursor(); 
    } 

    private void MoveCursor() 
    { 
     Cursor.Clip = Bounds; 
     this.Capture = true; 
    } 
    } 
} 

Form1.Designer.cs

namespace WindowsFormsApplication1 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.SuspendLayout(); 
     // 
     // textBox1 
     // 
     this.textBox1.AutoCompleteCustomSource.AddRange(new string[] { 
     "all", 
     "allah", 
     "allo"}); 
     this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; 
     this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; 
     this.textBox1.Location = new System.Drawing.Point(30, 70); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(227, 20); 
     this.textBox1.TabIndex = 0; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 264); 
     this.Controls.Add(this.textBox1); 
     this.Cursor = System.Windows.Forms.Cursors.No; 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.CursorChanged += new System.EventHandler(this.Form1_CursorChanged); 
     this.Load += new System.EventHandler(this.Form1_Load); 
     this.Resize += new System.EventHandler(this.Form1_Resize); 
     this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged); 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

    } 

    #endregion 

    private System.Windows.Forms.TextBox textBox1; 
    } 
    } 

回答

2

有不需要你爲P選項/調用:Cursor.Clip

編輯:新代碼。在單個文件中完成表單代碼。

using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 
    public Form1()  { 
     InitializeComponent(); 
     } 
     private void Form1_Activate(object sender, EventArgs e)  { 
      MoveCursor(); 
     } 
     private void Form1_Resize(object sender, EventArgs e)  { 
     MoveCursor(); 
    } 
     private void Form1_LocationChanged(object sender, EventArgs e)  { 
     MoveCursor(); 
    } 
     private void MoveCursor() 
     { 
     this.Capture = true; 
     System.Windows.Forms.Cursor.Clip = Bounds; 
    } 

    private void InitializeComponent() 
    { 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.SuspendLayout(); 
     this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {"all", "allak", "allo"}); 
     this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; 
     this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; 
     this.textBox1.Location = new System.Drawing.Point(30, 70); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(227, 20); 
     this.textBox1.TabIndex = 0; 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 264); 
     this.Controls.Add(this.textBox1); 
     this.Cursor = System.Windows.Forms.Cursors.No; 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Activated += new System.EventHandler(this.Form1_Activate); 
     this.Resize += new System.EventHandler(this.Form1_Resize); 
     this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged); 
     this.ResumeLayout(false); 
     this.PerformLayout(); 
     } 
     protected override void Dispose(bool disposing) 
     { 
     if (disposing && (components != null)) 
      components.Dispose(); 
     base.Dispose(disposing); 
     } 
     private System.ComponentModel.IContainer components = null; 

     private System.Windows.Forms.TextBox textBox1 = null; 
    } 
} 

除非這就像一個小亭特別鎖定的計算機,用戶可能會討厭它。考慮他們是否需要將另一個應用程序的Alt + Tab複製到剪貼板來填寫表單...請小心。

+0

我該如何利用這種方法?我應該在哪裏包含這些代碼,以及我應該從哪裏調用此方法?我正在尋找一個代碼,用戶只能在自己的應用程序上移動鼠標。我正在使用VS 2008.和它的一個表單應用程序。 用戶應該只能在應用程序上移動鼠標,而不能在任何桌面組件上移動。可以使用上面的代碼來完成嗎? – illep

+0

如果是我,我只能從Form_Load和Form_LocationChanged調用它。希望這隻在Modal對話框啓動時纔有效。 '使用(var frm = new MyForm()){frm.ShowDialog(); Cursor.Clip = Screen.Bounds;}'。另外,您需要捕獲鼠標。我會將內容添加到我的答案中。 –

+0

我仍然無法找到解決此問題的方法,鼠標仍然可以在窗體(C#窗體窗體應用程序)外部可見。幫助讚賞:( – illep