|
-
Feb 22nd, 2004, 04:12 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Center backgroundimage in MDI
Hi all!
By just adding an image to bgimg it is tiled over the whole form.
Is there a fast way to center the backgroundimage of a MDI-form ?
I fixed it by using a graphics object filled with my backgroundcolor and drew the image to the center and redrew it to bgimg of the MDI. But this is kinda slow.
I can't imagine that MS does not support centering by default (I use .NET/1.0).
Thanks in advance!
Last edited by dmr; Feb 23rd, 2004 at 08:51 AM.
Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?
Originally posted by aknisely
Sorry, but I feel uncomfortable on CC with clothes on
__________________
The truth ... is out there!
-
Feb 22nd, 2004, 08:42 AM
#2
if you make a seperate image to hold the centred image , this seems quite ok ( hope it helps , only quickly knocked it up )
VB Code:
public Bitmap imgCustom = null;
public Image im = null;
public MdiClient Client = null;
public System.Drawing.GraphicsUnit gUnit = System.Drawing.GraphicsUnit.Pixel;
private void Form1_Load(object sender, System.EventArgs e)
{
foreach(MdiClient mdi in Controls)
{
Client = (MdiClient)mdi;
im = Image.FromFile(@"C:\vb_logo.gif");
imgCustom = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
Graphics grphcs = Graphics.FromImage(imgCustom);
RectangleF r = imgCustom.GetBounds(ref gUnit);
grphcs.FillRectangle(new SolidBrush(Client.BackColor),r);
grphcs.DrawImage(im ,new Point(((0 + Client.Width) - (im.Width)) / 2, Convert.ToInt32(((0 + Client.Height) - (im.Height / 2)) / 2.5)));
grphcs.Save();
this.BackgroundImage = imgCustom;
}
}
private void Form1_Resize(object sender, System.EventArgs e)
{
/// to ensure we center the image when the form gets resized.
if(this.WindowState!=System.Windows.Forms.FormWindowState.Minimized)
{
Graphics grphcs = Graphics.FromImage(imgCustom);
RectangleF r = imgCustom.GetBounds(ref gUnit);
grphcs.FillRectangle(new SolidBrush(Client.BackColor),r);
grphcs.DrawImage(im ,new Point(((0 + Client.Width) - (im.Width)) / 2, Convert.ToInt32(((0 + Client.Height) - (im.Height / 2)) / 2.5)));
grphcs.Save();
this.BackgroundImage = imgCustom;
this.Refresh();
}
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Feb 23rd, 2004, 08:51 AM
#3
Thread Starter
Addicted Member
Hi, thanks alot for you soon reply.
I combined yours and mine code and now it seems to work fine.
Code:
private void Form1_Load(object sender, System.EventArgs e)
{
foreach(MdiClient mdi in Controls)
{
The marked line may fail, when you place controls that differ the type of MdiClient (-> almost everything...) on the MDI.
Finally, for everyone who might be interested in, I'm using the following code:
MDI-Form mdi
Image imgCustom = null
PictureBox picBackground (on MDI; visible = false; containing the image to center)
Code:
private void mdi_Resize(object sender, System.EventArgs e)
{
// to ensure we center the image when the form gets resized
if (this.WindowState != System.Windows.Forms.FormWindowState.Minimized)
{
if (imgCustom == null)
imgCustom = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
RectangleF r = imgCustom.GetBounds(ref gUnit);
Graphics g = Graphics.FromImage(imgCustom);
g.FillRectangle(new SolidBrush(this.BackColor), r);
g.DrawImage(picBackground.Image, (this.ClientRectangle.Width - picBackground.Image.Width) / 2, (this.ClientRectangle.Height - picBackground.Image.Height) / 2);
g.Save();
this.BackgroundImage = imgCustom;
this.Refresh();
}
}
Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?
Originally posted by aknisely
Sorry, but I feel uncomfortable on CC with clothes on
__________________
The truth ... is out there!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|