我正在開發一個 WinForms 應用程式,我試圖在關閉時最小化系統嘗試的表單,當從系統托盤打開時,我想直接轉到主應用程式而無需再次登錄。重點是減少進入主應用程式的登錄嘗試,應用程式應該在后臺運行,直到帳戶持有人退出應用程式并看到登錄螢屏。
這就是我要做的
程式.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Splashscreen());
Application.Run(new SignIn());
}
這就是我從登錄表單導航到主應用程式的方式
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
waitForm.Close();
this.Hide();
//Nothing to do with this system tray problem, just checking for another valid condition
if(condition == true)
{
MainApp mainapp = new MainApp();
mainapp.ShowDialog();
}
this.Close();
}
登錄后進入的主應用程式
private void MainApp_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
e.Cancel = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
但是,當我這樣做時,系統托盤圖示消失并且無法轉到指定頁面。有什么想法或經驗來實作這一點嗎?
在此先感謝各位!
uj5u.com熱心網友回復:
問題是,隱藏表單會結束ShowDialog()模態回圈。因此,當表單隱藏并退出main 方法中的回圈時,backgroundWorker1_RunWorkerCompleted您的SignIn表單將關閉。在您現有的代碼中查看我的評論:MainAppApplication.Run()
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
waitForm.Close();
this.Hide();
//Nothing to do with this system tray problem, just checking for another valid condition
if(condition == true)
{
MainApp mainapp = new MainApp();
// this method will exit when mainapp will be hidden:
mainapp.ShowDialog();
}
// this will exit your Application.Run(new SignIn()) in the static Main methods:
this.Close();
}
正如您所說,SignIn成功登錄后您不需要該實體,您可以按如下方式更改您的應用程式。
在SignIn表單中更改以下方法,如下所示:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
waitForm.Close();
//Nothing to do with this system tray problem, just checking for another valid condition
DialogResult = condition ? DialogResult.OK : DialogResult.Cancel;
}
這樣你就可以在你的方法中使用DialogResultof :SignInMainProgram.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Splashscreen());
using (var signIn = new SignIn())
{
switch (signIn.ShowDialog())
{
case DialogResult.OK:
break;
default:
return;
}
}
Application.Run(new MainApp());
}
其余代碼乍一看還不錯。
uj5u.com熱心網友回復:
我有一些我很久以前(十多年前)挖出的代碼。Windows 10 打破了它,所以最近有一些變化。您想要做的一件事(如果您取消關閉)是處理WmQueryEndSession和WmEndSession訊息 - 否則您的應用程式將阻止系統關閉。
private const int WmQueryEndSession=0x11;
private const int WmEndSession=0x16;
這是一些作業代碼的摘錄。它只是從檔案中復制/粘貼。我現在沒有可以測驗此代碼的 VS 系統。
這是結束會話代碼:
//needed to get this thing to close cleanly. OnSessionEnding and OnSessionEnded didn't work
protected override void WndProc(ref Message m){
switch(m.Msg){
case WmQueryEndSession:
m.Result=(IntPtr) 1; //basically "e.Cancel = false;"
goto case WmEndSession;
case WmEndSession:
forceExit=true;
Close();
return;
default:
base.WndProc(ref m);
break;
}
}
請注意,有關意見OnSessionEnding和OnSessionEnded從框架1.x版的日子日期; 今天可能不是真的
該forceExit標志最初為假。當用戶在按住控制鍵Quit的情況下單擊關閉按鈕或從背景關系選單中選擇時,它會被設定。
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if(forceExit)
return; //close and quit
//otherwise
forceExit=false;
if(!ModifierKeys.HasFlag(Keys.Control)) {
WindowState=FormWindowState.Minimized;
e.Cancel=true;
}
}
Restore我的背景關系選單中有一個選項(這就是表單的恢復方式)。這個應用程式在通知區域(又名系統托盤)中有最大的用途。在恢復狀態下,它主要用于更改應用程式設定等。 從評論中,我需要在Win10出來后在這里擺弄東西:
private void RestoreCmd_Click(object sender, EventArgs e) {
Show();
//restoring from initially minimized on Windows 10 seems to completely mess up all the control positions and sizes
//so store everything here and restore it below
Dictionary<Control, (Point location, Size size)> controlPositions = null;
if (textBox1.Size.Width != 0)
{
controlPositions = new Dictionary<Control, (Point pos, Size size)>();
foreach (var ctrl in this.Controls.Cast<Control>())
{
controlPositions.Add(ctrl, (location: ctrl.Location, size:ctrl.Size));
}
}
WindowState=FormWindowState.Normal;
//restore all the control positions and sizes
if (controlPositions != null)
{
foreach (var position in controlPositions)
{
position.Key.Size = position.Value.size;
position.Key.Location = position.Value.location;
}
}
}
背景關系選單中有Hide和Quit條目:
private void HideCmd_Click(object sender, EventArgs e) {
WindowState=FormWindowState.Minimized;
}
private void QuitCmd_Click(object sender, EventArgs e) {
forceExit=true;
Close();
}
這個應用程式有很多功能。我想我已經提取了所有的顯示/隱藏管理并將其放在這個答案中。如果我遺漏了什么,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357991.html
上一篇:是否可以動態調整控制元件的大小?
