【问题标题】:Start application hidden in Dot Net Compact Framework启动隐藏在 Dot Net Compact Framework 中的应用程序
【发布时间】:2011-04-19 08:06:18
【问题描述】:

我试图在加载 Windows 时隐藏我的应用程序加载。我创建了一个带有参数的快捷方式,如果参数等于“WINDOWS”,我试图隐藏表单。但无论我隐藏表单或将可见性设置为 false,表单总是显示。我该怎么做?

[MTAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                Debug.WriteLine("Arguments were passed");
                foreach (string item in args)
                {
                    MessageBox.Show(item);
                }


                Application.Run(new frmMain("WINDOWS"));
            }    

        }

在frmMain的构造函数中

public frmMain(string Argument)
        {
            InitializeComponent();

            if (Argument != null && Argument != "")
            {                
                if (Argument == "WINDOWS")
                {
                    this.Visible = false;
                    //Hide();
                }  
           }

但总是显示 frmMain 窗口。如何让它加载隐藏?

提前感谢很多:)

【问题讨论】:

  • 我认为最好不要与 windows 消息打架。在frmMain.Location放在屏幕可见区域之外,设置一个Timer组件来设置frmMain.Visible = false。

标签: c# windows-mobile compact-framework startup hidden


【解决方案1】:

我相信正确的答案是启动您自己的消息泵。我从 BenPas 博客(以前在 http://blog.xeviox.com)复制了以下代码,我只能在 Google 的缓存中找到该代码——该页面的链接已失效。但是我已经测试了代码并且它可以工作。

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public POINT(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public static implicit operator System.Drawing.Point(POINT p)
    {
        return new System.Drawing.Point(p.X, p.Y);
    }
        public static implicit operator POINT(System.Drawing.Point p)
    {
        return new POINT(p.X, p.Y);
    }
}

[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
    public IntPtr hwnd;
    public UInt32 message;
    public IntPtr wParam;
    public IntPtr lParam;
    public UInt32 time;
    public POINT pt;
}

[DllImport("coredll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin,
   uint wMsgFilterMax);

[DllImport("coredll.dll")]
public static extern bool TranslateMessage([In] ref MSG lpMsg);

[DllImport("coredll.dll")]
public static extern IntPtr DispatchMessage([In] ref MSG lpmsg);

您可以使用它来创建消息循环:

    [MTAThread]
    static void Main()
    {
        HiddenForm f = new HiddenForm();

        MSG msg;
        while(GetMessage(out msg, IntPtr.Zero, 0, 0))
        {
            TranslateMessage(ref msg);
            DispatchMessage(ref msg);
        }
    }

使用上述定时器消息和基于 Windows 的回调被执行,但没有窗口显示,并且没有任何内容添加到任务栏。

【讨论】:

    【解决方案2】:

    Application.Run(Form) 方法的定义是:

    “开始在当前线程上运行标准应用程序消息循环,并使指定的表单可见。”

    您可以创建表单,然后休眠或阻塞,直到您希望该表单可见,然后在您创建的表单上调用 Application.Run() 以显示它。

    如果应用程序需要在表单显示之前执行任务,您可以将该代码放在表单的逻辑之外(甚至根本不使用表单)。

    【讨论】:

    • 这个答案应该被标记为答案,因为除非调用 Application.Run(...),否则消息泵不会运行。而在 Compact Framework 中,唯一存在的 Application.Run 需要一个表单对象。
    【解决方案3】:

    我在这里写了一个简单的技术:

    How to make the startup form initially invisible or hidden

    【讨论】:

    • 问题是关于.NET compact framework,这意味着windows CE。
    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2010-11-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多