为什么设置双缓冲后,启动程序后不显示g.drawstring的内容,用鼠标在窗口滑动后才显示g.drawstring的内容。是什么地方出了问题

单应矩阵 2021-05-26 09:56:15
class DisplayTextForm : Form
{
public struct TextInfo
{
public string[] displayText;
public Rectangle[] rect;
}

[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);

[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

private TextInfo textInfos = new TextInfo();
private Label label1;
private int mouseFlag = -1;

public DisplayTextForm(string[] displayText,Form parentForm)
{
this.Size = new Size(300, 300);
this.Location = new Point(100, 100);
this.BackColor = Color.Red;
this.Show();
this.textInfos.displayText = displayText;
this.textInfos.rect = new Rectangle[5];
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DisplayTextForm_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DisplayTextForm_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DisplayTextForm_MouseMove);
this.DoubleBuffered = true;//设置本窗体
Invalidate();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
}


private void DisplayTextForm_Paint(object sender, PaintEventArgs e)
{
Draw();
}

public void Draw()
{
if (textInfos.displayText == null || textInfos.rect == null)
{
return;
}
Point textPos = new Point(0, 0);
using (Graphics g = this.CreateGraphics())
{
g.PageUnit = GraphicsUnit.Pixel;
g.SmoothingMode = SmoothingMode.HighQuality;
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
for (int i = 0; i < textInfos.displayText.Length; i++)
{
textPos = new Point(0, i * 20);
Font font = new Font("宋体", 10);
SizeF sizeF = g.MeasureString(textInfos.displayText[i], font, 500, sf);
Size size = System.Drawing.Size.Ceiling(sizeF);
textInfos.rect[i] = new Rectangle(textPos, size);

g.DrawRectangle(new Pen(Brushes.Black), new Rectangle(textPos, size));
if(mouseFlag != -1)
{
if (lastflag == mouseFlag) return;
g.FillRectangle(Brushes.Gray, textInfos.rect[mouseFlag]);
lastflag = mouseFlag;
}
g.DrawString(textInfos.displayText[i], font, Brushes.Black, textPos);
}
}
}

private int lastflag = -2;

private void DisplayTextForm_MouseMove(object sender, MouseEventArgs e)
{
Point mousePoint = e.Location;
for(int i = 0; i< textInfos.rect.Length; i++)
{
if(textInfos.rect[i].Contains(mousePoint))
{
mouseFlag = i;
Invalidate(textInfos.rect[i]);
}
}
}
...全文
298 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 05-26
DoubleBuffered = true 就是启用双缓存 显然再 SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 就多余了 虽然 SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 在编译时并无问题。但在调试时就通不过了
回复
刚才试了下,就算设置代码为

namespace 双缓冲
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;//设置本窗体
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawString("宋体sssss", new Font("宋体", 10), Brushes.Black, new Point(0, 0));
        }
    }
}
也是不显示的,把 this.DoubleBuffered = true;//设置本窗体 SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 给注释了,就能显示宋体sssss
回复
不设置双缓冲,刚启动就有用Graphics绘制的内容
回复
找到答案了,结帖。 Double-buffering is only enabled for the Paint event. You are directly drawing to the screen with CreateGraphics(), the g.Clear() call is very noticeable since it instantly erases the drawn image. Not drawing in the Paint event or OnPaint method is almost always a mistake.
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2021-05-26 09:56
社区公告

让您成为最强悍的C#开发者