仅在控件中锁定鼠标光标移动

发布于 2024-10-25 16:50:49 字数 389 浏览 1 评论 0原文

我有一个图片框,我使用位图在其上绘制一些内容(每次更改绘图后将位图加载为图片框图像)。现在我有一个“添加注释”按钮。这个按钮位于主窗体中的某个位置。我希望这样,当用户单击此按钮时,鼠标指针会跳转到图片框控件中,并且不会移出它,除非用户在控件内部单击或按 Escape 键。这可能吗?

我目前正在使用此代码:

    private void buttonAddNote_Click(object sender, EventArgs e)
    {
        Cursor = Cursors.Cross;
    }

或者更好的方法可能是在用户单击“添加注释”按钮时,鼠标单击仅在图片框组件内起作用(IE 用户无法单击程序中的其他位置)

I have a picture box which I draw some stuff on it using a bitmap (loading the bitmap as picturebox image after each change of drawing). Now I have a "Add a note" button. this button is somewhere in the mainForm. I want it so when user clicks on this button, the mouse pointer to jump into picturebox control and does not move out of it unless the user has clicked inside the control or pressed Escape key. Is this possible?

I am using this code at the moment:

    private void buttonAddNote_Click(object sender, EventArgs e)
    {
        Cursor = Cursors.Cross;
    }

Or a better approach could be on this time when user clicked add a note button, mouse clicks only work within picturebox component (IE user can not click somewhere else in program)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-11-01 16:50:50

第二,虽然你对用户限制的看法是正确的,所以我想出了这个想法。但非常感谢您向我展示如何锁定用户鼠标!十)

    public addNote = false;

    private void buttonAddNote_Click(object sender, EventArgs e)
    {
        if (!addNote)
            addNote = true;
        else addNote = false;
    }

    private void curveBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (addNote)
        {
            Cursor = Cursors.Cross;
        }
    }

    private void curveBox_MouseLeave(object sender, EventArgs e)
    {
        Cursor = Cursors.Default;
        addNote = false;
    }

On the second though you are right about user limitations, so I came up with this idea. But thanks a lot for showing me how to lock user mouses!!! X)

    public addNote = false;

    private void buttonAddNote_Click(object sender, EventArgs e)
    {
        if (!addNote)
            addNote = true;
        else addNote = false;
    }

    private void curveBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (addNote)
        {
            Cursor = Cursors.Cross;
        }
    }

    private void curveBox_MouseLeave(object sender, EventArgs e)
    {
        Cursor = Cursors.Default;
        addNote = false;
    }
成熟的代价 2024-11-01 16:50:49

用户界面中的这种模式是一个非常糟糕的主意。但 Winforms 确实使之成为可能。您可以使用 Cursor.Clip 属性来限制运动。它不能阻止用户仍然使用键盘快捷键访问“开始”菜单,您需要使用 Capture 属性来检测您是否丢失。

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        pictureBox1.MouseCaptureChanged += new EventHandler(pictureBox1_MouseCaptureChanged);
        pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
        button1.Click += new EventHandler(button1_Click);
    }

    private void button1_Click(object sender, EventArgs e) {
        var rc = pictureBox1.RectangleToScreen(new Rectangle(Point.Empty, pictureBox1.ClientSize));
        Cursor.Position = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
        Cursor.Clip = rc;
        pictureBox1.Capture = true;
        pictureBox1.Cursor = Cursors.Cross;
    }

    void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
        pictureBox1.Capture = false;
    }

    void pictureBox1_MouseCaptureChanged(object sender, EventArgs e) {
        if (!pictureBox1.Capture) {
            pictureBox1.Cursor = Cursors.Default;
            Cursor.Clip = new Rectangle(0, 0, 0, 0);
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Escape) pictureBox1.Capture = false;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Modality like that in a user interface is a pretty bad idea. But Winforms does make it possible. You can use the Cursor.Clip property to restrict motion. It can't prevent the user from still accessing, say, the Start menu with a keyboard shortcut, you need to use Capture property to detect that you lost.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        pictureBox1.MouseCaptureChanged += new EventHandler(pictureBox1_MouseCaptureChanged);
        pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
        button1.Click += new EventHandler(button1_Click);
    }

    private void button1_Click(object sender, EventArgs e) {
        var rc = pictureBox1.RectangleToScreen(new Rectangle(Point.Empty, pictureBox1.ClientSize));
        Cursor.Position = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
        Cursor.Clip = rc;
        pictureBox1.Capture = true;
        pictureBox1.Cursor = Cursors.Cross;
    }

    void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
        pictureBox1.Capture = false;
    }

    void pictureBox1_MouseCaptureChanged(object sender, EventArgs e) {
        if (!pictureBox1.Capture) {
            pictureBox1.Cursor = Cursors.Default;
            Cursor.Clip = new Rectangle(0, 0, 0, 0);
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Escape) pictureBox1.Capture = false;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文