Start application hidden in Dot Net Compact Framework
I am trying to make my application load hidden when windows loads. I have created a shortcut with a parameter and I am trying to hide the form if the parameter equals to "WINDOWS". But the Form is ALWAYS shown regardless I h开发者_Python百科ide the form or set the visibility to false. How do i get about doing this?
[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"));
}
}
and in the constructor of frmMain
public frmMain(string Argument)
{
InitializeComponent();
if (Argument != null && Argument != "")
{
if (Argument == "WINDOWS")
{
this.Visible = false;
//Hide();
}
}
But the frmMain window is ALWAYS shown. How do make it load hidden?
Thanx a lot in advance :)
I believe the proper answer is to start your own message pump. I copied the following code from the BenPas Blog (formerly at http://blog.xeviox.com), which I could only find in Google's cache -- the link to the page was dead. But I've tested the code and it works.
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);
Here is how you can use it to create the messge loop:
[MTAThread]
static void Main()
{
HiddenForm f = new HiddenForm();
MSG msg;
while(GetMessage(out msg, IntPtr.Zero, 0, 0))
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}
With the above timer messages and Windows based callbacks are executed, but no window shows, and there nothing is added to the taskbar.
The definition for the Application.Run(Form)
method is:
"Begins running a standard application message loop on the current thread, and makes the specified form visible."
You could create the form, then sleep, or block, until you want the Form to be visible, and then call Application.Run()
on the Form you created when it's time to show it.
If the application needs to perform tasks even before the Form is displayed, you could place that code outside the Form's logic (or even not use a Form at all).
I wrote about a simple technique here:
How to make the startup form initially invisible or hidden
精彩评论