如何通过鼠标单击退出进程?

时间:2013-06-20 17:40:13

标签: c#

我是C#的新手,刚刚开始写一些代码。 我有一些想法,但在继续之前,需要一些帮助。 如何通过检测鼠标单击退出正在运行的进程? 我写了一些行但是在编译和运行时,鼠标点击完全没有效果。 有人可以看看并帮助我吗? 这是我的台词......

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 Graphic_test_1
{
    public partial class Form1 : Form
    {
            public static Single lim_x, lim_y; // limits in X & Y
            public static int dly = 45;
            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Blue); // color of the pen
            System.Drawing.Graphics Canvas;
            public Boolean clik = false; // initialize
            public string mystring;
            public Form1()
            {
                    InitializeComponent();
            }

            protected override void OnMouseClick(MouseEventArgs e)
            {
                    base.OnMouseClick(e);
                    clik = true;
                    lim_x = e.X;
                    lim_y = e.Y;
            }

            private void btgo_Click(object sender, EventArgs e) // Start drawing  [GO]
            {
                    Canvas = this.CreateGraphics();
                    btgo.Enabled = false;
                    MessageBox.Show("Checking the limits of the canvas.\r\n" +
                            "Click anywhere to stop and find out X position",
                            "Watching Coordinates",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btgo.Visible = false;
                    btend.Enabled = false;

                    myPen.Color=System.Drawing.Color.Red; // color of the pen
                    myPen.Width = 2; // pen width
                    System.Drawing.Font drawfont = new System.Drawing.Font("Arial", 10); // Graphics font
                    System.Drawing.SolidBrush mybrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); // color for the font
                    System.Drawing.Color background;
                    background = this.BackColor;

                    lim_x = 0; // initialize
                    do
                    {
                            Canvas.DrawLine(myPen, 0, 200, lim_x, 200);
                            mystring = "Current X = " + lim_x.ToString();
                            mybrush.Color = System.Drawing.Color.Black;
                            Canvas.DrawString(mystring, drawfont, mybrush, 351, 334);
                            System.Threading.Thread.Sleep(dly);
                            mybrush.Color = background; // use the background color
                            Canvas.FillRectangle(mybrush, new Rectangle(350, 333, 500, 353));
                            if (clik)
                            {
                                    mybrush.Color = background; // use the background color
                                    Canvas.FillRectangle(mybrush, new Rectangle(350, 333, 500, 353));
                                    mystring = "Current X = " + lim_x.ToString();
                                    mybrush.Color = System.Drawing.Color.Black;
                                    Canvas.DrawString(mystring, drawfont, mybrush, 351, 334);
                                    MessageBox.Show("Final position in X = " + lim_x.ToString(),
                                            "Mouse click detected",
                                            MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    break;
                            }
                            else
                                    lim_x++;
                    } while (lim_x < 611);

                    myPen.Dispose(); // release the pen
                    mybrush.Dispose(); // release the brush
                    Canvas.Dispose(); // release the canvas
                    btend.Enabled = true;
                    btend.Focus();
            }

            private void btend_Click(object sender, EventArgs e) // quit program  [END]
            {
                    Dispose(); // program ends.
            }

    }

}

2 个答案:

答案 0 :(得分:3)

如果要关闭表单,请使用this.Close()。如果您想退出申请,可以使用Application.Exit()

答案 1 :(得分:0)

如果我理解得很好,你就试图阻止它正在运行的进程。 (例如,btgo点击事件)。

为此,您应该使用个人Thread来执行该过程。

为何选择主题?

您的应用程序将运行两个不同的过程:

  1. Thread
  2. 第二个Thread
  3. 因此,当您运行第二个Thread时,可以识别“鼠标点击”。

    E.g。我有一个Button和一个Label。我希望,在Button1点击后,创建并启动Thread。此Thread将循环10000次并修改Label1文本。但是当我点击标签时,循环将停止:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;
    
    public partial class Test : Form
    {
    
        Thread thread;
        public Test()
        {
            InitializeComponent();
    
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ThreadStart(StartThread));
            thread.Start();
        }
    
        private void StartThread()
        {
            for(int i =0;i<1000000;i++)
            {
                Thread.Sleep(1000);
                Label1.Invoke((MethodInvoker)(() => Label1.Text = i.ToString())); 
                //See the more information section, I will post a link about this.
    
            }
        }
    
        void Label1_Click(object sender, EventArgs e)
        {
            thread.Abort();
        }
    }
    

    更多信息:

    1. 您应该阅读有关如何正确关闭帖子here的信息。

    2. 调用方法here