Results 1 to 3 of 3

Thread: [RESOLVED] Center backgroundimage in MDI

  1. #1

    Thread Starter
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255

    [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!

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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:
    1. public Bitmap imgCustom = null;
    2.         public Image im = null;
    3.         public MdiClient Client = null;
    4.         public System.Drawing.GraphicsUnit gUnit = System.Drawing.GraphicsUnit.Pixel;
    5.  
    6.         private void Form1_Load(object sender, System.EventArgs e)
    7.         {
    8.             foreach(MdiClient mdi in Controls)
    9.             {
    10.                 Client = (MdiClient)mdi;
    11.                 im = Image.FromFile(@"C:\vb_logo.gif");
    12.                 imgCustom = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
    13.                 Graphics grphcs = Graphics.FromImage(imgCustom);
    14.                 RectangleF r = imgCustom.GetBounds(ref gUnit);
    15.                 grphcs.FillRectangle(new SolidBrush(Client.BackColor),r);
    16.                 grphcs.DrawImage(im ,new Point(((0 + Client.Width) - (im.Width)) / 2, Convert.ToInt32(((0 + Client.Height) - (im.Height / 2)) / 2.5)));
    17.                 grphcs.Save();
    18.                 this.BackgroundImage = imgCustom;
    19.             }
    20.         }
    21.  
    22.         private void Form1_Resize(object sender, System.EventArgs e)
    23.         {
    24.             /// to ensure we center the image when the form gets resized.
    25.             if(this.WindowState!=System.Windows.Forms.FormWindowState.Minimized)
    26.             {
    27.                 Graphics grphcs = Graphics.FromImage(imgCustom);
    28.                 RectangleF r = imgCustom.GetBounds(ref gUnit);
    29.                 grphcs.FillRectangle(new SolidBrush(Client.BackColor),r);
    30.                 grphcs.DrawImage(im ,new Point(((0 + Client.Width) - (im.Width)) / 2, Convert.ToInt32(((0 + Client.Height) - (im.Height / 2)) / 2.5)));
    31.                 grphcs.Save();
    32.                 this.BackgroundImage = imgCustom;
    33.                 this.Refresh();
    34.             }
    35.         }
    ~
    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]

  3. #3

    Thread Starter
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255
    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
  •  



Click Here to Expand Forum to Full Width