C#アプリケーションは、タスクバーアイコン(NotifyIcon)と最初は非表示のオーバーヘッドウィンドウで構成されています。 NotifyIcon(左クリック、一回クリック)をクリックすると、ウィンドウの表示を切り替えることができます。また、フォーカスを失うとウィンドウが非表示になります。NotifyIcon(タスクバーアイコン)をクリックするとC#トグルウィンドウが表示されます
これは、私がこれまで持っているものサブクラス化System.Windows.Forms.Form
です:
初期化:
this.ControlBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
// Instance variables: bool allowVisible;
// System.Windows.Forms.NotifyIcon notifyIcon;
this.allowVisible = false;
this.notifyIcon = new NotifyIcon();
this.notifyIcon.MouseUp += new MouseEventHandler(NotifyIconClicked);
this.Deactivate += new EventHandler(HideOnEvent);
インスタンスメソッド:それが必要のようなアイコンをクリックする
private void NotifyIconClicked(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this.Visible)
this.Hide();
else
this.Show();
}
}
new public void Show()
{
this.allowVisible = true;
this.Visible = true;
this.Activate();
}
new public void Hide()
{
this.allowVisible = false;
this.Visible = false;
}
private void HideOnEvent(object sender, EventArgs e)
{
this.Hide();
}
protected override void SetVisibleCore(bool visible)
{
base.SetVisibleCore(this.allowVisible ? visible : this.allowVisible);
}
は窓を明らかに。マウスを押している間はもう一度クリックすると表示が隠され、表示されます。
私の推測では、マウスのダウンイベントがウィンドウからフォーカスを奪って消えるためです。次に、マウスアップイベントがトリガされ、ウィンドウが非表示になっていることが表示されます。
私の次のアイデアは、マウスダウンイベントで、ウィンドウの可視性を読み取ることだったので、私は彼らが呼び出されるよう3つのイベントをテストし、UNIX時間をログに記録:
notifyIcon.MouseDown
notifyIcon.MouseUp
this.LostFocus
結果はかなり奇妙です:のウィンドウをしましょう表示されます。これは、アイコンをクリックすると発生します。フォーカスが失われたときはすぐに呼び出されます。マウスを上げるイベントの直前にマウスを放すとすぐに、マウスダウンはとなります。
1312372231 focus lost
1312372235 mouse down
1312372235 mouse up
なぜマウスダウンイベントが遅れているのですか?
どのようにしてウィンドウを切り替えることができますか?
ありがとうございます! – David