Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i've assigned a background image to a MDI form.

When i resize the form, the image retain the same size, but I want it to resize with the form.

 

i've tried in the Resize event this code:

 

  Graphic g = this.CreateGraphic();
  g.DrawImage(img, this.ClientRectangle);

 

but it doesn't works...

 

How can I do?

  • *Gurus*
Posted
	Bitmap myBitmap = null;
	MdiClient client = null;

	private void Form1_Load(object sender, System.EventArgs e)
	{
		myBitmap = new Bitmap(@"c:\windows\coffee Bean.bmp");

		SetStyle(ControlStyles.ResizeRedraw, true);

		foreach(Control c in Controls)
		{
			if (c is MdiClient)
			{
				client = (MdiClient)c;
				break;
			}
		}
		if (client != null)
			client.Paint += new PaintEventHandler(PaintMdiClient);
	}

	private void PaintMdiClient(object sender, PaintEventArgs e)
	{
		e.Graphics.DrawImage(myBitmap, client.Bounds);
	}

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

GREAT!

 

But can you explain me two things?

The MDIClient control is part of the Form or is the Form itself?

What the "@" does? Instead of the Bitmap, can I use an Image loaded from a resource file?

 

TY :-)

  • *Experts*
Posted (edited)

The MDIClient is the part of the form which can hold children; an MDI window is made of two parts: the main window, same as all the other windows, and an MDI client window, which is the part with the (usually) gray background that can hold other forms.

 

In C#, preceding a string with @ tells it not to process escape codes (they are in this format: \x where x can be a number of different things). Without it, divil would have had to change the path to "C:\\windows\\coffee bean.bmp", because \\ is the escape code for \. However, telling it not to look at escape codes allows him to use \ without error.

 

To use an image from a resource file:

private Bitmap GetResourceImage(String name) {
       return new Bitmap(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(name));
}

Now as long as you've marked the image you want in your resource file as "Embedded Resource" (for Build Action), you can replace the line

myBitmap = new Bitmap(@"c:\windows\coffee Bean.bmp");

with this:

myBitmap = GetResourceImage("MyNamespaceName.ImageIdentifier");

Edited by Volte
Posted (edited)

I'm actually trying to do something similar, except I'm drawing a gradient across the MDI client area. The relevant code:

 

Dim cli as MDIClient

Private Sub Form1_Load(.....)
   Dim ctrl as Control
   For Each ctrl in Me.Controls
       If TypeOf (ctrl) Is MDIClient Then
           cli = ctrl
           AddHandler cli.Paint, AddressOf cli_Paint
       End If
   Next
End Sub

Private Sub cli_Paint(ByVal sender as Object, ByVal e as PaintEventArgs)

   Dim G as Graphics = e.Graphics
   Dim br As New System.Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), _
   New Point(Me.Width, Me.Height), Color.Azure, Color.DarkBlue)
   G.FillRectangle(br, e.ClipRectangle)
   G.Dispose()

End Sub

 

My problem is with getting rid of the flicker when the form is resized. I've tried putting these lines in the form's constructor:

 

SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)

 

If I comment out the last line, it doesn't flicker, but it doesn't redraw the gradient for the new client size, either. I thought about adding another handler for cli.Resize, but the Event Argument that gets passed doesn't contain a graphics object.

 

Does anyone know of a way to get rid of resizing flicker in this situation?

Edited by ballisticnylon

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...