如何正确处理居中的MDI表单背景图像

发布于 2024-08-04 11:51:04 字数 208 浏览 4 评论 0原文

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

如何正确处理这种情况?
我真的必须在与表单大小和状态相关的所有事件处理程序中调用“this.Refresh()”吗?

应用程序是在.net 3.5SP1 C# 和 Windows.Forms 中实现的。

I have an MDI form with a centered background image.
Each time the user changes the size or state of the form, the image isn't updated at all. It remains in the old place (not centered any more) and is even lost when the form is made too small.

How can this situation correctly be handled?
Do I really have to call "this.Refresh()" in all event handlers related to form size and state?

Application is realized in .net 3.5SP1 C# with Windows.Forms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦言归人 2024-08-11 11:51:04

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

在 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;

Unfortunately there doesn't seem to be a super-quick way to do this, but the following is my solution and at least doesn't seem to rely on coincidences.

In the mdi constructor, handle resizing:

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

And then this override to handle maximize/restore events

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

Constant values are defined as:

    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;
烟雨扶苏 2024-08-11 11:51:04

您可以执行所有这些操作,也可以将 me.refresh 放入 MDI 的 resize 事件中。

You could do all of that, or you could just put a me.refresh in the MDI's resize event.

猫烠⑼条掵仅有一顆心 2024-08-11 11:51:04

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

    }

Call PositionContainersToParentMiddle method in Resize event of your MDI form.
I have not tested it, but it should work. You might have to put conditions in Resize event to stop Image location change at every resize.

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

    }
冷血 2024-08-11 11:51:04
Private Sub YourMDIFormName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Resize
    Me.BackgroundImage = My.Resources.YourBackgroundImageName
    Me.Refresh()
 End Sub
Private Sub YourMDIFormName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Resize
    Me.BackgroundImage = My.Resources.YourBackgroundImageName
    Me.Refresh()
 End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文