简体   繁体   English

修复背景粉碎 C# winform

[英]Fix background crush C# winforms

I work on MDIParent with background image after i move a child from in it the background image crush like this我在 MDIParent 上使用背景图像在我将孩子从其中移动后,背景图像像这样粉碎在此处输入图片说明

how can i solve this issue in C# winforms??我如何在 C# winforms 中解决这个问题?

The MDIClient Control is the object used, in Multiple-Document Interface, as the Container for all child Forms. MDIClient控件是在多文档界面中用作所有子窗体的容器的对象。

This Control has a BackGroundImage property.此控件具有BackGroundImage属性。 You can set an Image object using this property, instead of using the [MDIParent].BackGroundImage .您可以使用此属性设置 Image 对象,而不是使用[MDIParent].BackGroundImage This would actually solve the Image tearing problem.这实际上可以解决图像撕裂问题。
But, you can't set a specific Layout property.但是,您不能设置特定的 Layout 属性。 See the Docs about the MDIClient BackgroundImageLayout :请参阅有关MDIClient BackgroundImageLayout的文档:

This property is not relevant to this class.此属性与此类无关。

You can set this property, but it's ignored: the default ImageLayout.Tile is used instead.您可以设置此属性,但它会被忽略:改为使用默认的ImageLayout.Tile
A different Layout can be set, assigning the Image object to the MDIParent BackGroundImage and specifying a BackGroundImageLayout .可以设置不同的布局,将 Image 对象分配给MDIParent BackGroundImage并指定BackGroundImageLayout This will change the Layout, but it will also cause the tearing effect you are reporting.这会改变布局,但也会导致你报告的撕裂效果。

A possible solution is to draw the Image object on the MDIClient surface, using it's Paint() event as usual.一个可能的解决方案是在MDIClient表面上绘制 Image 对象,像往常一样使用它的Paint()事件。
This will solve the tearing effect.这将解决撕裂效应。 Not the flickering;不是闪烁; this can be noticed when you resize the MDIParent Form (well, MDI applications are not resized that often, maybe maximized and normalized).当您调整MDIParent表单的大小时,可以注意到这MDIParent (嗯,MDI 应用程序不会经常调整大小,可能会最大化和标准化)。
Some flickering can be seen when the background Image is not covered by a child Form.当背景图像没有被子窗体覆盖时,可以看到一些闪烁。

A small adjustment to the Image specs is required: setting its DPI resolution = to the MDIParent reported device context DPI, otherwise the Image size will not match the default size (it's affected by the DPI resolution).需要对图像规范进行小幅调整:将其 DPI 分辨率设置为MDIParent报告的设备上下文 DPI,否则图像大小将与默认大小不匹配(受 DPI 分辨率影响)。

See this post for a description:有关说明,请参阅此帖子:
Image is not drawn at the correct spot . 图像未在正确位置绘制

An example:一个例子:
(Here, I assume the background Image is loaded from the Project resources) (这里,我假设背景图片是从项目资源中加载的)

public partial class MDIParent : Form
{
    private MdiClient mdiBackground = null;
    private Bitmap BackGroundImage = null;

    public MDIParent()
    {
        InitializeComponent();
        //Specify an existing Resources' Image
        BackGroundImage = new Bitmap(Properties.Resources.[Some Res Image] as Bitmap);
        BackGroundImage.SetResolution(this.DeviceDpi, this.DeviceDpi);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        mdiBackground = this.Controls.OfType<MdiClient>().First();

        mdiBackground.Paint += (s, evt) => {
            evt.Graphics.DrawImage(BackGroundImage,
                    (mdiBackground.Width - BackGroundImage.Width) / 2.0F,
                    (mdiBackground.Height - BackGroundImage.Height) / 2.0F);
        };
        //Show some child Forms on start-up
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (this.mdiBackground != null) mdiBackground.Invalidate();
    }

    private void MDIParent_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (BackGroundImage != null) BackGroundImage.Dispose();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM