簡體   English   中英

是否可以改為顯示NotformIcon的ContextMenuStrip而不顯示ContextMenuStrip的窗體?

[英]Is it possible that instead ContextMenuStrip of NotifyIcon, display a Form, which will behave like ContextMenuStrip?

我想用一些更復雜的Form代替NotifyIcon ContextMenuStrip 當用戶單擊SystemTray中的NotifyIcon時,我可以顯示表單,但是當用戶單擊其他位置時,我無法隱藏/關閉諸如ContextMenuStrip的表單。

那可能嗎?

這是示例代碼,我以這種方式顯示該表單:

private void Mouse_Up_Event(object sender, MouseEventArgs e)
{  
    FormMenu f = new FormMenu();
    f.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    f.SetDesktopLocation(Cursor.Position.X - f.Width / 2,
        Cursor.Position.Y - f.Height - 20);
    f.Show();
    f.Focus();
}

FormMenu是具有面板和多個按鈕的復雜表單。

您可以使用ToolStripControlHostContextMenuStrip托管復雜的控件或窗體。

碼:

var c= new MyUserControl();
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(c));

您還可以通過以下方式將表單添加到上下文菜單欄中:

var f = new YourForm() { 
    TopLevel = false, 
    FormBorderStyle = System.Windows.Forms.FormBorderStyle.None, 
    MinimumSize= new Size(200, 200), /*Your preferred size*/
    Visible=true
};
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(f) );

截圖:

在此處輸入圖片說明

在上面的屏幕截圖中,我向沒有其他項目的上下文菜單條添加了一個表單,並將上下文菜單的ShowImageMargin屬性設置為false

您還可以具有其他項目和子菜單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM