2010-03-30 25 views

回答

1
  • 您是否嘗試捕獲VisibleChanged事件?

this.Shown += new EventHandler(Form1_Shown);

void Form1_Shown(object sender, EventArgs e) 
{ 
    this.Hide(); 
} 
  • 您也可以嘗試通過重寫WndProc攔截消息。
 
    protected override void WndProc(ref Message m) 
    { 
     const int SW_SHOW = 5; 
     if (m.Msg == SW_SHOW) 
     { 
      //DoSomething(); 
     } 

     base.WndProc(ref m); 
    } 
  • 典型的消息泵看起來像以下:
 
MSG msg; 
while(GetMessage(&msg, hwnd, 0, 0)) 
{ 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    // DoSomething. 
}
1

替換Application類中的消息循環並不實際。那麼還有更多的模板Windows消息循環正在進行。無論如何,它並不是真正的問題,Application類通過調用ShowWindow()強制窗體變得可見。這是必要的,因爲表單被懶惰地初始化,沒有ShowWindow()調用它永遠不會創建本地Window句柄。

這個問題很容易通過重寫SetVisibleCore()在常規的.NET framework版本來解決:

protected override void SetVisibleCore(bool value) { 
    if (!this.IsHandleCreated) { 
    this.CreateHandle(); 
    value = false; // Prevent becoming visible the first time 
    } 
    base.SetVisibleCore(value); 
} 

但我不認爲在CF.這可要找到解決方案,您需要準確解釋爲什麼要阻止顯示UI。沒有任何創建的窗口句柄,一個應用程序通常是作爲一個門檻死亡。這可能就像延遲Application.Run()調用一樣簡單。

+0

這不是小事,但我會不同意,它是不實際的。自衛隊這樣做,並做得很好。有很多情況下,它是有用的 - 一樣,如果你真的想支持IMessageFilters,或者類似這樣的用戶,實際控制啓動形式的知名度。 – ctacke 2010-03-30 15:43:11

2

我發現這是一個比你預計的Windows窗體應用程序更大的問題。對於我的解決方案,我從三個不同的網站獲得了建議,並發現此解決方案運行良好。爲了我的需要,我有一個Windows服務在系統托盤中有一個UI控制器組件。只需使用托盤圖標和圖標的上下文菜單即可啓動UI。 UI在激活時作爲模態對話框運行,允許用戶在後臺線程上啓動長時間運行的進程,一旦啓動UI需要恢復用戶的響應。

在構造函數中的Windows窗體類我設置了ControlBox和禁用最小化和最大化按鈕:

public SystemTrayApp() 
{ 
    InitializeComponent(); 
    this.Visible = false; 
    // Hide Maximize and Minimize buttons on form 
    this.StartPosition = FormStartPosition.CenterScreen; 
    this.MaximizeBox = false; 
    this.MinimizeBox = false; 
} 

ControlBox需要被啓用,以允許用戶四處移動對話框在屏幕上但不能改變它的大小,來禁止形式的右上角形式關閉按鈕,紅色的X,我用:

// Force form close button to be disabled 
private const int CP_NOCLOSE_BUTTON = 0x200; 
protected override CreateParams CreateParams 
{ 
    get 
    { 
     CreateParams cp = base.CreateParams; 
     cp.ClassStyle = cp.ClassStyle | CP_NOCLOSE_BUTTON; 
     return cp; 
    } 
} 

在靜態Main方法在應用程序運行主程序啓動時,我改變了def行兇Application.Run到:

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     SystemTrayApp systemTrayApp = new SystemTrayApp(); 
     systemTrayApp.Text = "File Copy Service Controller"; 

     // Show the instance of the form modally. 
     systemTrayApp.ShowInTaskbar = true; 
     systemTrayApp.ShowDialog(); 
     Application.Run(); 
    } 
} 

現在顯示了Windows窗體設計頁面,右鍵單擊表格,選擇屬性,然後選擇事件按鈕顯示爲形式的所有事件。向下滾動到顯示的事件並雙擊,新的事件處理程序將被添加到您的源代碼。導航到方法,並添加this.Hide()到處理程序:

// Runs the first time the application starts 
private void SystemTrayApp_Shown(object sender, EventArgs e) 
{ 
    this.Hide(); 
} 

最後打開Windows窗體項目屬性頁面並選擇應用程序選項卡。在啓動對象,選擇程序類作爲啓動對象。

您將需要一個按鈕或菜單控件添加到您的對話框,可以關閉窗體而不終止應用程序,設置按鈕或菜單控制隱藏應用程序,把它留在系統托盤中運行。

相關問題