Initialize window handle for form and child controls without displaying
Is there a way to programmatically force a form and all of its child controls to have window handles without it being visible? It looked like CreateControl would do it, but that only seems to work if the control is visible. Requesting the handle开发者_开发问答 of the form gives the form a handle, but doesn't give handles to all child controls, and I don't really want to iterate through all child controls manually forcing them to have handles.
Currently, I'm resorting to making the form visible for a split second and then hiding it, which seems to be a pretty hacky solution to me. Is there a better way?
I don't understand why you don't like iterating. It seems like a good solution to me. I'd take the opportunity to build a reusable recursive control iterator.
However, if you don't want to do that then you can try a simple variant on your current solution. Before you make the form visible set its position so that it does not appear on any monitor. Then when you hide it again restore the correct position.
If you want create the window handles without actually seeing the form, you can do this:
public Form1()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = false;
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(0, 0);
}
精彩评论