如何计算鼠标点击的正确位置

时间:2014-06-24 11:33:17

标签: c# winforms

我有2个程序: 一个是服务器 - “受控” 第二个是客户 - “控制器”

基本上是一个远程桌面程序

客户的观点是: enter image description here

红线是PC窗口大小。

蓝线是它自己的程序。

并且图片框最大化并设置为与程序窗口大小相同。

它旁边的图片正在重新调整大小,以便填充大部分图片框,但仍保持相同的宽高比。

发送协议是: 客户端处理所有计算并向服务器发送鼠标单击的x和y 服务器获取x和y并单击使用它们

我用来获取鼠标位置的代码是:

[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetCursorPos(out MousePoint lpMousePoint);

还有一件事 - 服务器和客户端同步他们的屏幕尺寸

我无法让鼠标点击所需的位置(许多人试图告诉)

算法需要能够计算鼠标相对于服务器在pic上单击的确切位置 例子 - 我按下图片框中窗口上的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;
    }

据我所知,我的更改解析功能过于复杂

0 个答案:

没有答案