Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在鼠标光标的当前位置显示新窗体?_C#_Winforms - Fatal编程技术网

C# 如何在鼠标光标的当前位置显示新窗体?

C# 如何在鼠标光标的当前位置显示新窗体?,c#,winforms,C#,Winforms,代码如下: private void hsMagnfier_OnMouseDown(object sender) { int x = mLastCursorPosition.X; int y = mLastCursorPosition.Y; MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPositi

代码如下:

private void hsMagnfier_OnMouseDown(object sender)
{
    int x = mLastCursorPosition.X;
    int y = mLastCursorPosition.Y;
    MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
    magnifier.Show();
}
上面的代码是一种
格式
,我可以在屏幕上拖动它

然后,当我点击一个图标时,它正在执行
放大镜.Show()
和放大镜窗体显示在鼠标当前位置的位置

但是如果我再次点击它,那么现在新表单的位置放大镜在我的
Form1
中心。而不是第一次鼠标当前位置的位置

这是
放大镜窗体
代码,可能是它第一次处于当前鼠标位置,但下一次/s它位于
窗体1
的中心

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace ScreenVideoRecorder
{
    public partial class MagnifierForm : Form
    {

        public MagnifierForm(Configuration configuration, Point startPoint)
        {
            InitializeComponent();

            //--- My Init ---
            mConfiguration = configuration;
            FormBorderStyle = FormBorderStyle.None;

            ShowInTaskbar = mConfiguration.ShowInTaskbar;
            TopMost = mConfiguration.TopMostWindow;
            Width = mConfiguration.MagnifierWidth;
            Height = mConfiguration.MagnifierHeight;

            // Make the window (the form) circular
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(ClientRectangle);
            Region = new Region(gp);

            mImageMagnifier = Properties.Resources.magnifierGlass;

            mTimer = new Timer();
            mTimer.Enabled = true;
            mTimer.Interval = 20;
            mTimer.Tick += new EventHandler(HandleTimer);

            mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                     Screen.PrimaryScreen.Bounds.Height);

            mStartPoint = startPoint;
            mTargetPoint = startPoint;

            if (mConfiguration.ShowInTaskbar)
                ShowInTaskbar = true;
            else
                ShowInTaskbar = false;
        }

        protected override void OnShown(EventArgs e)
        {
            RepositionAndShow();
        }

        private delegate void RepositionAndShowDelegate();

        private void RepositionAndShow()
        {
            if (InvokeRequired)
            {
                Invoke(new RepositionAndShowDelegate(RepositionAndShow));
            }
            else
            {
                // Capture the screen image now!
                Graphics g = Graphics.FromImage(mScreenImage);
                g.CopyFromScreen(0, 0, 0, 0, new Size(mScreenImage.Width, mScreenImage.Height));
                g.Dispose();                

                if (mConfiguration.HideMouseCursor)
                    Cursor.Hide();
                else
                    Cursor = Cursors.Cross;

                Capture = true;

                if (mConfiguration.RememberLastPoint)
                {
                    mCurrentPoint = mLastMagnifierPosition;
                    Cursor.Position = mLastMagnifierPosition;
                    Left = (int)mCurrentPoint.X - Width / 2;
                    Top = (int)mCurrentPoint.Y - Height / 2;
                }
                else
                {
                    mCurrentPoint = Cursor.Position;
                }
                Show();
            }
        }

        void HandleTimer(object sender, EventArgs e)
        {
            float dx = mConfiguration.SpeedFactor * (mTargetPoint.X - mCurrentPoint.X);
            float dy = mConfiguration.SpeedFactor * (mTargetPoint.Y - mCurrentPoint.Y);

            if (mFirstTime)
            {
                mFirstTime = false;

                mCurrentPoint.X = mTargetPoint.X;
                mCurrentPoint.Y = mTargetPoint.Y;

                Left = (int)mCurrentPoint.X - Width / 2;
                Top = (int)mCurrentPoint.Y - Height / 2;

                return;
            }

            mCurrentPoint.X += dx;
            mCurrentPoint.Y += dy;

            if (Math.Abs(dx) < 1 && Math.Abs(dy) < 1)
            {
                mTimer.Enabled = false;
            }
            else
            {
                // Update location
                Left = (int)mCurrentPoint.X - Width / 2;
                Top = (int)mCurrentPoint.Y - Height / 2;
                mLastMagnifierPosition = new Point((int)mCurrentPoint.X, (int)mCurrentPoint.Y);
            }

            Refresh();
        }


        protected override void OnMouseDown(MouseEventArgs e)
        {
            mOffset = new Point(Width / 2 - e.X, Height / 2 - e.Y);
            mCurrentPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
            mTargetPoint = mCurrentPoint;
            mTimer.Enabled = true;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (mConfiguration.CloseOnMouseUp)
            {
                Close();
                mScreenImage.Dispose();
            }

            Cursor.Show();
            Cursor.Position = mStartPoint;            
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mTargetPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
                mTimer.Enabled = true;
            } 
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (mConfiguration.DoubleBuffered)
            {
                // Do not paint background (required for double buffering)!
            }
            else
            {
                base.OnPaintBackground(e);
            }
        }

        protected override void  OnPaint(PaintEventArgs e)
        {
            if (mBufferImage == null)
            {
                mBufferImage = new Bitmap(Width, Height);
            }
            Graphics bufferGrf = Graphics.FromImage(mBufferImage);

            Graphics g;

            if (mConfiguration.DoubleBuffered)
            {
                g = bufferGrf;
            }
            else
            {
                g = e.Graphics;
            }

            if (mScreenImage != null)
            {
                Rectangle dest = new Rectangle(0, 0, Width, Height);
                int w = (int)(Width / mConfiguration.ZoomFactor);
                int h = (int)(Height / mConfiguration.ZoomFactor);
                int x = Left - w / 2 + Width / 2;
                int y = Top - h / 2 + Height / 2;

                g.DrawImage(
                    mScreenImage,
                    dest,
                    x, y,
                    w, h,
                    GraphicsUnit.Pixel);
            }

            if (mImageMagnifier != null)
            {
                g.DrawImage(mImageMagnifier, 0, 0, Width, Height);
            }

            if (mConfiguration.DoubleBuffered)
            {
                e.Graphics.DrawImage(mBufferImage, 0, 0, Width, Height);
            }      
        }


        //--- Data Members ---
        #region Data Members
        private Timer mTimer;
        private Configuration mConfiguration;
        private Image mImageMagnifier;
        private Image mBufferImage = null;
        private Image mScreenImage = null;
        private Point mStartPoint;
        private PointF mTargetPoint;
        private PointF mCurrentPoint;
        private Point mOffset;
        private bool mFirstTime = true;
        private static Point mLastMagnifierPosition = Cursor.Position;
        #endregion
    }
}

