一个简单的技术来提高Windows窗体的背景图像的绘制

介绍 这篇文章是关于一个简单的技巧来提高Windows的绘画表现形式。当一个背景图像用于形式和数量的控制,特别是当有很多透明背景的控制,你一定会面临一个问题呈现控件。你可以单独呈现控件加载后的形式。即使你让AllPaintingInWmPaint使用SetStyle双缓冲,不会提高性能。 (双缓冲内存有限环境中可以创建问题/终端服务器环境/如果你想使用OpenGL应用程序像VTK画在你的窗口。) 本文向您展示了如何克服这幅画问题甚至没有使用双缓冲。 使用的代码 技术是压倒一切的背景图像属性返回一个图像(位图),GDI很容易画。同样,你可以包括代码调整大小。这将避免变焦/拉伸需要的同时,进一步提高性能。 隐藏,收缩,复制Code

//C++ Source code
property System::Drawing::Image^ BackgroundImage 
{
    virtual System::Drawing::Image^ BackgroundImage::get()override
    {
        return this->m_bmpRenderBitMap;
    }

    virtual void BackgroundImage::set(System::Drawing::Image ^value) override
    {
        if(nullptr==value)
        {
            m_bmpRenderBitMap = nullptr;
            return;
        }
        //Create new BitMap Object of the size 
        this->m_bmpRenderBitMap = gcnew System::Drawing::Bitmap(value->Width, 
           value->Height, 
           System::Drawing::Imaging::PixelFormat::Format32bppPArgb);

        //Create graphics from image
        System::Drawing::Graphics ^g = 
          System::Drawing::Graphics::FromImage(this->m_bmpRenderBitMap);

        //set the graphics interpolation mode to high
        g->InterpolationMode = Drawing::Drawing2D::InterpolationMode::High;
        
        //draw the image to the graphics to create the new image 
        //which will be used in the onpaint background
        g->DrawImage(value, Rectangle(0, 0, this->m_bmpRenderBitMap->Width, 
                        this->m_bmpRenderBitMap->Height));
    }
}

同样可以在c#完成: 隐藏,收缩,复制Code

//C# Sample
public override Image BackgroundImage
{
    get
    {
        return bmpBackground;
    }
    set
    {
        if (value!=null)
        {
            //Create new BitMap Object of the size 
            bmpBackground = new Bitmap(value.Width,value.Height);

            //Create graphics from image
            System.Drawing.Graphics g = 
               System.Drawing.Graphics.FromImage(bmpBackground);
    
            //set the graphics interpolation mode to high
            g.InterpolationMode = 
               System.Drawing.Drawing2D.InterpolationMode.High;
    
            //draw the image to the graphics to create the new image 
            //which will be used in the onpaint background
            g.DrawImage(value, new Rectangle(0, 0, bmpBackground.Width, 
                        bmpBackground.Height)); 
        }
        else
            bmpBackground = null;
    }
}

发表了这篇文章后,我在谷歌搜索时,一个类似的文章被发现在MSDN博客:http://blogs.msdn.com/mhendersblog/archive/2005/10/12/480156.aspx。 本文转载于:http://www.diyabc.com/frontweb/news11188.html

posted @ 2020-08-11 04:02  Dincat  阅读(196)  评论(0编辑  收藏  举报