代码之家  ›  专栏  ›  技术社区  ›  Marc

如何正确处理居中的MDI窗体背景图像

  •  0
  • Marc  · 技术社区  · 16 年前

    我有一个MDI窗体,背景图像居中。
    每次用户更改表单的大小或状态时,图像都不会更新。它保持在原来的位置(不再居中),当窗体太小时甚至会丢失。

    如何正确处理这种情况?
    我真的需要在所有与窗体大小和状态相关的事件处理程序中调用“this.refresh()”吗?

    应用程序在.NET 3.5sp1 c中通过windows.forms实现。

    5 回复  |  直到 10 年前
        1
  •  2
  •   Clyde    16 年前

    不幸的是,似乎没有一个超快速的方法来做到这一点,但以下是我的解决方案,至少似乎不依赖巧合。

    在MDI构造函数中,处理大小调整:

    this.ResizeEnd += delegate { this.Refresh(); };
    

    然后此重写处理最大化/还原事件

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Win32.WM_SYSCOMMAND)
            {
                int test = m.WParam.ToInt32() & 0xFFF0;
                switch (test)
                {
                    case Win32.SC_MAXIMIZE:
                    case Win32.SC_RESTORE:
                        this.Invalidate();  // used to keep background image centered
                        break;
                }
            }
            base.WndProc(ref m);
        }
    

    常量值定义为:

        public const int WM_SYSCOMMAND =                    0x0112;
        //wparam for WM_SYSCOMMAND should be one of these after masking with 0xFFF0:
        public const int SC_RESTORE =                       0xF120;
        public const int SC_MINIMIZE =                      0xF020;
        public const int SC_MAXIMIZE =                      0xF030;
    
        2
  •  1
  •   Ivan Ferić    13 年前

    你可以做所有这些,或者你可以把 me.refresh 在MDI中 resize 事件。

        3
  •  0
  •   MRG    16 年前

    在MDI窗体的Resize事件中调用positionContainerstoparentmiddle方法。 我没有测试过它,但它应该可以工作。您可能需要在“调整大小”事件中设置条件,以便在每次调整大小时停止图像位置更改。

       private void YourMDI_Resize(object sender, EventArgs e)
        {
            PositionContainersToParentMiddle();
        }
    
        private void PositionContainersToParentMiddle()
        {
            int iInitX = (ParentOfImage.Size.Width - YourImage.Size.Width) / 2;
            int iInitY = ( ParentOfImage.Location.Y + YourImage.Size.Height ) / 2;
            YourImage.Location = new Point( iInitX, iInitY ) ;
    
        }
    
        4
  •  0
  •   Lawal    13 年前
    Private Sub YourMDIFormName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Resize
        Me.BackgroundImage = My.Resources.YourBackgroundImageName
        Me.Refresh()
     End Sub
    
        5
  •  -1
  •   Khalid    10 年前
        private void Change_BackgroundImage(string _path)
      {
        string imagepath = _path;
        System.IO.FileStream fs;
     // MDI Form image background layout change her`enter code here`e     
     //(Remember control imagebakground layout take default form background layount )
              this.BackgroundImageLayout = ImageLayout.Center;
                // Checking File exists if yes go --->
                if (System.IO.File.Exists(imagepath))
                {
                  // Read Image file
                  fs = System.IO.File.OpenRead(imagepath);
                    fs.Position = 0;
                    // Change MDI From back ground picture
                    foreach (Control ctl in this.Controls)
                    {
                        if (ctl is MdiClient)
                        {
                          //ctl.BackColor = Color.AntiqueWhite;
                           ctl.BackColor = Color.FromArgb(31, 26, 23);
                           ctl.BackgroundImage = System.Drawing.Image.FromStream(fs);
                          break;
                           }
                      }
                 }
              }
    
    推荐文章