winform最小化至托盤、右下角彈框提示
阿新 • • 發佈:2018-12-12
1、視窗【×】關閉按鈕變為最小化,並在托盤提示資訊
一般有些管理系統,為了防止客戶隨意關閉程式或者基於其他原因,一般會把 視窗【×】關閉按鈕變為最小化,如大家熟悉的飛信、MSN等等,但是有些不是很熟悉的客戶,最小化到托盤的時候,卻不知道程式到了那裡去了,因此,最小化的時候,伴隨一個氣泡提示資訊,顯得有一定的必要,如下截圖所示。
首先在主窗體的設計介面中新增一個NotifyIcon控制元件,然後實現相關的程式碼即可。
下面列出一些關鍵的程式碼出來,大家看了應該就知道如何實現了
{
if ( this .WindowState == FormWindowState.Minimized)
{
this .Show();
this .BringToFront();
this .Focus();
}
else
{
this .WindowState = FormWindowState.Minimized;
this .Hide();
}
}
private void notifyMenu_Exit_Click( object sender, EventArgs e)
{
try
{
this .ShowInTaskbar = false ;
Portal.gc.Quit();
}
catch
{
// Nothing to do.
}
}
private void notifyIcon1_MouseDoubleClick( object sender, MouseEventArgs e)
{
notifyMenu_Show_Click(sender, e);
}
private void MainForm_MaximizedBoundsChanged( object sender, EventArgs e)
{
this .Hide();
}
/// <summary>
/// 縮小到托盤中,不退出
/// </summary>
private void MainForm_FormClosing( object sender, FormClosingEventArgs e)
{
// 如果我們操作【×】按鈕,那麼不關閉程式而是縮小化到托盤,並提示使用者.
if ( this .WindowState != FormWindowState.Minimized)
{
e.Cancel = true ; // 不關閉程式
// 最小化到托盤的時候顯示圖示提示資訊,提示使用者並未關閉程式
this .WindowState = FormWindowState.Minimized;
notifyIcon1.ShowBalloonTip( 3000 , " 程式最小化提示 " ,
" 圖示已經縮小到托盤,開啟視窗請雙擊圖示即可。 " ,
ToolTipIcon.Info);
}
}
private void MainForm_Move( object sender, EventArgs e)
{
if ( this == null )
{
return ;
}
// 最小化到托盤的時候顯示圖示提示資訊
if ( this .WindowState == FormWindowState.Minimized)
{
this .Hide();
notifyIcon1.ShowBalloonTip( 3000 , " 程式最小化提示 " ,
" 圖示已經縮小到托盤,開啟視窗請雙擊圖示即可。 " ,
ToolTipIcon.Info);
}
}
這個已經封裝好程式碼了,只需要在Main函式裡面呼叫一下函式即可,允許多個例項會出現下面的對話方塊提示資訊,提示不允許多例項執行,如下所示:
程式碼如下所示。
/// <summary>/// 應用程式的主入口點。
/// </summary>
[STAThread]
private static void Main()
{
GlobalMutex();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
// ******啟動程式碼**********
}
private static Mutex mutex = null ;
private static void GlobalMutex()
{
// 是否第一次建立mutex
bool newMutexCreated = false ;
string mutexName = " Global\\ " + " WareHouseMis " ;//系統名稱,Global為全域性,表示即使通過通過虛擬桌面連線過來,也只是允許執行一次
try
{
mutex = new Mutex( false , mutexName, out newMutexCreated);
}
catch (Exception ex)
{
Console.Write(ex.Message);
System.Threading.Thread.Sleep( 1000 );
Environment.Exit( 1 );
}
// 第一次建立mutex
if (newMutexCreated)
{
Console.WriteLine( " 程式已啟動 " );
// todo:此處為要執行的任務
}
else
{
MessageUtil.ShowTips( " 另一個視窗已在執行,不能重複執行。 " );
System.Threading.Thread.Sleep( 1000 );
Environment.Exit( 1 ); // 退出程式
}
}
3、使用NotifyWindow給使用者提示資訊
可以通過NotifyWindow類(最後附件中有),做一些資訊的提示,方便使用者瞭解一些重要資訊的提示,介面較為友好,如下所示:
提示資訊的程式碼使用如下:
/// <summary>/// 彈出提示訊息視窗
/// </summary>
public void Notify( string caption, string content)
{
Notify(caption, content, 400 , 200 , 5000 );
}
/// <summary>
/// 彈出提示訊息視窗
/// </summary>
public void Notify( string caption, string content, int width, int height, int waitTime)
{
NotifyWindow notifyWindow = new NotifyWindow(caption, content);
notifyWindow.TitleClicked += new System.EventHandler(notifyWindowClick);
notifyWindow.TextClicked += new EventHandler(notifyWindowClick);
notifyWindow.SetDimensions(width, height);
notifyWindow.WaitTime = waitTime;
notifyWindow.Notify();
}
private void notifyWindowClick( object sender, EventArgs e)
{
// SystemMessageInfo info = BLLFactory<SystemMessage>.Instance.FindLast();
// if (info != null)
// {
// // FrmEditMessage dlg = new FrmEditMessage();
// // dlg.ID = info.ID;
// // dlg.ShowDialog();
// }
}