Since Compact Framework calls Show() on the form passed to Application.Run() ,我决定创建自己的消息泵。
我不知道我该怎么做。
任何帮助(比如一个简单的例子)将不胜感激。
请您参考如下方法:
我发现对于 Windows 窗体应用程序,这是一个比您预期的更大的问题。对于我的解决方案,我从三个不同的网站上提取了建议,发现这个解决方案非常有效。根据我的需要,我有一个 Windows 服务,它在系统托盘中有一个 UI Controller 组件。用户界面需要以托盘图标和图标的上下文菜单最小化启动。 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 方法在应用程序启动时运行的主程序中,我将默认的 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 窗体的设计页面,右键单击窗体并选择属性,然后选择事件按钮以显示窗体的所有事件。向下滚动到 Shown 事件并双击,一个新的事件处理程序将添加到您的源代码中。导航到该方法并将 this.Hide() 添加到处理程序:
// Runs the first time the application starts
private void SystemTrayApp_Shown(object sender, EventArgs e)
{
this.Hide();
}
最后打开 Windows 窗体项目的属性页并选择“应用程序”选项卡。在启动对象下,选择程序类作为启动对象。
您需要向对话框添加按钮或菜单控件,以允许在不终止应用程序的情况下关闭表单,设置按钮或菜单控件以隐藏应用程序,使其在系统托盘中运行。
