tags:

views:

73

answers:

3

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 idea why does it happen (to me, or, at all)?

thanks a lot.

A: 

Try disposing graphics object before frm.ShowDialog();.

DxCK
A: 

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();

dboarman
A: 

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.

AMissico