我试图在 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 窗口始终显示。如何使其隐藏加载?
提前致谢:)
请您参考如下方法:
我认为正确的答案是启动您自己的消息泵。我从 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 的回调,但没有窗口显示,也没有向任务栏添加任何内容。