再说一遍。奇怪。

Ok发现执行此操作的部分以放大镜形式出现:

mConfiguration.RememberLastPoint = false;
if (mConfiguration.RememberLastPoint)
{
    mCurrentPoint = mLastMagnifierPosition;
    Cursor.Position = mLastMagnifierPosition;
    Left = (int)mCurrentPoint.X - Width / 2;
    Top = (int)mCurrentPoint.Y - Height / 2;
}
else
{
    mCurrentPoint = Cursor.Position;
}

所以我现在添加了一行:
mConfiguration.RememberLastPoint=false现在是哪个做的。

假设您有两种表单-
主表单
子表单

this.Location = pntLocation;
如果您在
MouseUp
事件(例如)上从Master调用Child,请在
Master
窗体的
MouseUp
事件中写入代码

ChildForm obj=new ChildForm();
obj.pntLocation = new Point(Cursor.Position.X, Cursor.Position.Y);
obj.ShowDialog();
在子对象内为位置声明变量

public Point pntLocation;
现在在
子项的
表单加载
中设置位置

this.Location = pntLocation;

第一次在鼠标光标的当前位置显示放大镜窗体时再次选中,但下一次在上次显示放大镜窗体的位置显示新的放大镜窗体。如果我在屏幕上移动放大镜窗体并将其放在下一次的某个位置,我将再次显示放大镜,它将位于我最后一次将其放在的位置,而不是鼠标光标所在的位置。每次单击以显示放大镜窗体时,我都会再次检查它正在执行System.Windows.Forms.Cursor.Position操作,但会将其显示在上次所在的位置,而不是鼠标光标所在的位置。某些情况会阻止放大镜窗体显示鼠标光标当前所在的位置,并使其显示上次放大镜所在的位置。有些东西需要重置,可能是配置问题。