窗口重绘的问题
窗口初始化的时候设置了不透明:SetStyle(ControlStyles.Opaque, true);
然后在OnPaint方法和load,paint,move事件都重新画一下,但是有个问题。一开始的窗口是可以看到后面的东西的(因为没有画背景),但是移动之后,还是原来的东西,没有变。窗口最小化,在最大化之后,就正常了。不知道我有没有描述清楚,代码我贴出来:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DrawImage
{
public partial class Form1 : Form
{
private Image theImage;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = Image.FromFile("E:\\Snow.png");
}
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawImage(theImage, this.ClientRectangle);
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bufferBitmap;
Image bufferImage;
bufferBitmap = new Bitmap(this.Width, this.Height);
bufferImage = bufferBitmap;
Graphics g = Graphics.FromImage(bufferImage);
g.DrawImage(theImage, this.ClientRectangle);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(theImage, this.ClientRectangle);
}
private void Form1_Move(object sender, EventArgs e)
{
Bitmap bufferBitmap;
Image bufferImage;
bufferBitmap = new Bitmap(this.Width, this.Height);
bufferImage = bufferBitmap;
Graphics g = Graphics.FromImage(bufferImage);
g.DrawImage(theImage, this.ClientRectangle);
}
}
}
我想问问怎么才能在移动窗口之后,窗口还是可以正确显示后面的内容啊? --------------------编程问答-------------------- 其实您所说的"最大化后正常"是假像.只不过是重新截了张图.
而您的整个代码过程就是类似不断截图不断贴图的过程.
如果将窗体快速拖动,那么您的界面将是.....相当耗资源.
如果您需要将PNG透明图作为窗体界面,可参考:
http://topic.csdn.net/u/20090720/16/8d662d3a-1fe3-4204-a4b5-10fbe70162c7.html --------------------编程问答-------------------- 你在代码中加上双缓存机制试试,网上有类似的代码
补充:.NET技术 , C#