如题。我让chatgpt帮忙写一个功能简单的小程序,代码只有几十行,但是安装vs,还有net sdk都试了,还是没学会使用。太复杂的软件界面眼花缭乱,功能都找不到在哪。
在线的编译器也试了不少,都只能在线运行,但没有生成exe的功能。
所以想求一个比较简单的工具,能像记事本一样简单,把代码复制进去,点击 生成exe ,就能直接生成。
这是个能用cmd调用该exe直接控制鼠标在屏幕上点击的小工具。下面是代码,希望有大佬能帮我生成一下,谢谢。用c#是因为gpt推荐的。它说这个需求用c#最高效。因为我确实不会编程,只是需要这个小功能,
using System.Runtime.InteropServices;
using System.Windows.Forms; // 引用System.Windows.Forms以使用Cursor.Position
class Program
{
// Win32 API 声明
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, ref POINT lpPoints, uint cPoints);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr WindowFromPoint(int x, int y);
[DllImport("user32.dll")]
static extern IntPtr PostMessage(IntPtr hWnd, int Msg, UIntPtr wParam, UIntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, UIntPtr lParam);
[DllImport("gdi32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public int X;
public int Y;
}
private const int WM_LBUTTONDOWN = 0x0201; // 鼠标左键按下消息
private const int WM_LBUTTONUP = 0x0202; // 鼠标左键抬起消息
private const int WM_RBUTTONDOWN = 0x0204; // 鼠标右键按下消息
private const int WM_RBUTTONUP = 0x0205; // 鼠标右键抬起消息
private const int HWND_BROADCAST = 0xFFFF; // 发送消息到所有窗口
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: MouseClick <left|right> <X> <Y>");
return;
}
string buttonType = args[0].ToLower();
int X = int.Parse(args[1]);
int Y = int.Parse(args[2]);
// 根据DPI调整坐标
ScaleCoordinates(ref X, ref Y);
// 移动鼠标到指定位置
SetCursorPos(X, Y);
// 发送鼠标点击消息
if (buttonType == "left")
{
SendMouseClick(WM_LBUTTONDOWN, WM_LBUTTONUP, X, Y);
}
else if (buttonType == "right")
{
SendMouseClick(WM_RBUTTONDOWN, WM_RBUTTONUP, X, Y);
}
else
{
Console.WriteLine("Invalid button type. Use 'left' or 'right'.");
}
}
// 根据系统DPI设置调整坐标
static void ScaleCoordinates(ref int x, ref int y)
{
float dpiScale = GetDpiScaleFactor();
x = (int)(x * dpiScale);
y = (int)(y * dpiScale);
}
// 获取DPI缩放因子
static float GetDpiScaleFactor()
{
float dpiScale = 1.0f;
try
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
dpiScale = g.DpiX / 96f; // 96 DPI是Windows的默认值
}
}
catch (Exception ex)
{
Console.WriteLine("Error getting DPI scale factor: " + ex.Message);
}
return dpiScale;
}
// 发送鼠标点击消息
static void SendMouseClick(int downMsg, int upMsg, int x, int y)
{
PostMessage(IntPtr.Zero, downMsg, new UIntPtr(0), new UIntPtr(MAKELPARAM(x, y)));
PostMessage(IntPtr.Zero, upMsg, new UIntPtr(0), new UIntPtr(MAKELPARAM(x, y)));
}
// Win32 API: 将逻辑坐标转换为物理坐标
static void ConvertToPhysicalCoordinates(ref int x, ref int y)
{
IntPtr desktopHwnd = GetDC(IntPtr.Zero);
POINT point = new POINT { X = x, Y = y };
MapWindowPoints(desktopHwnd, IntPtr.Zero, ref point, 1);
ReleaseDC(IntPtr.Zero, desktopHwnd);
x = point.X;
y = point.Y;
}
// Win32 API: MAKELPARAM宏
static UIntPtr MAKELPARAM(int low, int high)
{
return new UIntPtr((uint)((low & 0xFFFF) | ((high & 0xFFFF) << 16)));
}
}
在此处键入或粘贴代码