繁体   English   中英

如何限制 C# Windows 窗体中的光标移动?

[英]How to restrict cursor movement in a C# Windows form?

有人可以给我一个示例代码来将光标限制在一个表单上。 我发现了这个( ClipCursor API ,说可以使用它来完成)。 我有一个 C# Windows 窗体应用程序并使用 VS 2008。

我的代码:

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;
   }
  }

有一个选项不需要你p / invoke: 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到另一个应用程序来复制一些东西到剪贴板来填写你的表单......请小心。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM