How do i make that the new Form will start in the position of the mouse cursor on screen ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Not only on the Form1 area but if my mouse now i put it in the screen somewhere for example at 0,0 or 167,34 so the new Form will open at 0,0 or at 167.34
In the new Form i did 3 constructors one of the for the test is getting the start position parameter:public MagnifierForm()
{
InitializeComponent();

_doMove = true;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
Width = 150;
Height = 150;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
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 = new Point(500, 500);
mTargetPoint = new Point(500, 500);
speed = 0.35F;
}

public MagnifierForm(bool MoveTheGlass, bool InTaskBar, int MagnifierWidth, int MagnifierHeight, Point StartPoint, float SpeedFactor)
{
InitializeComponent();

FormBorderStyle = FormBorderStyle.None;
TopMost = true;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
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);

_doMove = MoveTheGlass;
ShowInTaskbar = InTaskBar;
Width = MagnifierWidth;
Height = MagnifierHeight;
mStartPoint = StartPoint;
mTargetPoint = StartPoint;
speed = SpeedFactor;

}

public MagnifierForm(Point MousePosition)
{
mStartPoint = MousePosition;
mTargetPoint = MousePosition;
}

In Form1 i tried to do:protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.M))
{
if (mf == null)
{
mf = new Magnifier20070401.MagnifierForm(System.Windows.Forms.Cursor.Position);
mf.Show();

this.Select();
}
else if (mf.IsDisposed)
{
mf = new Magnifier20070401.MagnifierForm(System.Windows.Forms.Cursor.Position);
mf.Show();
}
else
{
mf.Close();
mf = null;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

Used Cursor.Position
But when im running the application and click Ctrl+M i see a new Form square empty and after a second or less im getting in the opened form an exception:
Value cannot be nullSystem.ArgumentNullException was unhandled
HResult=-2147467261
Message=Value cannot be null.
Parameter name: image
Source=System.Drawing
ParamName=image
StackTrace:
at System.Drawing.Graphics.FromImage(Image image)
at Magnifier20070401.MagnifierForm.RepositionAndShow() in d:C-SharpScreenVideoRecorderScreenVideoRecorderScreenVideoRecorderMagnifierForm.cs:line 116
at Magnifier20070401.MagnifierForm.OnShown(EventArgs e) in d:C-SharpScreenVideoRecorderScreenVideoRecorderScreenVideoRecorderMagnifierForm.cs:line 103
at System.Windows.Forms.Form.CallShownEvent()
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ScreenVideoRecorder.Program.Main() in d:C-SharpScreenVideoRecorderScreenVideoRecorderScreenVideoRecorderProgram.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

This is the new Form code: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 Magnifier20070401
{
public partial class MagnifierForm : Form
{
bool _doMove;

public MagnifierForm()
{
InitializeComponent();

_doMove = true;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
Width = 150;
Height = 150;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
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 = new Point(500, 500);
mTargetPoint = new Point(500, 500);
speed = 0.35F;
}

public MagnifierForm(bool MoveTheGlass, bool InTaskBar, int MagnifierWidth, int MagnifierHeight, Point StartPoint, float SpeedFactor)
{
InitializeComponent();

FormBorderStyle = FormBorderStyle.None;
TopMost = true;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
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);

_doMove = MoveTheGlass;
ShowInTaskbar = InTaskBar;
Width = MagnifierWidth;
Height = MagnifierHeight;
mStartPoint = StartPoint;
mTargetPoint = StartPoint;
speed = SpeedFactor;

}

public MagnifierForm(Point MousePosition)
{
mStartPoint = MousePosition;
mTargetPoint = MousePosition;
}

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

private delegate void RepositionAndShowDelegate();

private void RepositionAndShow()
{
if (InvokeRequired)
{
Invoke(new RepositionAndShowDelegate(RepositionAndShow));
}
else
{
Graphics g = Graphics.FromImage(mScreenImage);
g.CopyFromScreen(0, 0, 0, 0, new Size(mScreenImage.Width, mScreenImage.Height));
g.Dispose();

Cursor = Cursors.Cross;
Capture = true;
mCurrentPoint = Cursor.Position;
Show(); // to add here the bool of the Mouse Cursor to hide or not to hide
}
}

void HandleTimer(object sender, EventArgs e)
{
float dx = speed * (mTargetPoint.X - mCurrentPoint.X);
float dy = speed * (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 OnMouseMove(MouseEventArgs e)
{
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);
}*/
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 / 3.0);//mConfiguration.ZoomFactor);
int h = (int)(Height / 3.0);//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);
}
e.Graphics.DrawImage(mBufferImage, 0, 0, Width, Height);
}


//--- Data Members ---
#region Data Members
private float speed;
private Timer mTimer;
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


// New code \

protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);

Point pt = Control.MousePosition;
int eX = pt.X - this.Left;
int eY = pt.Y - this.Top;

mOffset = new Point(0, 0);
mCurrentPoint = PointToScreen(new Point(eX + mOffset.X, eY + mOffset.Y));
mTargetPoint = mCurrentPoint;
mTimer.Enabled = true;
this.Capture = true;
}

protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);

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

protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
this._doMove = !this._doMove;

//just to show that it follows when the bg is green
if (_doMove)
this.BackColor = Color.Green;
else
this.BackColor = SystemColors.Control;
}

The exception is on the line:Graphics g = Graphics.FromImage(mScreenImage);

What i want is that no matter where the mouse cursor is now on screen the new Form will be open at this mouse cursor location.

View the full article
 
Back
Top