C# 如何通过点击鼠标退出进程?

C# 如何通过点击鼠标退出进程?,c#,C#,我是C#新手,刚开始写一些代码。 我有一些想法,但在开始之前,我需要在这件事上得到一些帮助。 如何通过检测鼠标单击退出正在运行的进程? 我写了一些行,但在编译和运行时,鼠标点击根本没有效果。 谁能帮我看看吗? 这是我的台词 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; u

我是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.
            }

    }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间图形测试1
{
公共部分类Form1:Form
{
公共静态单极限x,极限y;//x&y中的极限
公共静态int dly=45;
System.Drawing.Pen myPen=新的System.Drawing.Pen(System.Drawing.Color.Blue);//笔的颜色
图形画布;
公共布尔clik=false;//初始化
公共字符串mystring;
公共表格1()
{
初始化组件();
}
鼠标单击时受保护的覆盖无效(鼠标目标e)
{
base.OnMouseClick(e);
clik=true;
lim_x=e.x;
lim_y=e.y;
}
私有void btgo\u单击(对象发送方,事件参数e)//开始绘图[GO]
{
Canvas=this.CreateGraphics();
btgo.Enabled=false;
MessageBox.Show(“检查画布的限制。\r\n”+
“单击任意位置停止并查找X位置”,
“监视坐标”,
MessageBoxButtons.OK,MessageBoxIcon.Information);
btgo.Visible=false;
btend.Enabled=false;
myPen.Color=System.Drawing.Color.Red;//笔的颜色
myPen.Width=2;//笔宽
System.Drawing.Font drawfont=新的System.Drawing.Font(“Arial”,10);//图形字体
System.Drawing.SolidBrush mybrush=新的System.Drawing.SolidBrush(System.Drawing.Color.Black);//字体的颜色
系统、绘图、颜色背景;
background=this.BackColor;
lim_x=0;//初始化
做
{
帆布.抽绳(myPen,0,200,lim_x,200);
mystring=“Current X=“+lim_X.ToString();
mybrush.Color=System.Drawing.Color.Black;
DrawString(mystring,drawfont,mybrush,35134);
系统。线程。线程。睡眠(dly);
mybrush.Color=background;//使用背景色
填充矩形(mybrush,新矩形(350333500353));
如果(clik)
{
mybrush.Color=background;//使用背景色
填充矩形(mybrush,新矩形(350333500353));
mystring=“Current X=“+lim_X.ToString();
mybrush.Color=System.Drawing.Color.Black;
DrawString(mystring,drawfont,mybrush,35134);
MessageBox.Show(“X中的最终位置=“+lim_X.ToString(),
“检测到鼠标点击”,
MessageBoxButtons.OK,MessageBoxIcon.Stop);
打破
}
其他的
lim_x++;
}而(lim_x<611);
myPen.Dispose();//释放画笔
mybrush.Dispose();//释放笔刷
Canvas.Dispose();//释放画布
btend.Enabled=true;
btend.Focus();
}
私有void btend_单击(对象发送者,事件参数)//退出程序[END]
{
Dispose();//程序结束。
}
}

}

如果要关闭表单,请使用
this.close()
。如果要退出应用程序,可以使用
application.exit()

如果我理解得很清楚,您正在尝试停止它正在运行的进程。(例如btgo点击事件)

为此,您应该使用一个单独的
线程
来执行该过程

为什么使用线程?

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

  • 线程
  • 第二个
    线程
  • 因此,在运行第二个
    线程时,可以识别“鼠标点击”

    例如,我有一个
    按钮
    和一个
    标签
    。我想在
    按钮1上单击,创建并启动
    线程。此
    线程将循环10000次并修改
    标签1
    文本。但当我单击标签时,循环将停止:

    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();
        }
    }
    
    使用系统;
    使用系统图;
    使用System.Windows.Forms;
    使用系统线程;
    公共部分类测试:表单
    {
    螺纹;
    公开考试()
    {
    初始化组件();
    }
    无效按钮1\u单击(对象发送者,事件参数e)
    {
    线程=新线程(新线程开始(StartThread));
    thread.Start();
    }
    私有void StartThread()
    {
    for(int i=0;i Label1.Text=i.ToString());
    //请参阅更多信息部分,我将发布一个关于此的链接。
    }
    }
    无效标签1_单击(对象发送方,事件参数e)
    {
    thread.Abort();