当我检查Form.Deactivate中的鼠标按钮状态时,它不表示必须导致停用Form的状态。
就给定的示例项目而言,这意味着,如果通过按下按钮刚刚显示了dropDownForm(并因此获得了焦点),并且我在dropDownForm的边界矩形外部按住了鼠标左键,则正确触发了Deactivate事件,但是在其处理程序体内,鼠标状态错误地显示为MouseButtons.None。
有趣的是,唯一的例外是当我单击MainForm的标题栏时,两次触发了停用,第一次出现是显示正确的鼠标按钮状态。
在Deactivate事件中是否有任何方法可以使鼠标按钮状态得到正确的结果?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormDeactivateTest
{
public class MainForm : Form
{
Form dropDownForm;
Button button1;
public MainForm()
{
button1 = new Button();
SuspendLayout();
button1.Location = new Point(107, 54);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 0;
button1.Text = "Show Form";
button1.UseVisualStyleBackColor = true;
button1.Click += Button1Click;
dropDownForm = new Form();
dropDownForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
dropDownForm.ControlBox = false;
dropDownForm.ShowInTaskbar = false;
dropDownForm.ShowIcon = false;
dropDownForm.MinimizeBox = false;
dropDownForm.MaximizeBox = false;
dropDownForm.HelpButton = false;
dropDownForm.Text = "";
dropDownForm.TopMost = true;
dropDownForm.StartPosition = FormStartPosition.Manual;
dropDownForm.Deactivate += dropDownDeactivate;
AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(284, 261);
Controls.Add(button1);
Name = "MainForm";
Text = "FormDeactivateTest";
ResumeLayout(false);
}
void Button1Click(object sender, EventArgs e)
{
if (dropDownForm.Visible) dropDownForm.Hide();
else
{
dropDownForm.Location = button1.PointToScreen(new Point(0, button1.Height+1));
dropDownForm.Show();
}
}
void dropDownDeactivate(object sender, EventArgs e)
{
if (Control.MouseButtons == MouseButtons.None)
{
MessageBox.Show("Mouse button press not recognized");
}
else
{
MessageBox.Show("Mouse button pressed");
}
}
}
}
更新如果您要使用的是一个包含任意内容的下拉面板,则可以使用ToolStripDropDownButton
和ToolStripControlHost
。优势包括:
如果您希望它不出现在顶部,您甚至可以取消停放它或将其放在另一个面板中。
public class MainForm : Form
{
private ToolStrip toolStrip1;
private ToolStripDropDownButton toolStripDropDownButton1;
public MainForm()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripDropDownButton1 = new System.Windows.Forms.ToolStripDropDownButton();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripDropDownButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(284, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripDropDownButton1
//
this.toolStripDropDownButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripDropDownButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripDropDownButton1.Image")));
this.toolStripDropDownButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";
this.toolStripDropDownButton1.Size = new System.Drawing.Size(29, 22);
this.toolStripDropDownButton1.Text = "toolStripDropDownButton1";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.toolStrip1);
this.Name = "MainForm";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
var tv = new TreeView();
tv.Nodes.Add("test", "testText");
tv.Nodes.Add("test2", "testTexttt");
tv.Nodes.Add("test2", "testTexttttttt");
var hosted = new ToolStripControlHost(tv);
toolStripDropDownButton1.DropDownItems.Add(hosted);
}
}
上一个答案
用户也可以通过Alt + Tab触发Deactivate
,远离您的表单(这将导致没有鼠标单击),因此不确定您为什么要这样做。
但是您可以在OnMouseUp上使用一个标志和一个侦听器来实现。如果您单击父母的边框,则不会触发。
private bool recentlyDeactivated = false;
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (recentlyDeactivated)
{
if (e.Button == MouseButtons.None)
{
MessageBox.Show("Mouse button press not recognized");
}
else
{
MessageBox.Show("Mouse button pressed");
}
}
recentlyDeactivated = false;
}
void dropDownDeactivate(object sender, EventArgs e)
{
recentlyDeactivated = true;
}