繁体   English   中英

启动隐藏在Dot Net Compact Framework中的应用程序

[英]Start application hidden in Dot Net Compact Framework

我试图在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窗口。 如何使其加载隐藏?

Thanx提前很多:)

我相信正确的答案是开始自己的消息泵。 我从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的回调被执行,但没有窗口显示,并且没有任何内容添加到任务栏。

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

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

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

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

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

如何使启动表单最初不可见或隐藏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM