我是C#的新手,我遇到了两个错误。我从这里得到了这段代码,并把它放在我的应用程序中。我已经尝试更换鼠标功能,但从未成功,我不确定是否
public static extern void mouse_event
位于正确的位置。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{ //Here it expects "}"
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick()
{
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
}
}
}//Here the error is "type or namespace definition, or end-of-file expected
答案 0 :(得分:2)
由于您在}
之后编写了public const int MOUSEEVENTF_LEFTDOWN = 0x02;
,并且{
关键字无法在方法正文中显示,因此预计会public
。局部常数是......好的......本地的,不是公共的。
对于MouseClick
,您不能使用此语法在方法内编写方法,您只能编写lambdas和内联委托。
你的意思是?
private void button2_Click(object sender, EventArgs e)
{
const int MOUSEEVENTF_LEFTDOWN = 0x02;
const int MOUSEEVENTF_LEFTUP = 0x04;
const int MOUSEEVENTF_RIGHTDOWN = 0x08;
const int MOUSEEVENTF_RIGHTUP = 0x10;
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
或者是这个?
private void button2_Click(object sender, EventArgs e)
{
MouseClick();
}
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick()
{
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
答案 1 :(得分:1)
问题1:
您无法在方法
public const
个字段
private void button2_Click(object sender, EventArgs e)
{ //Here it expects "}"
public const int MOUSEEVENTF_LEFTDOWN = 0x02; //error
Access modifiers(public
,private
...)是用于指定成员或类型的已声明可访问性的关键字,它们不应在方法内使用。在const定义之前删除public
关键字将解决问题。如果要从类中的其他方法或从类外部访问常量,则应将它们移到类中。
问题2:
您正在另一个方法中定义一个方法,但您不允许这样做。 MouseClick
在button2_Click
内声明,这是不合法的。
尝试将您的代码更改为此
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
private void button2_Click(object sender, EventArgs e)
{
MouseClick();
}
public void MouseClick()
{
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
答案 2 :(得分:0)
您必须将MouseClick
方法和常量声明移到Form1
类的范围内:
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick()
{
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
private void button2_Click(object sender, EventArgs e)
{ //Here it expects "}"
MouseClick();
}
}