Weird behviour of creating graphics from a form's handle
EDIT: sorry, my bad, i forgot a line. and those are clean conditions (completely new project)
Form frm = new Form();
Graphics graphics = Graphics.FromHwnd(frm.Handle);
frm.FormBorderStyle = FormBorderStyle.None;
frm.WindowState = FormWindowState.Maximized;
frm.TransparencyKey = Color.Magenta;
frm.ShowDialog();
with, and without the second line, i get two quite different results..
some why, when i create the graphics object from the form handle, it does not maximize the form...
am i the only one it happen to? do you have any id开发者_如何学JAVAea why does it happen (to me, or, at all)?
thanks a lot.
Just a side note. The form handle is not even created yet:
Form frm = new Form();
if (frm.IsHandleCreated == true)
{
// Doesn't get here
}
Graphics graphics = Graphics.FromHwnd(frm.Handle);
Maybe you are looking for something like:
Form frm = new Form();
frm.HandleCreated += (s, ev) =>
{
if (frm.IsHandleCreated == true)
{
Graphics graphics = Graphics.FromHwnd(frm.Handle);
// FIXME not sure what you want to do, but drawing into this still won't work.
}
};
frm.Paint += (s, ev) =>
{
ev.Graphics.DrawLine(Pens.Black, 10, 10, 100, 100);
};
frm.FormBorderStyle = FormBorderStyle.None;
frm.WindowState = FormWindowState.Maximized;
frm.TransparencyKey = Color.Magenta;
frm.ShowDialog();
I have found some erratic behaviors in creating a graphics object. The graphics object is intended for a short-lived use. Normally the following 2 methods are the most reliable:
private void Form1_Paint(object sender , PaintEventArgs e)
{
Graphics g = e.Graphics;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.TransparencyKey = Color.Magenta;
}
OR:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.TransparencyKey = Color.Magenta;
base.OnPaint(e);
}
Also, because you are not manually creating a graphics object, you do not perform g.Dispose();
Try disposing graphics object before frm.ShowDialog();
.
I have experienced similar behavior two times before. I don't have specific answer for your question, but you have to make sure the form is fully created (displayed/loaded/handle created) first before getting the Graphics object. In your case, I would get the Graphics object after the ShowDialog call. From your code snippet this would be hard to do.) Notice dboarman-FissureStudios "reliable" methods, are reliable because the form/control is fully created.
Also, why are you using Graphics.FromHandle? I would use the Control.CreateGraphics method.
Moreover, the form's handle is not created yet. Therefore, the call Graphics.FromHandle is incorrect.
I have had problems before with forms when creating a Graphics object like this and not disposing it. You must call the Dispose method once you are done. You cannot store the Graphics object. You must create and dispose of it new each time you need it. Check out the documentation for the Graphics class.
精彩评论