我有这个显示/打开一个新表单的代码:
在 gkh_Keydown 事件中,当我单击 Ctrl + M 时,它正在显示/打开新的 Form 。现在我想这样做,当我再次单击 Ctrl + M 时,它将关闭新的 Form 。
当我单击一次以打开新表单时,它将首先进入此表单:
public MagnifierMainForm(bool showMain)
{
InitializeComponent();
if (showMain == true)
{
GetConfiguration();
//--- My Init ---
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
StartPosition = FormStartPosition.CenterScreen;
mImageMagnifierMainControlPanel = Properties.Resources.magControlPanel20061222;
if (mImageMagnifierMainControlPanel == null)
throw new Exception("Resource cannot be found!");
Width = mImageMagnifierMainControlPanel.Width;
Height = mImageMagnifierMainControlPanel.Height;
HotSpot hsConfiguration = new HotSpot(new Rectangle(50, 15, 35, 30));
hsConfiguration.OnMouseDown += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseDown);
hsConfiguration.OnMouseUp += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseUp);
hsConfiguration.OnMouseMove += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseMove);
HotSpot hsMagnfier = new HotSpot(new Rectangle(10, 15, 30, 30));
hsMagnfier.OnMouseMove += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseMove);
hsMagnfier.OnMouseDown += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseDown);
hsMagnfier.OnMouseUp += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseUp);
HotSpot hsExit = new HotSpot(new Rectangle(95, 20, 15, 15));
hsExit.OnMouseUp += new HotSpot.MouseEventDelegate(hsExit_OnMouseUp);
mHotSpots.Add(hsConfiguration);
mHotSpots.Add(hsMagnfier);
mHotSpots.Add(hsExit);
ShowInTaskbar = false;
this.Show();
}
else
{
GetConfiguration();
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
magnifier.Show();
}
}
而且由于我做错了,它正在做其他部分:
GetConfiguration();
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
magnifier.Show();
放大镜.Show(); 显示新的 Form 。
现在我希望如果我再次执行 Ctrl + M 它将关闭 Form magnifier.Show();
因此,在其他部分的 gkh_KeyDown 事件的表单中,我做了:
magnifierform.Close();
这次只为放大镜添加了一个新变量并尝试关闭它。所以在放大镜中我做了:
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;
}
public MagnifierForm()
{
}
添加了另一个实例,它什么都不做,因为我只想关闭它。但它从未关闭。
magnifierform 是 MagnifierForm 表单的一个变量,我想直接关闭它,而不是像以前使用其他表单 MagnifierMainForm 一样。
我只想关闭它,但它从未关闭。我在行上使用了一个断点:
magnifierform.Close();
在第二个 Ctrl + M 上,它到达了那里,但没有关闭 MagnifierForm 。就是什么都不做。
编辑
现在在我添加的 MagnifierForm 中尝试了其他东西:
public MagnifierForm()
{
this.Close();
}
在另一边的 gkh_KeyDown 事件的 Form1 中,我将其更改为:
else
{
magnifierform = new MagnifierForm();
}
所以我在第二个 Ctrl + M 上执行的实例再次断点停在那里但是当我继续时它并没有关闭 Form 。