C# 如何计算鼠标点击的正确位置
我有两个程序: 一个是服务器-受控制的 第二个是客户端-控制器 基本上它是一个远程桌面程序 来自客户端的视图是: 红线是电脑窗口的大小 蓝线是程序本身 并且picturebox被最大化并设置为与程序窗口大小相同 它旁边的图片正在重新调整大小,这样它将填满大部分图片盒,但仍保持相同的纵横比 发送协议是: 客户端完成所有计算,并通过鼠标单击x和y向服务器发送数据 服务器获取x和y并单击使用它们 我用来获取鼠标位置的代码是:C# 如何计算鼠标点击的正确位置,c#,winforms,C#,Winforms,我有两个程序: 一个是服务器-受控制的 第二个是客户端-控制器 基本上它是一个远程桌面程序 来自客户端的视图是: 红线是电脑窗口的大小 蓝线是程序本身 并且picturebox被最大化并设置为与程序窗口大小相同 它旁边的图片正在重新调整大小,这样它将填满大部分图片盒,但仍保持相同的纵横比 发送协议是: 客户端完成所有计算,并通过鼠标单击x和y向服务器发送数据 服务器获取x和y并单击使用它们 我用来获取鼠标位置的代码是: [DllImport("user32.dll")] [return
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePoint lpMousePoint);
还有一件事-服务器和客户端同步屏幕大小
我根本无法让鼠标点击所需的位置,许多人都试图告诉我
该算法需要能够计算鼠标单击pic相对于服务器的准确位置
示例-我按下pictureBox窗口上的X按钮-我需要它单击服务器端的同一个X按钮
我使用的重新调整大小的代码:
public Image resizeImage(int newWidth, int newHeight, Image imgPhoto)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
imgPhoto.Dispose();
return bmPhoto;
}
据我所知,我的更改解决功能太复杂了我不知道,但您的大小调整似乎太复杂了。如果你一开始就改变了高度和宽度,那么长宽比怎么可能是相同的呢?你的尺寸调整工作正常吗?是的,没有问题